Skip to content

Commit bafd748

Browse files
fix(types/utils): pass root context when no DX content in layout (#1917)
* fix(types/utils): pass root context when no DX content in layout * chore: amend changelog * tests(types): patch(update) blocks in DX layout * tests(types): move tests in existing method * Get the site root a better way in path2uid --------- Co-authored-by: David Glick <[email protected]>
1 parent 6ba17e4 commit bafd748

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

news/1917.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `AttributeError` when updating the default blocks layout for a content type. @nileshgulia, @davisagli

src/plone/restapi/deserializer/utils.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,36 @@
22
from plone.app.redirector.interfaces import IRedirectionStorage
33
from plone.uuid.interfaces import IUUID
44
from plone.uuid.interfaces import IUUIDAware
5-
from zope.component import getMultiAdapter
5+
from Products.CMFCore.interfaces import ISiteRoot
66
from zope.component import getUtility
7+
from zope.component.hooks import getSite
78

89
import re
910

1011

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

1314

15+
def get_portal():
16+
closest_site = getSite()
17+
if closest_site is not None:
18+
for potential_portal in closest_site.aq_chain:
19+
if ISiteRoot.providedBy(potential_portal):
20+
return potential_portal
21+
raise Exception("Plone site root not found")
22+
23+
1424
def path2uid(context, link):
15-
# unrestrictedTraverse requires a string on py3. see:
16-
# https://github.com/zopefoundation/Zope/issues/674
1725
if not link:
1826
return ""
19-
portal = getMultiAdapter(
20-
(context, context.REQUEST), name="plone_portal_state"
21-
).portal()
27+
portal = get_portal()
2228
portal_url = portal.portal_url()
2329
portal_path = "/".join(portal.getPhysicalPath())
2430
path = link
25-
context_url = context.absolute_url()
31+
try:
32+
context_url = context.absolute_url()
33+
except AttributeError:
34+
context_url = portal_url
2635
relative_up = len(context_url.split("/")) - len(portal_url.split("/"))
2736
if path.startswith(portal_url):
2837
path = path[len(portal_url) + 1 :]

src/plone/restapi/tests/test_services_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ def test_types_document_patch_create_missing(self):
298298
},
299299
"5060e030-727b-47bc-8023-b80b7cccd96f": {"@type": "image"},
300300
"e3d8f8e4-8fee-47e7-9451-28724bf74a90": {"@type": "text"},
301+
"d6a0a308-0757-4713-bfe0-359817b364cd": {
302+
"@type": "slate",
303+
"value": [
304+
{
305+
"type": "p",
306+
"children": [
307+
{"text": ""},
308+
{
309+
"type": "link",
310+
"data": {"url": "/doc1"},
311+
"children": [{"text": "Plone"}],
312+
},
313+
{"text": ""},
314+
],
315+
}
316+
],
317+
"plaintext": " Plone ",
318+
},
301319
},
302320
},
303321
"blocks_layout": {
@@ -311,6 +329,7 @@ def test_types_document_patch_create_missing(self):
311329
"338013ce-acca-454f-a6f4-14113c187dca",
312330
"5060e030-727b-47bc-8023-b80b7cccd96f",
313331
"e3d8f8e4-8fee-47e7-9451-28724bf74a90",
332+
"d6a0a308-0757-4713-bfe0-359817b364cd",
314333
]
315334
},
316335
},

0 commit comments

Comments
 (0)