Added renderer name in preview if not default
authorThierry Florac <tflorac@ulthar.net>
Wed, 25 Sep 2019 09:46:48 +0200
changeset 266 8a1a1e258f43
parent 265 69d8c550cc2d
child 267 f77dafa1a966
Added renderer name in preview if not default
src/pyams_portal/portlet.py
--- a/src/pyams_portal/portlet.py	Fri Jul 26 19:18:42 2019 +0200
+++ b/src/pyams_portal/portlet.py	Wed Sep 25 09:46:48 2019 +0200
@@ -11,7 +11,6 @@
 #
 
 import logging
-logger = logging.getLogger('PyAMS (portal)')
 
 import venusian
 from persistent import Persistent
@@ -30,11 +29,13 @@
 
 from pyams_cache.beaker import get_cache
 from pyams_form.interfaces.form import IFormContextPermissionChecker
-from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalPortletsConfiguration, IPortalTemplate, \
-    IPortalTemplateConfiguration, IPortlet, IPortletConfiguration, IPortletPreviewer, IPortletRenderer, \
-    IPortletRendererSettings, IPortletSettings, MANAGE_TEMPLATE_PERMISSION, PORTLET_RENDERER_SETTINGS_KEY, PREVIEW_MODE
-from pyams_utils.adapter import ContextAdapter, adapter_config, get_adapter_weight, get_annotation_adapter
-from pyams_utils.factory import get_object_factory, is_interface, factory_config
+from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalPortletsConfiguration, \
+    IPortalTemplate, IPortalTemplateConfiguration, IPortlet, IPortletConfiguration, \
+    IPortletPreviewer, IPortletRenderer, IPortletRendererSettings, IPortletSettings, \
+    MANAGE_TEMPLATE_PERMISSION, PORTLET_RENDERER_SETTINGS_KEY, PREVIEW_MODE
+from pyams_utils.adapter import ContextAdapter, adapter_config, get_adapter_weight, \
+    get_annotation_adapter
+from pyams_utils.factory import factory_config, get_object_factory, is_interface
 from pyams_utils.interfaces import ICacheKeyValue
 from pyams_utils.interfaces.url import DISPLAY_CONTEXT
 from pyams_utils.registry import get_global_registry
@@ -42,8 +43,14 @@
 from pyams_utils.vocabulary import vocabulary_config
 from pyams_viewlet.viewlet import ViewContentProvider
 
+
 __docformat__ = 'restructuredtext'
 
+from pyams_portal import _
+
+
+logger = logging.getLogger('PyAMS (portal)')
+
 
 #
 # Portlets utilities
@@ -90,8 +97,8 @@
                 component = ob
 
             config = context.config.with_package(info.module)
-            logger.debug("Registering portlet {0} named '{1}'".format(str(component) if component else str(factory),
-                                                                      name))
+            logger.debug("Registering portlet {0} named '{1}'".format(
+                str(component) if component else str(factory), name))
             config.registry.registerUtility(component=component, factory=factory,
                                             provided=IPortlet, name=name)
 
@@ -130,7 +137,8 @@
 
     def __init__(self, context, request, view, settings):
         super(PortletContentProvider, self).__init__(context, request, view)
-        self.portlet = request.registry.queryUtility(IPortlet, name=settings.configuration.portlet_name)
+        self.portlet = request.registry.queryUtility(IPortlet,
+                                                     name=settings.configuration.portlet_name)
         self.settings = settings
 
     def render(self):
@@ -141,6 +149,12 @@
         return super(PortletContentProvider, self).render()
 
 
+PREVIEW_PREFIX = '''<div class="padding-x-5 text-info">
+    <span>{label}</span>
+    {renderer}
+</div>'''
+
+
 @implementer(IPortletPreviewer)
 class PortletPreviewer(PortletContentProvider):
     """Portlet previewer adapter"""
@@ -159,7 +173,14 @@
         if self.settings.renderer == 'hidden':
             return render('templates/portlet-hidden.pt', {}, request=self.request)
         else:
-            return super(PortletPreviewer, self).render()
+            result = super(PortletPreviewer, self).render()
+            settings = self.settings
+            if settings.renderer:
+                renderer = 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'
@@ -210,7 +231,8 @@
             return PORTLETS_CACHE_DISPLAY_CONTEXT_KEY.format(portlet=ICacheKeyValue(self.settings),
                                                              hostname=self.request.host,
                                                              context=ICacheKeyValue(self.context),
-                                                             display=ICacheKeyValue(display_context),
+                                                             display=ICacheKeyValue(
+                                                                 display_context),
                                                              lang=self.request.locale_name)
 
     def render(self):
@@ -242,7 +264,8 @@
         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, request, context),
+                 for name, adapter in sorted(request.registry.getAdapters((request.root, request,
+                                                                           request, context),
                                                                           IPortletRenderer),
                                              key=get_adapter_weight)]
         super(PortletRenderersVocabulary, self).__init__(terms)
@@ -285,21 +308,21 @@
         if value == self._renderer:
             return
         request = check_request()
-        renderer = request.registry.queryMultiAdapter((request.root, request, request, self), IPortletRenderer,
-                                                      name=self._renderer)
+        renderer = request.registry.queryMultiAdapter((request.root, request, request, self),
+                                                      IPortletRenderer, name=self._renderer)
         if (renderer is not None) and (renderer.target_interface is not None):
             noLongerProvides(self, renderer.target_interface)
         self._renderer = value
-        renderer = request.registry.queryMultiAdapter((request.root, request, request, self), IPortletRenderer,
-                                                      name=self._renderer)
+        renderer = request.registry.queryMultiAdapter((request.root, request, request, self),
+                                                      IPortletRenderer, name=self._renderer)
         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)
+        return request.registry.queryMultiAdapter((request.root, request, request, self),
+                                                  IPortletRenderer, name=self._renderer)
 
     @property
     def configuration(self):
@@ -385,7 +408,8 @@
             return parent
         while True:
             if IPortalContext.providedBy(parent):
-                configuration = IPortalPortletsConfiguration(parent).get_portlet_configuration(self.portlet_id)
+                configuration = IPortalPortletsConfiguration(parent).get_portlet_configuration(
+                    self.portlet_id)
                 if not configuration.inherit_parent:
                     return parent
                 page = IPortalPage(parent)
@@ -401,7 +425,8 @@
     @property
     def settings(self):
         if self.inherit_parent:
-            return IPortalPortletsConfiguration(self.parent).get_portlet_configuration(self.portlet_id).settings
+            return IPortalPortletsConfiguration(self.parent).get_portlet_configuration(
+                self.portlet_id).settings
         else:
             return self._settings