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

Improve the block transforms to include also anchors when resolving uid back and forth #1746

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/1746.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve the block transforms to include also anchors when resolving uid back and forth @sneridagh
13 changes: 8 additions & 5 deletions src/plone/restapi/deserializer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ def path2uid(context, link):
)

# handle edge-case when we have non traversable path like /@@download/file
if "/@@" in path:
path, suffix = path.split("/@@", 1)
suffix = "/@@" + suffix
else:
suffix = ""
SUFFIXES = ["/@@", "#"]
suffix = ""
for suffix_separator in SUFFIXES:
if suffix_separator in path:
path, suffix = path.split(suffix_separator, 1)
suffix = suffix_separator + suffix

obj = portal.unrestrictedTraverse(path, None)
if obj is None or obj == portal:
return link
Expand All @@ -39,6 +41,7 @@ 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: 9 additions & 3 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,13 +23,19 @@ def resolve_uid(path):
if match is None:
return path, None

uid, suffix = match.groups()
uid, suffix, anchor = 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
93 changes: 92 additions & 1 deletion src/plone/restapi/tests/test_blocks_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class TestBlocksDeserializer(unittest.TestCase):

layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING

def setUp(self):
Expand Down Expand Up @@ -444,6 +443,98 @@ def test_slate_simple_link_deserializer(self):
link = value[0]["children"][1]["data"]["url"]
self.assertTrue(link.startswith("../resolveuid/"))

def test_slate_simple_link_deserializer_with_anchor(self):
blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": "%s/image-1#anchor-id"
% self.portal.absolute_url()
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}

res = self.deserialize(blocks=blocks)
value = res.blocks["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(link, f"../resolveuid/{self.image.UID()}#anchor-id")

def test_slate_simple_link_deserializer_with_suffix(self):
blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": "%s/image-1/@@download/file"
% self.portal.absolute_url()
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}

res = self.deserialize(blocks=blocks)
value = res.blocks["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(link, f"../resolveuid/{self.image.UID()}/@@download/file")

def test_slate_simple_link_deserializer_with_suffix_and_anchor(self):
blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": "%s/image-1/@@download/file#anchor-id"
% self.portal.absolute_url()
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}

res = self.deserialize(blocks=blocks)
value = res.blocks["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(
link, f"../resolveuid/{self.image.UID()}/@@download/file#anchor-id"
)

def test_aquisition_messing_with_link_deserializer(self):
self.portal.invokeFactory(
"Folder",
Expand Down
109 changes: 106 additions & 3 deletions src/plone/restapi/tests/test_blocks_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


class TestBlocksSerializer(unittest.TestCase):

layer = PLONE_RESTAPI_DX_INTEGRATION_TESTING

def setUp(self):
Expand Down Expand Up @@ -285,7 +284,111 @@ def test_simple_link_serializer(self):
)
value = res["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertTrue(link, self.portal.absolute_url() + "/doc1")
self.assertEqual(link, self.portal.absolute_url() + "/doc1")

def test_simple_link_serializer_with_anchor(self):
doc_uid = IUUID(self.portal["doc1"])
resolve_uid_link = f"../resolveuid/{doc_uid}#anchor-id"

blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": resolve_uid_link,
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}
res = self.serialize(
context=self.portal["doc1"],
blocks=blocks,
)
value = res["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
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"])
resolve_uid_link = f"../resolveuid/{doc_uid}/@@download/file"

blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": resolve_uid_link,
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}
res = self.serialize(
context=self.portal["doc1"],
blocks=blocks,
)
value = res["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(link, f"{self.portal['doc1'].absolute_url()}/@@download/file")

def test_simple_link_serializer_with_suffix_and_anchor(self):
doc_uid = IUUID(self.portal["doc1"])
resolve_uid_link = f"../resolveuid/{doc_uid}/@@download/file#anchor-id"

blocks = {
"abc": {
"@type": "slate",
"plaintext": "Frontpage content here",
"value": [
{
"children": [
{"text": "Frontpage "},
{
"children": [{"text": "content "}],
"data": {
"url": resolve_uid_link,
},
"type": "link",
},
{"text": "here"},
],
"type": "h2",
}
],
}
}
res = self.serialize(
context=self.portal["doc1"],
blocks=blocks,
)
value = res["abc"]["value"]
link = value[0]["children"][1]["data"]["url"]
self.assertEqual(
link, f"{self.portal['doc1'].absolute_url()}/@@download/file#anchor-id"
)

def test_slate_table_block_link_serializer(self):
doc_uid = IUUID(self.portal["doc1"])
Expand Down Expand Up @@ -388,7 +491,7 @@ def test_slate_table_block_link_serializer(self):
rows = res["abc"]["table"]["rows"]
cell = rows[1]["cells"][0]
link = cell["value"][0]["children"][1]["data"]["url"]
self.assertTrue(link, self.portal.absolute_url() + "/doc1")
self.assertEqual(link, self.portal.absolute_url() + "/doc1")

@unittest.skipUnless(
HAS_PLONE_6,
Expand Down
Loading