Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
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