Skip to content

Commit

Permalink
Merge 4792364 into 4a3ad2b
Browse files Browse the repository at this point in the history
  • Loading branch information
avoinea committed Dec 16, 2020
2 parents 4a3ad2b + 4792364 commit 88fa2a6
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/995.feature
@@ -0,0 +1 @@
- Add expandable `@layout` endpoint to easily access content-type blocks layout [@avoinea]
1 change: 1 addition & 0 deletions src/plone/restapi/services/configure.zcml
Expand Up @@ -15,6 +15,7 @@
<include package=".database"/>
<include package=".discussion"/>
<include package=".groups"/>
<include package=".layout"/>
<include package=".navigation"/>
<include package=".history"/>
<include package=".locking" />
Expand Down
Empty file.
24 changes: 24 additions & 0 deletions src/plone/restapi/services/layout/configure.zcml
@@ -0,0 +1,24 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:plone="http://namespaces.plone.org/plone"
xmlns:zcml="http://namespaces.zope.org/zcml">

<plone:service
method="GET"
name="@layout"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
factory=".get.Layout"
permission="zope2.View"
/>

<plone:service
method="GET"
name="@layout"
for="Products.CMFCore.interfaces.IContentish"
factory=".get.LayoutGet"
permission="zope2.View"
/>

<adapter factory=".get.Layout" name="layout" />

</configure>
45 changes: 45 additions & 0 deletions src/plone/restapi/services/layout/get.py
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
from plone.restapi.interfaces import IExpandableElement
from plone.restapi.serializer.converters import json_compatible
from plone.restapi.services import Service
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.interfaces import IPloneSiteRoot
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface


@implementer(IExpandableElement)
@adapter(Interface, Interface)
class Layout(object):
def __init__(self, context, request):
self.context = context
self.request = request

def __call__(self, expand=False):
result = {"layout": {"@id": "{}/@layout".format(self.context.absolute_url())}}
if not expand:
return result

if IPloneSiteRoot.providedBy(self.context):
return result

ttool = getToolByName(self.context, "portal_types")
ptype = getattr(self.context, "portal_type")
if not ptype:
return result

fti = ttool[ptype]
schema = fti.lookupSchema()
for field in schema:
if field.endswith("blocks") or field.endswith("blocks_layout"):
result["layout"][field] = json_compatible(schema[field].default)
return result


class LayoutGet(Service):
"""Get layout information"""

def reply(self):
info = Layout(self.context, self.request)
return info(expand=True)["layout"]
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/collection.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/collection/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/collection/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/collection/@navigation"
},
Expand Down
12 changes: 12 additions & 0 deletions src/plone/restapi/tests/http-examples/collection_fullobjects.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/collection/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/collection/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/collection/@navigation"
},
Expand Down Expand Up @@ -62,6 +65,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/front-page/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/front-page/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/front-page/@navigation"
},
Expand Down Expand Up @@ -126,6 +132,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/doc1/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/doc1/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/doc1/@navigation"
},
Expand Down Expand Up @@ -191,6 +200,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/doc2/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/doc2/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/doc2/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/content_get.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/folder/my-document/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/folder/my-document/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/folder/my-document/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/content_get_folder.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/folder/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/folder/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/folder/@navigation"
},
Expand Down
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/folder/my-document/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/folder/my-document/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/folder/my-document/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/content_post.resp
Expand Up @@ -10,6 +10,9 @@ Location: http://localhost:55001/plone/folder/my-document
"breadcrumbs": {
"@id": "http://localhost:55001/plone/folder/my-document/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/folder/my-document/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/folder/my-document/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/document.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/front-page/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/front-page/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/front-page/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/event.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/event/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/event/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/event/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/expansion.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/front-page/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/front-page/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/front-page/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/expansion_expanded.resp
Expand Up @@ -15,6 +15,9 @@ Content-Type: application/json
}
]
},
"layout": {
"@id": "http://localhost:55001/plone/front-page/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/front-page/@navigation"
},
Expand Down
Expand Up @@ -115,6 +115,9 @@ Content-Type: application/json
}
]
},
"layout": {
"@id": "http://localhost:55001/plone/front-page/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/front-page/@navigation",
"items": [
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/file.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/file/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/file/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/file/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/folder.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/folder/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/folder/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/folder/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/image.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/image/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/image/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/image/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/jwt_logged_in.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/@navigation"
}
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/link.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/link/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/link/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/link/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/newsitem.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/newsitem/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/newsitem/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/newsitem/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/search_fullobjects.resp
Expand Up @@ -12,6 +12,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/doc1/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/doc1/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/doc1/@navigation"
},
Expand Down
3 changes: 3 additions & 0 deletions src/plone/restapi/tests/http-examples/siteroot.resp
Expand Up @@ -9,6 +9,9 @@ Content-Type: application/json
"breadcrumbs": {
"@id": "http://localhost:55001/plone/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/@navigation"
}
Expand Down
Expand Up @@ -10,6 +10,9 @@ Location: http://localhost:55001/plone/de/mydocument
"breadcrumbs": {
"@id": "http://localhost:55001/plone/de/mydocument/@breadcrumbs"
},
"layout": {
"@id": "http://localhost:55001/plone/de/mydocument/@layout"
},
"navigation": {
"@id": "http://localhost:55001/plone/de/mydocument/@navigation"
},
Expand Down

0 comments on commit 88fa2a6

Please sign in to comment.