Example #1
0
 def check_broken_blocks_links(self, blocks):
     for value in blocks.values():
         entity_map = value.get("text", {}).get("entityMap", {})
         for entity in entity_map.values():
             if entity.get("type") != "LINK":
                 continue
             href = entity.get("data", {}).get("href", "")
             url = entity.get("data", {}).get("url", "")
             RESOLVEUID_RE = re.compile(
                 "^[./]*resolve[Uu]id/([^/]*)/?(.*)$")
             MAIL_RE = r"^[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*$"
             for path in [href, url]:
                 if not path:
                     continue
                 if (path.startswith("http") or path.startswith("www")
                         or path.startswith("mailto")
                         or re.search(MAIL_RE, path)):
                     #  skip external links and emails
                     continue
                 match = RESOLVEUID_RE.match(path)
                 if match is None:
                     return True
                 uid, suffix = match.groups()
                 if not uuidToObject(uid):
                     return True
     return False
Example #2
0
    def getCurrentObject(self, portal=None):
        '''Returns object information for a selected object'''
        request = self.REQUEST
        if portal is None:
            portal = getToolByName(self, 'portal_url').getPortalObject()
        reference_tool = getToolByName(portal, 'reference_catalog')
        rt = self.getResourceType()
        portal_types = rt.portal_types
        src = request.get('src')
        # Remove any spurious query string or fragment
        for c in '#?':
            if c in src:
                src = src.split(c, 1)[0]

        match = UIDURL.match(src)

        if match:
            # src=http://someurl/resolveuid/<uid>
            uid = match.group(1)
            obj = uuidToObject(uid)
            
        elif src and '://' in src:
            # src=http://someurl/somepath/someobject
            base = portal.absolute_url()
            if src.startswith(base):
                src = src[len(base):].lstrip('/')
            try:
                obj = portal.restrictedTraverse(src)
                if portal_types:
                    while not shasattr(obj.aq_base, 'portal_type'):
                        obj = obj.aq_parent
                    while obj.portal_type not in portal_types:
                        obj = obj.aq_parent
                        if obj is portal:
                            return []
            except (KeyError, AttributeError):
                return []
        else:
            # src=<uid1> <uid2> ... <uidn>
            src = src.split(' ') # src is a list of uids.
            objects = [ uuidToObject(uid) for uid in src ]
            objects = [ o for o in objects if o is not None ]
            return objects

        if obj is None:
            return None
        return [obj]
 def test_uuidToURL_permission(self):
     from plone.outputfilters.browser.resolveuid import uuidToURL
     from plone.outputfilters.browser.resolveuid import uuidToObject
     self.portal.invokeFactory('Document', id='page', title='Page')
     page = self.portal['page']
     self.logout()
     self.assertEqual('http://nohost/plone/page', uuidToURL(page.UID()))
     self.assertTrue(page.aq_base is uuidToObject(page.UID()).aq_base)
 def lookup_uid(self, uid):
     context = self.context
     if HAS_LINGUAPLONE:
         # If we have LinguaPlone installed, add support for language-aware
         # references
         uids = translated_references(context, context.Language(), uid)
         if len(uids) > 0:
             uid = uids[0]
     return uuidToObject(uid)
 def lookup_uid(self, uid):
     context = self.context
     if HAS_LINGUAPLONE:
         # If we have LinguaPlone installed, add support for language-aware
         # references
         uids = translated_references(context, context.Language(), uid)
         if len(uids) > 0:
             uid = uids[0]
     return uuidToObject(uid)
    def test_uuidToURL_permission(self):
        from plone.outputfilters.browser.resolveuid import uuidToURL
        from plone.outputfilters.browser.resolveuid import uuidToObject

        self.portal.invokeFactory("Document", id="page", title="Page")
        page = self.portal["page"]
        self.logout()
        self.assertEqual("http://nohost/plone/page", uuidToURL(page.UID()))
        self.assertTrue(page.aq_base is uuidToObject(page.UID()).aq_base)
Example #7
0
    def getPathByUID(self):
        """Returns the absolute url of an object specified in the request by UID"""

        obj = uuidToObject(self.request.get('uid', ""))
        if obj is not None:
            return obj.absolute_url()
        else:
            self.request.response.setStatus(410)
            return ''
