Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<configure | ||
xmlns="http://namespaces.zope.org/zope" | ||
xmlns:plone="http://namespaces.plone.org/plone"> | ||
|
||
<adapter factory=".get.Portlets" name="portlets"/> | ||
|
||
<plone:service | ||
method="GET" | ||
for="zope.interface.Interface" | ||
factory=".get.PortletsGet" | ||
name="@portlets" | ||
permission="zope2.View" | ||
/> | ||
|
||
</configure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
from plone.app.portlets.interfaces import IPortletTypeInterface | ||
from plone.portlets.interfaces import IPortletAssignmentMapping | ||
from plone.portlets.interfaces import IPortletAssignmentSettings | ||
from plone.portlets.interfaces import IPortletManager | ||
from plone.restapi.interfaces import IExpandableElement | ||
from plone.restapi.services import Service | ||
from zope.component import adapter | ||
from zope.component import getUtilitiesFor | ||
from zope.component import queryMultiAdapter | ||
from zope.component.hooks import getSite | ||
from zope.interface import implementer | ||
from zope.interface import Interface | ||
from zope.interface import providedBy | ||
|
||
|
||
@implementer(IExpandableElement) | ||
@adapter(Interface, Interface) | ||
class Portlets(object): | ||
|
||
def __init__(self, context, request): | ||
self.context = context | ||
self.request = request | ||
self.portal = getSite() | ||
|
||
def __call__(self, expand=False): | ||
result = { | ||
'navigation': { | ||
'@id': '{}/@navigation'.format(self.context.absolute_url()), | ||
}, | ||
} | ||
if not expand: | ||
return result | ||
|
||
portlets_schemata = { | ||
iface: name | ||
for name, iface in getUtilitiesFor(IPortletTypeInterface) | ||
} | ||
items = {} | ||
for manager_name, manager in getUtilitiesFor(IPortletManager): | ||
mapping = queryMultiAdapter((self.context, manager), | ||
IPortletAssignmentMapping) | ||
if mapping is None: | ||
continue | ||
mapping = mapping.__of__(self.context) | ||
for name, assignment in mapping.items(): | ||
type_ = None | ||
schema = None | ||
for schema in providedBy(assignment).flattened(): | ||
type_ = portlets_schemata.get(schema, None) | ||
if type_ is not None: | ||
break | ||
if type_ is None: | ||
continue | ||
assignment = assignment.__of__(mapping) | ||
settings = IPortletAssignmentSettings(assignment) | ||
if manager_name not in items: | ||
items[manager_name] = [] | ||
items[manager_name].append({ | ||
'type': type_, | ||
'visible': settings.get('visible', True), | ||
'assignment': { | ||
name: getattr(assignment, name, None) | ||
for name in schema.names() | ||
}, | ||
}) | ||
result['portlets']['items'] = items | ||
return result | ||
|
||
|
||
class PortletsGet(Service): | ||
|
||
def reply(self): | ||
portlets = Portlets(self.context, self.request) | ||
return portlets(expand=True)['portlets'] |