Updated content provider rendering API
authorThierry Florac <tflorac@ulthar.net>
Wed, 25 Aug 2021 12:22:20 +0200
changeset 293 e606ea8100d5
parent 292 dacbe2096da4
child 294 1839654be6db
Updated content provider rendering API
src/pyams_portal/portlet.py
src/pyams_portal/skin/__init__.py
src/pyams_portal/views.py
--- a/src/pyams_portal/portlet.py	Fri Aug 06 16:35:44 2021 +0200
+++ b/src/pyams_portal/portlet.py	Wed Aug 25 12:22:20 2021 +0200
@@ -142,12 +142,12 @@
                                                      name=settings.configuration.portlet_name)
         self.settings = settings
 
-    def render(self):
+    def render(self, template_name=''):
         if self.portlet is None:
             return ''
         if self.portlet.permission and not self.request.has_permission(self.portlet.permission):
             return ''
-        return super(PortletContentProvider, self).render()
+        return super().render(template_name)
 
 
 PREVIEW_PREFIX = '''<div class="padding-x-5 text-info">
@@ -170,16 +170,15 @@
         slot_id, slot_name = config.get_portlet_slot(self.settings.configuration.portlet_id)
         return config.get_slot_configuration(slot_name)
 
-    def render(self):
+    def render(self, template_name=''):
         if self.settings.renderer == 'hidden':
             return render('templates/portlet-hidden.pt', {}, request=self.request)
-        else:
-            result = super(PortletPreviewer, self).render()
-            renderer = self.settings.get_renderer()
-            translate = self.request.localizer.translate
-            result = PREVIEW_PREFIX.format(label=translate(_("Renderer:")),
-                                           renderer=translate(renderer.label)) + result
-            return result
+        result = super(PortletPreviewer, self).render()
+        renderer = self.settings.get_renderer()
+        translate = self.request.localizer.translate
+        result = PREVIEW_PREFIX.format(label=translate(_("Renderer:")),
+                                       renderer=translate(renderer.label)) + result
+        return result
 
 
 PORTLETS_CACHE_REGION = 'portlets'
@@ -226,33 +225,33 @@
                                              hostname=self.request.host,
                                              context=ICacheKeyValue(self.context),
                                              lang=self.request.locale_name)
-        else:
-            return PORTLETS_CACHE_DISPLAY_CONTEXT_KEY.format(portlet=ICacheKeyValue(self.settings),
-                                                             hostname=self.request.host,
-                                                             context=ICacheKeyValue(self.context),
-                                                             display=ICacheKeyValue(
-                                                                 display_context),
-                                                             lang=self.request.locale_name)
+        return PORTLETS_CACHE_DISPLAY_CONTEXT_KEY.format(portlet=ICacheKeyValue(self.settings),
+                                                         hostname=self.request.host,
+                                                         context=ICacheKeyValue(self.context),
+                                                         display=ICacheKeyValue(
+                                                             display_context),
+                                                         lang=self.request.locale_name)
 
-    def render(self):
+    def render(self, template_name=''):
         preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
         if preview_mode or not self.use_portlets_cache:
-            return super(PortletRenderer, self).render()
-        else:
-            portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
-            cache_key = self.get_cache_key()
-            if self.use_authentication:
-                cache_key = '{0}::{1}'.format(cache_key, self.request.principal.id)
-            # load rendered content from cache, or create output and store it in cache
-            try:
-                result = portlets_cache.get_value(cache_key)
-                logger.debug("Retrieving portlet content from cache key {0}".format(cache_key))
-            except KeyError:
-                self.update()
-                result = super(PortletRenderer, self).render()
-                portlets_cache.set_value(cache_key, result)
-                logger.debug("Storing portlet content to cache key {0}".format(cache_key))
-            return result
+            return super().render(template_name)
+        portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
+        cache_key = self.get_cache_key()
+        if template_name:
+            cache_key = '{}::{}'.format(cache_key, template_name)
+        if self.use_authentication:
+            cache_key = '{}::{}'.format(cache_key, self.request.principal.id)
+        # load rendered content from cache, or create output and store it in cache
+        try:
+            result = portlets_cache.get_value(cache_key)
+            logger.debug("Retrieving portlet content from cache key {0}".format(cache_key))
+        except KeyError:
+            self.update()
+            result = super().render(template_name)
+            portlets_cache.set_value(cache_key, result)
+            logger.debug("Storing portlet content to cache key {0}".format(cache_key))
+        return result
 
 
 @vocabulary_config(name='PyAMS portlet renderers')
@@ -262,11 +261,13 @@
     def __init__(self, context):
         request = check_request()
         translate = request.localizer.translate
-        terms = [SimpleTerm(name, title=translate(adapter.label))
-                 for name, adapter in sorted(request.registry.getAdapters((request.root, request,
+        terms = [
+            SimpleTerm(name, title=translate(adapter.label))
+            for name, adapter in sorted(request.registry.getAdapters((request.root, request,
                                                                            request, context),
                                                                           IPortletRenderer),
-                                             key=get_adapter_weight)]
+                                        key=get_adapter_weight)
+        ]
         super(PortletRenderersVocabulary, self).__init__(terms)
 
 
--- a/src/pyams_portal/skin/__init__.py	Fri Aug 06 16:35:44 2021 +0200
+++ b/src/pyams_portal/skin/__init__.py	Wed Aug 25 12:22:20 2021 +0200
@@ -36,5 +36,5 @@
     label = _("Hidden portlet")
     weight = 9999
 
-    def render(self):
+    def render(self, template_name=''):
         return ''
--- a/src/pyams_portal/views.py	Fri Aug 06 16:35:44 2021 +0200
+++ b/src/pyams_portal/views.py	Wed Aug 25 12:22:20 2021 +0200
@@ -13,17 +13,13 @@
 __docformat__ = 'restructuredtext'
 
 
-# import standard library
-
 from pyramid.decorator import reify
 from pyramid.exceptions import NotFound
 
-# import packages
 from pyams_default_theme.page import BaseIndexPage
 from pyams_pagelet.pagelet import pagelet_config
-# import interfaces
-from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration, \
-    IPortalPortletsConfiguration, IPortletRenderer, IPortlet, PREVIEW_MODE, IPortletCSSClass
+from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalPortletsConfiguration, \
+    IPortalTemplateConfiguration, IPortlet, IPortletCSSClass, IPortletRenderer, PREVIEW_MODE
 from pyams_skin.layer import IPyAMSUserLayer
 from pyams_template.template import template_config
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
@@ -73,12 +69,11 @@
         if portlet is not None:
             return self.request.registry.queryMultiAdapter((portlet, self.request), IPortletCSSClass, default='')
 
-    def render_portlet(self, portlet_id):
+    def render_portlet(self, portlet_id, template_name=''):
         renderer = self.portlets.get(portlet_id)
         if renderer is not None:
-            return renderer.render()
-        else:
-            return ''
+            return renderer.render(template_name)
+        return ''
 
 
 @pagelet_config(name='', context=IPortalContext, layer=IPyAMSUserLayer)