src/pyams_portal/portlet.py
changeset 133 7ad82c70e528
parent 126 862e9f6a3cf8
child 135 3bca72c8574d
equal deleted inserted replaced
132:75822b1c3da3 133:7ad82c70e528
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
       
    12 from pyams_portal.views import PREVIEW_MODE
    12 
    13 
    13 __docformat__ = 'restructuredtext'
    14 __docformat__ = 'restructuredtext'
    14 
    15 
    15 
    16 
    16 # import standard library
    17 # import standard library
    22 # import interfaces
    23 # import interfaces
    23 from pyams_form.interfaces.form import IFormContextPermissionChecker
    24 from pyams_form.interfaces.form import IFormContextPermissionChecker
    24 from pyams_portal.interfaces import IPortlet, IPortletSettings, IPortletConfiguration, IPortletPreviewer, \
    25 from pyams_portal.interfaces import IPortlet, IPortletSettings, IPortletConfiguration, IPortletPreviewer, \
    25     IPortletRenderer, IPortalPortletsConfiguration, IPortalTemplate, IPortalContext, IPortalPage, \
    26     IPortletRenderer, IPortalPortletsConfiguration, IPortalTemplate, IPortalContext, IPortalPage, \
    26     IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION, IPortletRendererSettings, PORTLET_RENDERER_SETTINGS_KEY
    27     IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION, IPortletRendererSettings, PORTLET_RENDERER_SETTINGS_KEY
       
    28 from pyams_utils.interfaces import ICacheKeyValue
    27 from zope.traversing.interfaces import ITraversable
    29 from zope.traversing.interfaces import ITraversable
    28 
    30 
    29 # import packages
    31 # import packages
    30 from persistent import Persistent
    32 from persistent import Persistent
    31 from persistent.mapping import PersistentMapping
    33 from persistent.mapping import PersistentMapping
       
    34 from pyams_cache.beaker import get_cache
    32 from pyams_utils.adapter import adapter_config, ContextAdapter, get_adapter_weight, get_annotation_adapter
    35 from pyams_utils.adapter import adapter_config, ContextAdapter, get_adapter_weight, get_annotation_adapter
    33 from pyams_utils.factory import get_object_factory, is_interface
    36 from pyams_utils.factory import get_object_factory, is_interface
    34 from pyams_utils.request import check_request
    37 from pyams_utils.request import check_request
    35 from pyams_utils.vocabulary import vocabulary_config
    38 from pyams_utils.vocabulary import vocabulary_config
    36 from pyams_viewlet.viewlet import ViewContentProvider
    39 from pyams_viewlet.viewlet import ViewContentProvider
   132     def __init__(self, context, request, view, settings):
   135     def __init__(self, context, request, view, settings):
   133         super(PortletContentProvider, self).__init__(context, request, view)
   136         super(PortletContentProvider, self).__init__(context, request, view)
   134         self.portlet = request.registry.queryUtility(IPortlet, name=settings.configuration.portlet_name)
   137         self.portlet = request.registry.queryUtility(IPortlet, name=settings.configuration.portlet_name)
   135         self.settings = settings
   138         self.settings = settings
   136 
   139 
   137     def __call__(self):
   140     def render(self):
   138         if self.portlet is None:
   141         if self.portlet is None:
   139             return ''
   142             return ''
   140         if self.portlet.permission and not self.request.has_permission(self.portlet.permission):
   143         if self.portlet.permission and not self.request.has_permission(self.portlet.permission):
   141             return ''
   144             return ''
   142         self.update()
   145         return super(PortletContentProvider, self).render()
   143         return self.render()
       
   144 
   146 
   145 
   147 
   146 @implementer(IPortletPreviewer)
   148 @implementer(IPortletPreviewer)
   147 class PortletPreviewer(PortletContentProvider):
   149 class PortletPreviewer(PortletContentProvider):
   148     """Portlet previewer adapter"""
   150     """Portlet previewer adapter"""
   162             return render('templates/portlet-hidden.pt', {}, request=self.request)
   164             return render('templates/portlet-hidden.pt', {}, request=self.request)
   163         else:
   165         else:
   164             return super(PortletPreviewer, self).render()
   166             return super(PortletPreviewer, self).render()
   165 
   167 
   166 
   168 
       
   169 PORTLETS_CACHE_KEY = 'portlet::{portlet}::{context}'
       
   170 PORTLETS_CACHE_REGION = 'portlets'
       
   171 PORTLETS_CACHE_NAME = 'PyAMS::portlet'
       
   172 
       
   173 
   167 @implementer(IPortletRenderer)
   174 @implementer(IPortletRenderer)
   168 class PortletRenderer(PortletContentProvider):
   175 class PortletRenderer(PortletContentProvider):
   169     """Portlet renderer adapter"""
   176     """Portlet renderer adapter"""
   170 
   177 
   171     settings_interface = None
   178     settings_interface = None
   186         return config.get_slot_configuration(slot_name)
   193         return config.get_slot_configuration(slot_name)
   187 
   194 
   188     @property
   195     @property
   189     def renderer_settings(self):
   196     def renderer_settings(self):
   190         return IPortletRendererSettings(self.settings)
   197         return IPortletRendererSettings(self.settings)
       
   198 
       
   199     def render(self):
       
   200         preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
       
   201         if preview_mode:
       
   202             return super(PortletRenderer, self).render()
       
   203         else:
       
   204             portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
       
   205             cache_key = PORTLETS_CACHE_KEY.format(portlet=ICacheKeyValue(self.settings),
       
   206                                                   context=ICacheKeyValue(self.context))
       
   207             try:
       
   208                 result = portlets_cache.get_value(cache_key)
       
   209                 logger.debug("Retrieving portlet content for cache key {0}".format(cache_key))
       
   210             except KeyError:
       
   211                 self.update()
       
   212                 result = super(PortletRenderer, self).render()
       
   213                 portlets_cache.set_value(cache_key, result)
       
   214                 logger.debug("Storing portlet content for cache key {0}".format(cache_key))
       
   215             return result
   191 
   216 
   192 
   217 
   193 @vocabulary_config(name='PyAMS portlet renderers')
   218 @vocabulary_config(name='PyAMS portlet renderers')
   194 class PortletRenderersVocabulary(SimpleVocabulary):
   219 class PortletRenderersVocabulary(SimpleVocabulary):
   195     """Portlet renderers vocabulary"""
   220     """Portlet renderers vocabulary"""