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

Handle aliases in path2uid #1848

Merged
merged 7 commits into from
Dec 17, 2024
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
1 change: 1 addition & 0 deletions news/1848.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix resolving paths in deserializer if the target was moved in the same request. @cekk
11 changes: 11 additions & 0 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
from plone.app.redirector.interfaces import IRedirectionStorage
from zope.component import getUtility

import re

PATH_RE = re.compile(r"^(.*?)((?=/@@|#).*)?$")
Expand Down Expand Up @@ -35,6 +38,14 @@ def path2uid(context, link):
suffix = match.group(2) or ""

obj = portal.unrestrictedTraverse(path, None)
if obj is None:
# last try: maybe the object or some parent has been renamed.
# if yes, there should be a reference into redirection storage
storage = getUtility(IRedirectionStorage)
alias_path = storage.get(path)
if alias_path:
path = alias_path
obj = portal.unrestrictedTraverse(path, None)
if obj is None or obj == portal:
return link
segments = path.split("/")
Expand Down
14 changes: 14 additions & 0 deletions src/plone/restapi/tests/test_blocks_deserializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from plone import api
from plone.dexterity.interfaces import IDexterityFTI
from plone.dexterity.interfaces import IDexterityItem
from plone.restapi.behaviors import IBlocks
Expand Down Expand Up @@ -724,3 +725,16 @@ def test_deserialize_url_with_image_scales(self):
res = self.deserialize(blocks=blocks)
self.assertTrue(res.blocks["123"]["url"].startswith("../resolveuid/"))
self.assertNotIn("image_scales", res.blocks["123"])

def test_deserializer_resolve_path_also_if_it_is_an_alias(self):

self.portal.invokeFactory(
"Document",
id="doc",
)
api.content.move(source=self.portal.doc, id="renamed-doc")
blocks = {"abc": {"href": "%s/doc" % self.portal.absolute_url()}}

res = self.deserialize(blocks=blocks)
link = res.blocks["abc"]["href"]
self.assertEqual(link, f"../resolveuid/{self.portal['renamed-doc'].UID()}")
Loading