src/pyams_skin/configuration.py
changeset 0 bb4aabe07487
child 16 bc06fca47fc7
equal deleted inserted replaced
-1:000000000000 0:bb4aabe07487
       
     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 import pkg_resources
       
    18 
       
    19 # import interfaces
       
    20 from pyams_skin.interfaces.configuration import IStaticConfiguration, IConfiguration, SKIN_CONFIGURATION_KEY
       
    21 from pyams_utils.interfaces.site import IConfigurationManager, IConfigurationFactory
       
    22 from pyams_utils.interfaces.tales import ITALESExtension
       
    23 from zope.annotation.interfaces import IAnnotations
       
    24 from zope.traversing.interfaces import ITraversable
       
    25 
       
    26 # import packages
       
    27 from persistent import Persistent
       
    28 from pyams_utils.adapter import ContextRequestViewAdapter, ContextAdapter, adapter_config
       
    29 from pyams_utils.request import check_request
       
    30 from pyams_utils.traversing import get_parent
       
    31 from zope.container.contained import Contained
       
    32 from zope.interface import implementer, Interface
       
    33 from zope.location.location import locate
       
    34 from zope.schema.fieldproperty import FieldProperty
       
    35 
       
    36 from pyams_skin import _
       
    37 
       
    38 
       
    39 @adapter_config(context=(Interface, Interface, Interface), provides=IStaticConfiguration)
       
    40 class StaticConfiguration(object):
       
    41     """Default static configuration"""
       
    42 
       
    43     application_package = 'pyams_skin'
       
    44     application_name = 'PyAMS'
       
    45 
       
    46     version_location = 'menus'
       
    47 
       
    48     include_top_links = True
       
    49     include_site_search = True
       
    50     site_search_placeholder = _("Search...")
       
    51     site_search_handler = '#search.html'
       
    52     include_mobile_search = True
       
    53     mobile_search_placeholder = _("Search...")
       
    54     mobile_search_handler = '#search.html'
       
    55     include_user_activity = True
       
    56     include_user_shortcuts = True
       
    57     include_logout_button = True
       
    58     include_minify_button = True
       
    59     include_flags = False
       
    60     include_menus = True
       
    61     include_ribbon = True
       
    62     include_reload_button = True
       
    63     body_css_class = False
       
    64 
       
    65     def __init__(self, context, request, view):
       
    66         self.context = context
       
    67         self.request = request
       
    68         self.view = view
       
    69 
       
    70     @property
       
    71     def version(self):
       
    72         return pkg_resources.get_distribution(self.application_package).version
       
    73 
       
    74 
       
    75 @adapter_config(name='static_configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
       
    76 class StaticConfigurationTalesExtension(ContextRequestViewAdapter):
       
    77     """extension:static_configuration TALES expression"""
       
    78 
       
    79     def render(self, context):
       
    80         if context is None:
       
    81             context = self.context
       
    82         registry = self.request.registry
       
    83         return registry.queryMultiAdapter((context, self.request, self.view), IStaticConfiguration)
       
    84 
       
    85 
       
    86 @implementer(IConfiguration)
       
    87 class Configuration(Persistent, Contained):
       
    88     """Manageable configuration"""
       
    89 
       
    90     title = FieldProperty(IConfiguration['title'])
       
    91     description = FieldProperty(IConfiguration['description'])
       
    92     author = FieldProperty(IConfiguration['author'])
       
    93 
       
    94 
       
    95 @adapter_config(context=IConfigurationManager, provides=IConfiguration)
       
    96 def ConfigurationFactory(context):
       
    97     """Configuration factory"""
       
    98     annotations = IAnnotations(context)
       
    99     configuration = annotations.get(SKIN_CONFIGURATION_KEY)
       
   100     if configuration is None:
       
   101         registry = check_request().registry
       
   102         factory = registry.queryAdapter(context, IConfigurationFactory)
       
   103         if factory is not None:
       
   104             configuration = annotations[SKIN_CONFIGURATION_KEY] = factory()
       
   105             locate(configuration, context, '++configuration++')
       
   106     return configuration
       
   107 
       
   108 
       
   109 @adapter_config(name='configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
       
   110 class ConfigurationTalesExtension(ContextRequestViewAdapter):
       
   111     """extension:configuration TALES expression"""
       
   112 
       
   113     def render(self, context):
       
   114         if context is None:
       
   115             context = self.context
       
   116         manager = get_parent(context, IConfigurationManager)
       
   117         if manager is not None:
       
   118             return IConfiguration(manager)
       
   119 
       
   120 
       
   121 @adapter_config(name='configuration', context=IConfigurationManager, provides=ITraversable)
       
   122 class ConfigurationTraverser(ContextAdapter):
       
   123     """++configuration++ namespace traverser"""
       
   124 
       
   125     def traverse(self, name, furtherpath=None):
       
   126         return IConfiguration(self.context)