Skip to content

Commit

Permalink
Merge pull request #72 from RedTurtle/fix_resolveuid_links
Browse files Browse the repository at this point in the history
Fix resolveuid links also for Link urls
  • Loading branch information
cekk authored Aug 29, 2023
2 parents 9a5f357 + 6db1354 commit 2df0722
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ Changelog
5.2.1 (unreleased)
------------------

- Use plone.volto uid_to_url method to convert resolveuid links in summary.
[cekk]
- Patch plone.restapi RESOLVEUID_RE regexp to catch more urls.
[cekk]
- Ignore non-existing indexes in custom ranking.
[cekk]


5.2.0 (2023-08-21)
------------------
Expand Down
7 changes: 5 additions & 2 deletions src/redturtle/volto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from zope.i18nmessageid import MessageFactory
from ZTUtils.Lazy import LazyCat
from ZTUtils.Lazy import LazyMap

from plone.restapi.serializer import utils

import logging

import re

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,3 +76,6 @@ def Catalog_sortResults(
logger.info("install monkey patch for Products.ZCatalog.Catalog.Catalog.sortResults")
Catalog._orig_sortResults = Catalog.sortResults
Catalog.sortResults = Catalog_sortResults

# patch plone.restapi regexp to catch also other
utils.RESOLVEUID_RE = re.compile(".*?/resolve[Uu]id/([^/]*)/?(.*)$")
19 changes: 4 additions & 15 deletions src/redturtle/volto/restapi/serializer/dxfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
from plone.app.contenttypes.utils import replace_link_variables_by_paths
from plone.app.dexterity.behaviors.metadata import IPublication
from plone.dexterity.interfaces import IDexterityContent
from plone.outputfilters.browser.resolveuid import uuidToURL
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.serializer.dxfields import DefaultFieldSerializer
from plone.restapi.serializer.utils import uid_to_url
from redturtle.volto.interfaces import IRedturtleVoltoLayer
from zope.component import adapter
from zope.component import getMultiAdapter
from zope.interface import implementer
from zope.schema.interfaces import IDatetime
from zope.schema.interfaces import ITextLine


import re


Expand All @@ -26,21 +26,10 @@ def __call__(self):
if self.field.getName() != "remoteUrl":
return super(TextLineFieldSerializer, self).__call__()
value = self.get_value()

path = replace_link_variables_by_paths(context=self.context, url=value)
match = RESOLVEUID_RE.match(path)
if match:
uid, suffix = match.groups()
value = uuidToURL(uid)
else:
portal = getMultiAdapter(
(self.context, self.context.REQUEST), name="plone_portal_state"
).portal()
ref_obj = portal.restrictedTraverse(path, None)
if ref_obj:
value = ref_obj.absolute_url()
url = uid_to_url(path)

return json_compatible(value)
return json_compatible(url)


@adapter(IDatetime, IDexterityContent, IRedturtleVoltoLayer)
Expand Down
26 changes: 8 additions & 18 deletions src/redturtle/volto/restapi/serializer/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
from plone import api
from plone.app.contenttypes.interfaces import ILink
from plone.app.contenttypes.utils import replace_link_variables_by_paths
from plone.outputfilters.browser.resolveuid import uuidToURL
from plone.restapi.deserializer import json_body
from plone.restapi.imaging import get_scale_infos
from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.serializer.summary import (
DefaultJSONSummarySerializer as BaseSerializer,
)
from plone.restapi.serializer.utils import uid_to_url
from redturtle.volto.interfaces import IRedturtleVoltoLayer
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface

import re


RESOLVEUID_RE = re.compile(".*?/resolve[Uu]id/([^/]*)/?(.*)$")

EMPTY_STRINGS = ["None"]

Expand Down Expand Up @@ -89,19 +85,13 @@ def get_remote_url(self):
# it isn't an internal link, so we can return it
return value
path = replace_link_variables_by_paths(context=self.context, url=value)
match = RESOLVEUID_RE.match(path)
if match:
uid, suffix = match.groups()
return uuidToURL(uid)
else:
portal = api.portal.get()
try:
ref_obj = portal.restrictedTraverse(path, None)
if ref_obj:
return ref_obj.absolute_url()
except Exception:
return ""
return ""

url = uid_to_url(path)

if url == path:
# something wrong with the path, maybe a missing object?
return ""
return url

def __call__(self, force_all_metadata=False):
if force_all_metadata:
Expand Down
37 changes: 37 additions & 0 deletions src/redturtle/volto/tests/test_resolveuid_re_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.restapi.serializer.utils import uid_to_url
from redturtle.volto.testing import REDTURTLE_VOLTO_FUNCTIONAL_TESTING

import unittest


class TestRESOLVEUIDREPatch(unittest.TestCase):
layer = REDTURTLE_VOLTO_FUNCTIONAL_TESTING

def setUp(self):
self.app = self.layer["app"]
self.portal = self.layer["portal"]
self.portal_url = self.portal.absolute_url()

setRoles(self.portal, TEST_USER_ID, ["Manager"])

self.document = api.content.create(
container=self.portal,
type="Document",
title="Document",
)

def test_uuid_to_url_with_link_pattern(self):
path = f"/Plone/resolveuid/{self.document.UID()}"

res = uid_to_url(path)
self.assertEqual(res, self.document.absolute_url())

def test_uuid_to_url_with_link_pattern_with_suffix(self):
path = f"/Plone/resolveuid/{self.document.UID()}/@@download/file"

res = uid_to_url(path)
self.assertEqual(res, f"{self.document.absolute_url()}/@@download/file")

0 comments on commit 2df0722

Please sign in to comment.