Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolveuid links also for Link urls #72

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Loading