Skip to content

Commit

Permalink
Improve the block transforms to include also anchors when resolving u…
Browse files Browse the repository at this point in the history
…id back and forth
  • Loading branch information
sneridagh committed Jan 30, 2024
1 parent af1d223 commit ad8d359
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 11 deletions.
12 changes: 7 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 Down
6 changes: 4 additions & 2 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,15 @@ 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()
if suffix:
return href + "/" + suffix, brain
if anchor:
return href + anchor, brain
target_object = brain._unrestrictedGetObject()
adapter = queryMultiAdapter(
(target_object, target_object.REQUEST),
Expand Down
61 changes: 60 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,66 @@ 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_aquisition_messing_with_link_deserializer(self):
self.portal.invokeFactory(
"Folder",
Expand Down
73 changes: 70 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,75 @@ 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_slate_table_block_link_serializer(self):
doc_uid = IUUID(self.portal["doc1"])
Expand Down Expand Up @@ -388,7 +455,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

0 comments on commit ad8d359

Please sign in to comment.