Added "get_cache_key" method in base portlets renderer
authorThierry Florac <tflorac@ulthar.net>
Fri, 26 Jul 2019 12:50:02 +0200
changeset 263 87994557fc51
parent 262 e872b89f74b8
child 264 492c2e287d5b
Added "get_cache_key" method in base portlets renderer
src/pyams_portal/interfaces.py
src/pyams_portal/portlet.py
--- a/src/pyams_portal/interfaces.py	Wed Mar 27 09:04:28 2019 +0100
+++ b/src/pyams_portal/interfaces.py	Fri Jul 26 12:50:02 2019 +0200
@@ -156,15 +156,22 @@
     """
 
     label = Attribute("Renderer name")
+    weight = Attribute("Renderer weight used for sorting")
 
     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")
 
+    slot_configuration = Attribute("Template page slot configuration")
+    renderer_settings = Attribute("Renderer settings")
+
     use_portlets_cache = Attribute("Can renderer use rendering cache?")
     use_authentication = Attribute("If 'True', portlet cache entry key is based on current authentication")
 
+    def get_cache_key(self):
+        """Defines key used to get/set portlet contents in cache"""
+
     resources = Attribute("Tuple of Fanstatic resources needed by this renderer")
 
 
--- a/src/pyams_portal/portlet.py	Wed Mar 27 09:04:28 2019 +0100
+++ b/src/pyams_portal/portlet.py	Fri Jul 26 12:50:02 2019 +0200
@@ -10,8 +10,6 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
 import logging
 logger = logging.getLogger('PyAMS (portal)')
 
@@ -44,6 +42,8 @@
 from pyams_utils.vocabulary import vocabulary_config
 from pyams_viewlet.viewlet import ViewContentProvider
 
+__docformat__ = 'restructuredtext'
+
 
 #
 # Portlets utilities
@@ -199,25 +199,27 @@
     def use_portlets_cache(self):
         return not bool(self.request.params)
 
+    def get_cache_key(self):
+        display_context = self.request.annotations.get(DISPLAY_CONTEXT)
+        if display_context is None:
+            return PORTLETS_CACHE_KEY.format(portlet=ICacheKeyValue(self.settings),
+                                             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)
+
     def render(self):
         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)
-            # check for display context
-            display_context = self.request.annotations.get(DISPLAY_CONTEXT)
-            if display_context is None:
-                cache_key = PORTLETS_CACHE_KEY.format(portlet=ICacheKeyValue(self.settings),
-                                                      hostname=self.request.host,
-                                                      context=ICacheKeyValue(self.context),
-                                                      lang=self.request.locale_name)
-            else:
-                cache_key = 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)
+            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