Added IParagraphContainerPortletRenderer interface to create custom paragraphs container portlet renderers
authorThierry Florac <tflorac@ulthar.net>
Fri, 26 Jul 2019 13:08:32 +0200
changeset 450 3dd8901d34cf
parent 449 5bb4ee333834
child 451 0c41d132ccb7
Added IParagraphContainerPortletRenderer interface to create custom paragraphs container portlet renderers
src/pyams_default_theme/component/paragraph/interfaces/__init__.py
src/pyams_default_theme/component/paragraph/portlet/__init__.py
--- a/src/pyams_default_theme/component/paragraph/interfaces/__init__.py	Wed Jun 26 11:15:56 2019 +0200
+++ b/src/pyams_default_theme/component/paragraph/interfaces/__init__.py	Fri Jul 26 13:08:32 2019 +0200
@@ -10,10 +10,20 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
+from zope.interface import Attribute
+
+from pyams_portal.interfaces import IPortletContentProvider
+
+
 __docformat__ = 'restructuredtext'
 
-# import standard library
+
+class IParagraphContainerPortletRenderer(IPortletContentProvider):
+    """Custom paragraph container portlet renderer
 
-# import interfaces
+    This interface is used to create custom paragraphs container portlet renderers, which may be
+    registered with a view name.
 
-# import packages
+    """
+
+    use_portlets_cache = Attribute("Use portlets cache?")
--- a/src/pyams_default_theme/component/paragraph/portlet/__init__.py	Wed Jun 26 11:15:56 2019 +0200
+++ b/src/pyams_default_theme/component/paragraph/portlet/__init__.py	Fri Jul 26 13:08:32 2019 +0200
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
 from zope.interface import Interface
 
 from pyams_content.component.paragraph.interfaces import IParagraphContainer
@@ -19,6 +17,7 @@
     IParagraphNavigationPortletSettings
 from pyams_content.features.renderer.interfaces import ISharedContentRenderer
 from pyams_content.shared.site.interfaces import ISiteContainer
+from pyams_default_theme.component.paragraph.interfaces import IParagraphContainerPortletRenderer
 from pyams_default_theme.interfaces import IContentSummaryInfo
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
 from pyams_portal.portlet import PortletRenderer
@@ -27,6 +26,9 @@
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 
+
+__docformat__ = 'restructuredtext'
+
 from pyams_default_theme import _
 
 
@@ -39,22 +41,42 @@
     label = _("Default paragraphs renderer")
 
     renderers = ()
+    custom_renderer = None
 
     def update(self):
         super(ParagraphContainerPortletRenderer, self).update()
         settings = self.settings
         registry = self.request.registry
-        container = IParagraphContainer(self.context, None)
-        if container is not None:
-            paragraphs = container.get_visible_paragraphs(settings.paragraphs, settings.anchors_only,
-                                                          settings.factories)
-            renderers = [paragraph.get_renderer(self.request) for paragraph in paragraphs]
+        renderer = self.custom_renderer = registry.queryMultiAdapter((self.context, self.request, self.view),
+                                                                     IParagraphContainerPortletRenderer,
+                                                                     name=self.request.view_name)
+        if renderer is not None:
+            renderer.update()
+            self.renderers = [renderer]
         else:
-            renderers = [adapter for name, adapter in sorted(registry.getAdapters((self.context, self.request),
-                                                                                  ISharedContentRenderer),
-                                                             key=lambda x: x[1].weight)]
-        self.renderers = list(filter(lambda x: x is not None, renderers))
-        [renderer.update() for renderer in self.renderers]
+            container = IParagraphContainer(self.context, None)
+            if container is not None:
+                paragraphs = container.get_visible_paragraphs(settings.paragraphs, settings.anchors_only,
+                                                              settings.factories)
+                renderers = [paragraph.get_renderer(self.request) for paragraph in paragraphs]
+            else:
+                renderers = [adapter for name, adapter in sorted(registry.getAdapters((self.context, self.request),
+                                                                                      ISharedContentRenderer),
+                                                                 key=lambda x: x[1].weight)]
+            self.renderers = list(filter(lambda x: x is not None, renderers))
+            [renderer.update() for renderer in self.renderers]
+
+    @property
+    def use_portlets_cache(self):
+        use_cache = super(ParagraphContainerPortletRenderer, self).use_portlets_cache
+        if use_cache and (self.custom_renderer is not None):
+            return self.custom_renderer.use_portlets_cache
+
+    def get_cache_key(self):
+        key = super(ParagraphContainerPortletRenderer, self).get_cache_key()
+        if self.custom_renderer is not None:
+            key = '{}::{}'.format(key, self.request.view_name)
+        return key
 
     def get_navigation_links(self):