def upgrade(self, ghost): target_path = ghost._content_path if target_path: try: target = ghost.get_root().unrestrictedTraverse(target_path) except (AttributeError, KeyError, NotFound, TypeError): logger.error( u'Unexisting target for Ghost %s: %s.', content_path(ghost), "/".join(target_path)) return ghost target = aq_inner(target) if not ISilvaObject.providedBy(target): logger.error( u'Ghost target is not a Silva object for: %s.', content_path(ghost)) return ghost try: [o for o in aq_iter(target, error=RuntimeError)] except RuntimeError: logger.error( u'Invalid target for Ghost %s: %s.', content_path(ghost), '/'.join(target_path)) return ghost if target is not None and ISilvaObject.providedBy(target): logger.info( u'Upgrading Ghost target for: %s.', "/".join(ghost.getPhysicalPath())) container = aq_parent(ghost).get_container() ghost.set_haunted( target, auto_delete=IGhostFolder.providedBy(container)) del ghost._content_path return ghost
def get_lookup_content(content): if IVersion.providedBy(content): content = content.get_silva_object() if (ISilvaObject.providedBy(content) and not IContainer.providedBy(content)): return content.get_container() return content
def apply(self, root, request): try: content = traverse(self.path, root, request) except zExceptions.BadRequest: return root request['URL'] = self.url request['ACTUAL_URL'] = self.server_url + urlparse.urlunparse( ('', '') + urlparse.urlparse(request['ACTUAL_URL'])[2:]) request['SERVER_URL'] = str(self.server_url) request._script = list(self.server_script) request._resetURLS() if self.skin: # Apply hardcoded skin skin = queryUtility(IBrowserSkinType, name=self.skin) if skin is None: logger.error( u"Missing skin '%s', please update your settings.", self.skin) else: applySkinButKeepSome(request, skin) if self.skin_enforce: request[SET_SKIN_ALLOWED_FLAG] = False else: # Fallback on the default Silva skin if ISilvaObject.providedBy(content): publication = content.get_publication() skin_lookup = ISkinLookup(publication, None) if skin_lookup is not None: skin = skin_lookup.get_skin(request) if skin is not None: # We found a skin to apply applySkinButKeepSome(request, skin) return content
def is_silva_content(self, content): """Is the content a Silva content ? You can override this method in a subclass to change this behaviour. """ cls = content['instance'] return (ISilvaObject.implementedBy(cls) or IVersion.implementedBy(cls))
def convert(identifier): try: content = get_content_from_id(int(identifier)) except ValueError: self.raise_error('invalid_value', field) if ISilvaObject.providedBy(content): return content return None
def set_smi_skin(context, request, default='silva.ui.interfaces.ISilvaUITheme'): """Set the SMI skin. """ if ISilvaObject.providedBy(context) or IVersion.providedBy(context): skin_name = context.get_root()._smi_skin if not skin_name: skin_name = default applySkin(request, getUtility(Interface, skin_name))
def has_default_metadata(self, content): """Tell if the content should have default metadata set sets. You can override this method in a subclass to change this behaviour. """ cls = content['instance'] return ((ISilvaObject.implementedBy(cls) and not interfaces.IVersionedObject.implementedBy(cls)) or IVersion.implementedBy(cls))
def current_section_class(self): before = None for parent in self.request.PARENTS: if IRoot.providedBy(parent): if before is not None and ISilvaObject.providedBy(before): return 'section-{0}'.format(before.getId()) else: break before = parent return 'section-root'
def _validate(self, value): super(ID, self)._validate(value) if self.context: context = self.context if IVersion.providedBy(context) or ISilvaObject.providedBy(context): context = context.get_container() try: ISilvaNameChooser(context).checkName(value, None) except ContentError as error: raise InvalidID(error.reason) return
def resolve_path(url, content_path, context, obj_type=u'link'): """Resolve path to an object or report an error. Return (url, target i.e an content in Silva, fragment). """ if isinstance(url, unicode): # If the link contains unicode, that is not a link. try: url.encode('ascii') except UnicodeEncodeError: logger.error(u"Invalid %s '%s' (contains unicode).", obj_type, url) return url, None, None url = url.strip() try: scheme, netloc, path, parameters, query, fragment = urlparse(url) except ValueError: logger.error(u"Invalid %s '%s' (is not a valid URL).", obj_type, url) # We quote them so they parse ... return urllib.quote(url), None, None if scheme: # This is a remote URL or invalid URL. #logger.debug(u'found a remote link %s' % url) return url, None, None if not path: # This is to an anchor in the document, nothing else return url, None, fragment try: cleaned_path, path_root = split_path(path, context) target = path_root.unrestrictedTraverse(cleaned_path) except (AttributeError, KeyError, NotFound, TypeError): # Try again using Silva Root as / try: cleaned_path, path_root = split_path( path, context, context.get_root()) target = path_root.unrestrictedTraverse(cleaned_path) except (AttributeError, KeyError, NotFound, TypeError): logger.warn( u'Cannot resolve %s %s in %s', obj_type, url, content_path) return url, None, fragment if not ISilvaObject.providedBy(target): logger.error( u'%s %s did not resolve to a Silva content in %s', obj_type, path, content_path) return url, None, fragment try: [o for o in aq_iter(target, error=RuntimeError)] return url, target, fragment except RuntimeError: logger.error( u'Invalid target %s %s in %s', obj_type, path, content_path) return url, None, fragment
def __init__(self, screen): # Follow Zope 2 information to appear in the undo log. note = [] if ISilvaObject.providedBy(screen.context) or IVersion.providedBy(screen.context): note.append("/".join(screen.context.getPhysicalPath())) else: note.append("/") names = [] while IRESTComponent.providedBy(screen): names.append(screen.__name__) screen = screen.__parent__ if names: note.extend(["SMI action:", "/".join(reversed(names))]) self.note = " ".join(note) self.user = getSecurityManager().getUser() self.user_path = "" auth_folder = aq_parent(self.user) if auth_folder is not None: self.user_path = "/".join(auth_folder.getPhysicalPath()[1:-1])
def __call__(self, value): multiple = bool(self.field._field.get_value('multiple')) if isinstance(value, ReferencesSolver): value.defer(self.__call__, not multiple, self.context) return if value is None: value = [] elif multiple: assert isinstance(value, list) else: assert ISilvaObject.providedBy(value) value = [value] if self.identifier in self.content.__dict__: identifier = self.content.__dict__[self.identifier] else: identifier = unicode(uuid.uuid1()) self.content.__dict__[self.identifier] = identifier self.content._p_changed = True references = ReferenceSet(self.context, identifier) references.set(value)
def _get_login_page(self, request): parent = request.PARENTS and request.PARENTS[0] or None if ISilvaObject.providedBy(parent): root = parent.get_publication() else: root = IVirtualSite(request).get_root() if request.environ.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest': login_path = 'xml_login_form.html' else: login_path = self.login_path # Restory the public skin self._restore_public_skin(request, root) page = None if parent is not None: page = queryMultiAdapter((parent, request), name=login_path) if page is None: page = queryMultiAdapter((root, request), name=login_path) if page is not None: # Set parent and name for URL (and security) page.__parent__ = root page.__name__ = '@@' + login_path return page
def run(self, root, options): target = root target_path = options.folder or options.content to_version = root.get_silva_software_version() from_version = options.version if target_path and not from_version: logger.error('You need to provide --from-version ' 'to upgrade a folder or a content.') exit(3) if target_path: try: target = root.unrestrictedTraverse(target_path) except (KeyError, AttributeError): logger.error('Invalid content path %s.', target_path) exit(1) if not ISilvaObject.providedBy(target): logger.error('Content %s is not a Silva content.', target_path) exit(2) if not from_version: from_version = root.get_silva_content_version() logger.info("Upgrade from version %s to version %s." % ( from_version, to_version)) if options.content: registry.upgrade_content( target, from_version, to_version, whitelist=options.require_tags, blacklist=options.exclude_tags) else: registry.upgrade( target, from_version, to_version, whitelist=options.require_tags, blacklist=options.exclude_tags) if IRoot.providedBy(target): target._content_version = to_version transaction.commit()
def _validate(self): """ test if the given id is valid, returning a status code about its validity or reason of invalidity """ folder = self._context maybe_id = self._maybe_id allow_dup = self._allow_dup if self._valid_id.match(maybe_id) is None: return self.CONTAINS_BAD_CHARS if maybe_id.endswith('__'): # ugly, but Zope explicitely checks this ... return self.RESERVED_POSTFIX prefixing = maybe_id.split('_') if (len(prefixing) > 1) and (prefixing[0] in self._reserved_prefixes): return self.RESERVED_PREFIX if maybe_id in self._reserved_ids: return self.RESERVED if self._interface is not None: for interface, prefixes in \ list(self._reserved_ids_for_interface.items()): if self._interface.isOrExtends(interface): if maybe_id in prefixes: return self.RESERVED_FOR_CONTENT attr = getattr(aq_inner(folder), maybe_id, _marker) if attr is not _marker: if ISilvaObject.providedBy(attr): # there is a silva object with the same id if allow_dup: return self.OK attr = getattr(folder.aq_base, maybe_id, _marker) if attr is _marker: # shadowing a content object is ok (hopefully) return self.OK if IAsset.providedBy(attr): return self.IN_USE_ASSET # else it must be a content object (?) return self.IN_USE_CONTENT # check if object with this id is acquired; if not, it cannot be # allowed attr2 = getattr(folder.aq_base, maybe_id, _marker) if attr2 is not _marker: #either it is an attribute/method (self.RESERVED) #or it is an object within the container (self.IN_USE_ZOPE) if maybe_id in folder.objectIds(): return self.IN_USE_ZOPE else: return self.RESERVED # object using wanted id is acquried # now it may be a Zope object, which is allowed (for now) # or it is an attribute (which is disallowed) if not hasattr(attr, 'meta_type'): # not a zope object (guessing ...) return self.RESERVED try: # Call Zope verification function checkValidId(folder, str(maybe_id), allow_dup) except BadRequest: return self.CONTAINS_BAD_CHARS return self.OK
def _validate(self): """ test if the given id is valid, returning a status code about its validity or reason of invalidity """ folder = self._context maybe_id = self._maybe_id allow_dup = self._allow_dup if self._valid_id.match(maybe_id) is None: return self.CONTAINS_BAD_CHARS if maybe_id.endswith('__'): # ugly, but Zope explicitely checks this ... return self.RESERVED_POSTFIX prefixing = maybe_id.split('_') if (len(prefixing) >1) and (prefixing[0] in self._reserved_prefixes): return self.RESERVED_PREFIX if maybe_id in self._reserved_ids: return self.RESERVED if self._interface is not None: for interface, prefixes in \ self._reserved_ids_for_interface.items(): if self._interface.isOrExtends(interface): if maybe_id in prefixes: return self.RESERVED_FOR_CONTENT attr = getattr(aq_inner(folder), maybe_id, _marker) if attr is not _marker: if ISilvaObject.providedBy(attr): # there is a silva object with the same id if allow_dup: return self.OK attr = getattr(folder.aq_base, maybe_id, _marker) if attr is _marker: # shadowing a content object is ok (hopefully) return self.OK if IAsset.providedBy(attr): return self.IN_USE_ASSET # else it must be a content object (?) return self.IN_USE_CONTENT # check if object with this id is acquired; if not, it cannot be # allowed attr2 = getattr(folder.aq_base, maybe_id, _marker) if attr2 is not _marker: #either it is an attribute/method (self.RESERVED) #or it is an object within the container (self.IN_USE_ZOPE) if maybe_id in folder.objectIds(): return self.IN_USE_ZOPE else: return self.RESERVED # object using wanted id is acquried # now it may be a Zope object, which is allowed (for now) # or it is an attribute (which is disallowed) if not hasattr(attr, 'meta_type'): # not a zope object (guessing ...) return self.RESERVED try: # Call Zope verification function checkValidId(folder, str(maybe_id), allow_dup) except BadRequest: return self.CONTAINS_BAD_CHARS return self.OK
def search_url(self): search = getattr(self.context, "search", None) if ISilvaObject.providedBy(search): # get_silva_object cleanup the Acquisition path return absoluteURL(search.get_silva_object(), self.request) return None