Example #8
0
 def resolveuid(self, context, reference_catalog, uid):
     """Convert a uid to an object by looking it up in the reference catalog.
     If not found then tries to fallback to a possible hook (e.g. so you could
     resolve uids on another system).
     """
     target = uuidToObject(uid)
     if target is not None:
         return target
     hook = getattr(context, 'kupu_resolveuid_hook', None)
     if hook is not None:
         target = hook(uid)
     return target
Example #9
0
    def getPathByUID(self):
        """Returns the path of an object specified in the request by UID"""

        context = self.context
        request = context.REQUEST

        if not hasattr(request, 'uid'):
            return ""

        uid = request['uid']
        obj = uuidToObject(uid)

        if obj:
            return obj.absolute_url()

        return ""
Example #10
0
    def _instanceFromRequest(self):
        """Get instance object from UID in request.
        Throws an error if there isn't a UID.
        """
        if not hasattr(self, '_instance'):
            # XXX TODO: this needs to handle the case where the object is
            # being created through portal_factory and hasn't yet been
            # saved. The UID won't be in the catalog so we need to create
            # a dummy object instead.
            tool = self._tool
            UID = tool.REQUEST.get('instance', None)
            if UID is None:
                return None
            self._instance = uuidToObject(UID)
        if self.subObject:
            return self._instance[self.subObject]

        return self._instance
Example #11
0
    def docontinue(self):
        """Scan selected documents looking for convertible links"""
        uids = self.uids
        if uids is None:
            self.uids = uids = []
            brains = self.portal_catalog.searchResults(self.mkQuery())
            for b in brains:
                uid = self.UIDfromBrain(b)
                if uid:
                    uids.append(uid)
            self._firstoutput = True
            self._continue = True
            return True

        pos = self.position
        self._total = total = len(uids)

        uids = uids[pos:pos+self.batch_size]
        self.position = pos + len(uids)
        if not uids:
            self._continue = False
            return False # Done

        self._objects = res = []
        for uid in uids:
            obj = uuidToObject(uid)
            if self.portal_type==FRAGMENT_TYPE and obj.portal_type!=FRAGMENT_TYPE:
                try:
                    fldr = obj.cp_container.titles
                except:
                    continue
                else:
                    for o in fldr.objectValues([FRAGMENT_TYPE]):
                        objinfo = self.object_check(o)
                        if objinfo:
                            res.append(objinfo)
            else:
                objinfo = self.object_check(obj)
                if objinfo:
                    res.append(objinfo)

        self._continue = True
        return True
Example #12
0
def uid_to_url(path):
    if not path:
        return ""
    match = RESOLVEUID_RE.match(path)
    if match is None:
        return path

    uid, suffix = match.groups()
    href = uuidToURL(uid)
    if href is None:
        return path
    if suffix:
        href += "/" + suffix
    else:
        target_object = uuidToObject(uid)
        if target_object:
            adapter = queryMultiAdapter((target_object, target_object.REQUEST),
                                        IObjectPrimaryFieldTarget)
            if adapter and adapter():
                href = adapter()
    return href
Example #13
0
    def resolveToPath(self, absurl):
        if 'resolveuid/' in absurl:
            bits = absurl.split('resolveuid/', 1)
            bits = bits[1].split('/',1)
            uid = bits[0]
            if len(bits)==1:
                tail = ''
            else:
                tail = '/' + bits[1]

            # TODO: should be able to convert uid to brain without
            # touching the actual object.
            obj = uuidToObject(uid)
            if obj is not None:
                newurl = obj.absolute_url()
                return uid, newurl, tail
            # If the uid doesn't exist then we can try the fallback
            # script. Even if the fallback works though we'll assume
            # an external link for simplicity.
            hook = getattr(self.tool, 'kupu_resolveuid_hook', None)
            if hook is not None:
                target = hook(uid)
                return None, target, ''
        return None, None, None
 def test_uuidToObject(self):
     from plone.outputfilters.browser.resolveuid import uuidToObject
     self.assertTrue(self.portal['image.jpg'].aq_base
                     is uuidToObject(self.UID).aq_base)
 def test_uuidToObject(self):
     from plone.outputfilters.browser.resolveuid import uuidToObject
     self.assertTrue(
         self.portal['image.jpg'].aq_base is uuidToObject(self.UID).aq_base)
 def lookup_uid(self, uid):
     return uuidToObject(uid)