src/pyams_content/features/header/skin/__init__.py
branchdev-dc
changeset 1086 3d259e1718ef
parent 1079 a5e56749ca3d
parent 1084 6b6a884fa28a
child 1087 978a2b9123b9
equal deleted inserted replaced
1079:a5e56749ca3d 1086:3d259e1718ef
     1 #
       
     2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     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
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 import logging
       
    18 logger = logging.getLogger('PyAMS (content)')
       
    19 
       
    20 # import interfaces
       
    21 from pyams_content.features.header.interfaces import IHeaderTarget, IHeaderRenderer, IHeaderSettings
       
    22 from pyams_content.features.header.skin.interfaces import IHeaderClass
       
    23 from pyams_content.features.renderer.interfaces import HIDDEN_RENDERER_NAME
       
    24 from pyams_portal.interfaces import PREVIEW_MODE
       
    25 from pyams_skin.layer import IPyAMSLayer
       
    26 from pyams_utils.interfaces import ICacheKeyValue
       
    27 from pyams_utils.interfaces.url import DISPLAY_CONTEXT
       
    28 
       
    29 # import packages
       
    30 from pyams_cache.beaker import get_cache
       
    31 from pyams_content.features.renderer.skin import BaseContentRenderer
       
    32 from pyams_portal.portlet import PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME, PORTLETS_CACHE_KEY
       
    33 from pyams_utils.adapter import adapter_config
       
    34 from pyams_utils.traversing import get_parent
       
    35 from pyramid.decorator import reify
       
    36 from zope.interface import implementer
       
    37 
       
    38 from pyams_content import _
       
    39 
       
    40 
       
    41 @implementer(IHeaderRenderer)
       
    42 class BaseHeaderRenderer(BaseContentRenderer):
       
    43     """Base header renderer"""
       
    44 
       
    45     name = None
       
    46     settings_key = None
       
    47 
       
    48     @reify
       
    49     def settings_target(self):
       
    50         context = self.request.annotations.get(DISPLAY_CONTEXT)
       
    51         if context is None:
       
    52             context = self.context
       
    53         return get_parent(context, IHeaderTarget)
       
    54 
       
    55     @reify
       
    56     def settings(self):
       
    57         if self.settings_interface is None:
       
    58             return None
       
    59         target = self.settings_target
       
    60         settings = IHeaderSettings(target)
       
    61         while settings.inherit:
       
    62             settings = IHeaderSettings(settings.parent)
       
    63         return settings.settings
       
    64 
       
    65     @reify
       
    66     def cache_key(self):
       
    67         return PORTLETS_CACHE_KEY.format(portlet=self.name,
       
    68                                          context=ICacheKeyValue(self.settings_target),
       
    69                                          lang=self.request.locale_name)
       
    70 
       
    71     @property
       
    72     def main_header_class(self):
       
    73         request = self.request
       
    74         return request.registry.queryMultiAdapter((request.context, request), IHeaderClass, default='')
       
    75 
       
    76     def render(self):
       
    77         preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
       
    78         if preview_mode:
       
    79             return super(BaseHeaderRenderer, self).render()
       
    80         else:
       
    81             portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
       
    82             cache_key = self.cache_key
       
    83             if self.context is not self.request.context:  # display shared content
       
    84                 cache_key = '{0}::shared'.format(cache_key)
       
    85             try:
       
    86                 result = portlets_cache.get_value(cache_key)
       
    87                 logger.debug("Retrieving header content from cache key {0}".format(cache_key))
       
    88             except KeyError:
       
    89                 result = super(BaseHeaderRenderer, self).render()
       
    90                 portlets_cache.set_value(cache_key, result)
       
    91                 logger.debug("Storing header content to cache key {0}".format(cache_key))
       
    92             return result
       
    93 
       
    94 
       
    95 @adapter_config(name=HIDDEN_RENDERER_NAME, context=(IHeaderTarget, IPyAMSLayer), provides=IHeaderRenderer)
       
    96 class HiddenHeaderRenderer(BaseHeaderRenderer):
       
    97     """Hidden header renderer"""
       
    98 
       
    99     name = HIDDEN_RENDERER_NAME
       
   100     label = _("Hidden header")
       
   101     weight = -999
       
   102 
       
   103     def render(self):
       
   104         return ''