Added support for portlet renderer settings
authorThierry Florac <thierry.florac@onf.fr>
Thu, 14 Jun 2018 17:30:40 +0200
changeset 107 563e611a329f
parent 106 1aa90799dde1
child 108 7dd8d5c7e206
Added support for portlet renderer settings
src/pyams_portal/interfaces/__init__.py
src/pyams_portal/portlet.py
--- a/src/pyams_portal/interfaces/__init__.py	Thu Jun 14 17:30:13 2018 +0200
+++ b/src/pyams_portal/interfaces/__init__.py	Thu Jun 14 17:30:40 2018 +0200
@@ -85,6 +85,9 @@
                       required=False,
                       default='')
 
+    def get_renderer(self, request=None):
+        """Get renderer utility"""
+
 
 PORTLETS_CONFIGURATION_KEY = 'pyams_portal.portlets'
 
@@ -147,9 +150,16 @@
 
     label = Attribute("Renderer name")
 
+    settings_interface = Attribute("Settings interface defined for this renderer")
+    settings_key = Attribute("Annotations key used to store renderer settings")
+
     target_interface = Attribute("Target interface provided by this renderer")
 
 
+class IPortletRendererSettings(Interface):
+    """Portlet renderer settings interface"""
+
+
 class IPortalPortletsConfiguration(IContained):
     """Portal template portlet configuration interface"""
 
--- a/src/pyams_portal/portlet.py	Thu Jun 14 17:30:13 2018 +0200
+++ b/src/pyams_portal/portlet.py	Thu Jun 14 17:30:40 2018 +0200
@@ -23,13 +23,13 @@
 from pyams_form.interfaces.form import IFormContextPermissionChecker
 from pyams_portal.interfaces import IPortlet, IPortletSettings, IPortletConfiguration, IPortletPreviewer, \
     IPortletRenderer, IPortalPortletsConfiguration, IPortalTemplate, IPortalContext, IPortalPage, \
-    IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION
+    IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION, IPortletRendererSettings
 from zope.traversing.interfaces import ITraversable
 
 # import packages
 from persistent import Persistent
 from persistent.mapping import PersistentMapping
-from pyams_utils.adapter import adapter_config, ContextAdapter, get_adapter_weight
+from pyams_utils.adapter import adapter_config, ContextAdapter, get_adapter_weight, get_annotation_adapter
 from pyams_utils.request import check_request
 from pyams_utils.vocabulary import vocabulary_config
 from pyams_viewlet.viewlet import ViewContentProvider
@@ -167,7 +167,11 @@
 class PortletRenderer(PortletContentProvider):
     """Portlet renderer adapter"""
 
+    settings_interface = None
+    settings_key = None
+
     target_interface = None
+
     weight = 0
 
     @property
@@ -236,11 +240,25 @@
         if (renderer is not None) and (renderer.target_interface is not None):
             alsoProvides(self, renderer.target_interface)
 
+    def get_renderer(self, request=None):
+        if request is None:
+            request = check_request()
+        return request.registry.queryMultiAdapter((request.root, request, request, self), IPortletRenderer,
+                                                  name=self._renderer)
+
     @property
     def configuration(self):
         return self.__parent__
 
 
+@adapter_config(context=IPortletSettings, provides=IPortletRendererSettings)
+def portlet_renderer_settings_adapter(context):
+    renderer = context.get_renderer()
+    if not renderer.settings_interface:
+        return None
+    return get_annotation_adapter(context, renderer.settings_key, renderer.settings_interface)
+
+
 @implementer(IPortletConfiguration)
 class PortletConfiguration(Persistent, Contained):
     """Portlet configuration persistent class