src/pyams_content/shared/view/theme.py
changeset 92 3facc843c06f
child 337 9a3e4f9cc8f5
equal deleted inserted replaced
91:87e08c0f3e3c 92:3facc843c06f
       
     1 #
       
     2 # Copyright (c) 2008-2015 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 
       
    18 # import interfaces
       
    19 from pyams_content.component.theme.interfaces import IThemesInfo
       
    20 from pyams_content.shared.view.interfaces import IViewThemesSettings, IWfView, VIEW_THEMES_SETTINGS_KEY
       
    21 from zope.annotation.interfaces import IAnnotations
       
    22 
       
    23 # import packages
       
    24 from persistent import Persistent
       
    25 from pyams_utils.adapter import adapter_config
       
    26 from pyramid.threadlocal import get_current_registry
       
    27 from zope.container.contained import Contained
       
    28 from zope.interface import implementer
       
    29 from zope.lifecycleevent import ObjectCreatedEvent
       
    30 from zope.location import locate
       
    31 from zope.schema.fieldproperty import FieldProperty
       
    32 
       
    33 
       
    34 @implementer(IViewThemesSettings)
       
    35 class ViewThemesSettings(Persistent, Contained):
       
    36     """View themes settings"""
       
    37 
       
    38     select_context_themes = FieldProperty(IViewThemesSettings['select_context_themes'])
       
    39     themes = FieldProperty(IViewThemesSettings['themes'])
       
    40 
       
    41     def get_themes(self, context):
       
    42         themes = set()
       
    43         if self.select_context_themes:
       
    44             themes_info = IThemesInfo(context, None)
       
    45             if themes_info is not None:
       
    46                 themes |= set(themes_info.themes or ())
       
    47         if self.themes:
       
    48             themes |= set(self.themes)
       
    49         return themes
       
    50 
       
    51     def get_themes_index(self, context):
       
    52         return [theme.label for theme in self.get_themes(context)]
       
    53 
       
    54 
       
    55 @adapter_config(context=IWfView, provides=IViewThemesSettings)
       
    56 def ViewThemesSettingsFactory(view):
       
    57     """View themes settings factory"""
       
    58     annotations = IAnnotations(view)
       
    59     settings = annotations.get(VIEW_THEMES_SETTINGS_KEY)
       
    60     if settings is None:
       
    61         settings = annotations[VIEW_THEMES_SETTINGS_KEY] = ViewThemesSettings()
       
    62         get_current_registry().notify(ObjectCreatedEvent(settings))
       
    63         locate(settings, view, '++view:themes++')
       
    64     return settings