Skip to content

Commit

Permalink
fix suffix handling
Browse files Browse the repository at this point in the history
  • Loading branch information
davisagli committed Feb 1, 2024
1 parent 2a8c881 commit 196fd42
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
15 changes: 8 additions & 7 deletions src/plone/restapi/deserializer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from plone.uuid.interfaces import IUUID
from plone.uuid.interfaces import IUUIDAware
from zope.component import getMultiAdapter
import re

PATH_RE = re.compile(r"^(.*?)((?=/@@|#).*)?$")


def path2uid(context, link):
Expand All @@ -24,13 +27,12 @@ def path2uid(context, link):
portal_path=portal_path, path=path.lstrip("/")
)

# handle edge-case when we have non traversable path like /@@download/file
SUFFIXES = ["/@@", "#"]
# handle edge cases with suffixes like /@@download/file or a fragment
suffix = ""
for suffix_separator in SUFFIXES:
if suffix_separator in path:
path, suffix = path.split(suffix_separator, 1)
suffix = suffix_separator + suffix
match = PATH_RE.match(path)
if match is not None:
path = match.group(1)
suffix = match.group(2) or ""

obj = portal.unrestrictedTraverse(path, None)
if obj is None or obj == portal:
Expand All @@ -41,7 +43,6 @@ def path2uid(context, link):
if obj is None:
break
suffix = "/" + segments.pop() + suffix
print(suffix)
# check if obj is wrong because of acquisition
if not obj or "/".join(obj.getPhysicalPath()) != "/".join(segments):
return link
Expand Down
12 changes: 3 additions & 9 deletions src/plone/restapi/serializer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re


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


def resolve_uid(path):
Expand All @@ -23,19 +23,13 @@ def resolve_uid(path):
if match is None:
return path, None

uid, suffix, anchor = match.groups()
uid, suffix = match.groups()
brain = uuidToCatalogBrain(uid)
if brain is None:
return path, None
href = brain.getURL()
SUFFIXES = ["/@@", "#"]
suffix = ""
for suffix_separator in SUFFIXES:
if suffix_separator in path:
path, suffix = path.split(suffix_separator, 1)
suffix = suffix_separator + suffix
if suffix:
return href + suffix, brain
return href + "/" + suffix, brain
target_object = brain._unrestrictedGetObject()
adapter = queryMultiAdapter(
(target_object, target_object.REQUEST),
Expand Down
2 changes: 1 addition & 1 deletion src/plone/restapi/tests/test_blocks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def test_simple_link_serializer_with_anchor(self):
)
value = res["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(link, f"{self.portal['doc1'].absolute_url()}#anchor-id")
self.assertEqual(link, f"{self.portal['doc1'].absolute_url()}/#anchor-id")

def test_simple_link_serializer_with_suffix(self):
doc_uid = IUUID(self.portal["doc1"])
Expand Down

0 comments on commit 196fd42

Please sign in to comment.