From c3306d2c76a92dbd807837ea321d9c14363b7368 Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Tue, 26 Nov 2024 15:50:16 +0100 Subject: [PATCH 1/4] Handle aliases in path2uid --- src/plone/restapi/deserializer/utils.py | 11 +++++++++++ .../restapi/tests/test_blocks_deserializer.py | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/plone/restapi/deserializer/utils.py b/src/plone/restapi/deserializer/utils.py index 67d67d5556..c6b687fa2f 100644 --- a/src/plone/restapi/deserializer/utils.py +++ b/src/plone/restapi/deserializer/utils.py @@ -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"^(.*?)((?=/@@|#).*)?$") @@ -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("/") diff --git a/src/plone/restapi/tests/test_blocks_deserializer.py b/src/plone/restapi/tests/test_blocks_deserializer.py index 874e5446e8..95b61ad146 100644 --- a/src/plone/restapi/tests/test_blocks_deserializer.py +++ b/src/plone/restapi/tests/test_blocks_deserializer.py @@ -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 @@ -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.assertTrue(link.startswith("../resolveuid/")) From ca04194fed68386496b081c898652ecb5a2e2d5f Mon Sep 17 00:00:00 2001 From: Andrea Cecchi Date: Tue, 26 Nov 2024 15:58:37 +0100 Subject: [PATCH 2/4] add changelog --- news/1848.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 news/1848.feature diff --git a/news/1848.feature b/news/1848.feature new file mode 100644 index 0000000000..59295cf453 --- /dev/null +++ b/news/1848.feature @@ -0,0 +1 @@ +Handle aliases in path2uid to resolve also paths of renamed contents. @cekk From 4ea41ea4178cc25d49e864751bf92c4e96697568 Mon Sep 17 00:00:00 2001 From: David Glick Date: Mon, 16 Dec 2024 21:03:53 -0800 Subject: [PATCH 3/4] make sure it found the correct page --- src/plone/restapi/tests/test_blocks_deserializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plone/restapi/tests/test_blocks_deserializer.py b/src/plone/restapi/tests/test_blocks_deserializer.py index 95b61ad146..dcaf3bf2c8 100644 --- a/src/plone/restapi/tests/test_blocks_deserializer.py +++ b/src/plone/restapi/tests/test_blocks_deserializer.py @@ -737,4 +737,4 @@ def test_deserializer_resolve_path_also_if_it_is_an_alias(self): res = self.deserialize(blocks=blocks) link = res.blocks["abc"]["href"] - self.assertTrue(link.startswith("../resolveuid/")) + self.assertEqual(link, f"../resolveuid/{self.portal['renamed-doc'].UID()}") From 69e6c618a8a406033c83593fe9fc61e1183171b8 Mon Sep 17 00:00:00 2001 From: David Glick Date: Mon, 16 Dec 2024 21:06:31 -0800 Subject: [PATCH 4/4] adjust changelog --- news/1848.bugfix | 1 + news/1848.feature | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 news/1848.bugfix delete mode 100644 news/1848.feature diff --git a/news/1848.bugfix b/news/1848.bugfix new file mode 100644 index 0000000000..c9816615af --- /dev/null +++ b/news/1848.bugfix @@ -0,0 +1 @@ +Fix resolving paths in deserializer if the target was moved in the same request. @cekk diff --git a/news/1848.feature b/news/1848.feature deleted file mode 100644 index 59295cf453..0000000000 --- a/news/1848.feature +++ /dev/null @@ -1 +0,0 @@ -Handle aliases in path2uid to resolve also paths of renamed contents. @cekk