Updated view to update all portlets before starting rendering
authorThierry Florac <thierry.florac@onf.fr>
Thu, 21 Jun 2018 16:36:07 +0200
changeset 131 ed2ff7b44c73
parent 130 26038059e74d
child 132 75822b1c3da3
Updated view to update all portlets before starting rendering
src/pyams_portal/views.py
--- a/src/pyams_portal/views.py	Thu Jun 21 16:35:18 2018 +0200
+++ b/src/pyams_portal/views.py	Thu Jun 21 16:36:07 2018 +0200
@@ -30,16 +30,27 @@
 from pyramid.exceptions import NotFound
 
 
-@pagelet_config(name='', context=IPortalContext, layer=IPyAMSUserLayer)
-@template_config(template='templates/pagelet.pt', layer=IPyAMSUserLayer)
-class PortalContextIndexPage(BaseIndexPage):
-    """Portal context index page"""
+class BasePortalContextIndexPage(BaseIndexPage):
+    """Base portal context index page"""
+
+    portlets = None
 
     def update(self):
-        wf_info = IWorkflowPublicationInfo(self.context, None)
-        if (wf_info is not None) and not wf_info.is_visible(self.request):
-            raise NotFound()
-        super(PortalContextIndexPage, self).update()
+        super(BasePortalContextIndexPage, self).update()
+        # extract all renderers list
+        self.portlets = {}
+        template_configuration = self.template_configuration
+        portlets_configuration = self.portlet_configuration
+        for row_id in range(template_configuration.rows):
+            for slot_name in template_configuration.get_slots(row_id):
+                for portlet_id in template_configuration.slot_config[slot_name].portlet_ids:
+                    settings = portlets_configuration.get_portlet_configuration(portlet_id).settings
+                    renderer = self.request.registry.queryMultiAdapter((self.context, self.request, self, settings),
+                                                                       IPortletRenderer,
+                                                                       name=settings.renderer)
+                    if renderer is not None:
+                        renderer.update()
+                        self.portlets[portlet_id] = renderer
 
     @reify
     def page(self):
@@ -57,16 +68,28 @@
         return self.request.registry.getUtility(IPortlet, name=name)
 
     def render_portlet(self, portlet_id):
-        settings = self.portlet_configuration.get_portlet_configuration(portlet_id).settings
-        renderer = self.request.registry.queryMultiAdapter((self.context, self.request, self, settings),
-                                                           IPortletRenderer,
-                                                           name=settings.renderer)
+        renderer = self.portlets.get(portlet_id)
         if renderer is not None:
-            return renderer()
+            return renderer.render()
         else:
             return ''
 
 
+@pagelet_config(name='', context=IPortalContext, layer=IPyAMSUserLayer)
+@template_config(template='templates/pagelet.pt', layer=IPyAMSUserLayer)
+class PortalContextIndexPage(BasePortalContextIndexPage):
+    """Portal context index page"""
+
+    def update(self):
+        wf_info = IWorkflowPublicationInfo(self.context, None)
+        if (wf_info is not None) and not wf_info.is_visible(self.request):
+            raise NotFound()
+        super(PortalContextIndexPage, self).update()
+
+
+PREVIEW_MODE = 'PREVIEW_MODE'
+
+
 @pagelet_config(name='preview.html', context=IPortalContext, layer=IPyAMSUserLayer,
                 permission=VIEW_SYSTEM_PERMISSION)
 class PortalContextPreviewPage(PortalContextIndexPage):
@@ -74,4 +97,5 @@
 
     def update(self):
         # Bypass publication status in preview
+        self.request.annotations[PREVIEW_MODE] = True
         super(PortalContextIndexPage, self).update()