src/pyams_skin/configuration.py
changeset 0 bb4aabe07487
child 16 bc06fca47fc7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/configuration.py	Thu Feb 19 10:59:00 2015 +0100
@@ -0,0 +1,126 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import pkg_resources
+
+# import interfaces
+from pyams_skin.interfaces.configuration import IStaticConfiguration, IConfiguration, SKIN_CONFIGURATION_KEY
+from pyams_utils.interfaces.site import IConfigurationManager, IConfigurationFactory
+from pyams_utils.interfaces.tales import ITALESExtension
+from zope.annotation.interfaces import IAnnotations
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from persistent import Persistent
+from pyams_utils.adapter import ContextRequestViewAdapter, ContextAdapter, adapter_config
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from zope.container.contained import Contained
+from zope.interface import implementer, Interface
+from zope.location.location import locate
+from zope.schema.fieldproperty import FieldProperty
+
+from pyams_skin import _
+
+
+@adapter_config(context=(Interface, Interface, Interface), provides=IStaticConfiguration)
+class StaticConfiguration(object):
+    """Default static configuration"""
+
+    application_package = 'pyams_skin'
+    application_name = 'PyAMS'
+
+    version_location = 'menus'
+
+    include_top_links = True
+    include_site_search = True
+    site_search_placeholder = _("Search...")
+    site_search_handler = '#search.html'
+    include_mobile_search = True
+    mobile_search_placeholder = _("Search...")
+    mobile_search_handler = '#search.html'
+    include_user_activity = True
+    include_user_shortcuts = True
+    include_logout_button = True
+    include_minify_button = True
+    include_flags = False
+    include_menus = True
+    include_ribbon = True
+    include_reload_button = True
+    body_css_class = False
+
+    def __init__(self, context, request, view):
+        self.context = context
+        self.request = request
+        self.view = view
+
+    @property
+    def version(self):
+        return pkg_resources.get_distribution(self.application_package).version
+
+
+@adapter_config(name='static_configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class StaticConfigurationTalesExtension(ContextRequestViewAdapter):
+    """extension:static_configuration TALES expression"""
+
+    def render(self, context):
+        if context is None:
+            context = self.context
+        registry = self.request.registry
+        return registry.queryMultiAdapter((context, self.request, self.view), IStaticConfiguration)
+
+
+@implementer(IConfiguration)
+class Configuration(Persistent, Contained):
+    """Manageable configuration"""
+
+    title = FieldProperty(IConfiguration['title'])
+    description = FieldProperty(IConfiguration['description'])
+    author = FieldProperty(IConfiguration['author'])
+
+
+@adapter_config(context=IConfigurationManager, provides=IConfiguration)
+def ConfigurationFactory(context):
+    """Configuration factory"""
+    annotations = IAnnotations(context)
+    configuration = annotations.get(SKIN_CONFIGURATION_KEY)
+    if configuration is None:
+        registry = check_request().registry
+        factory = registry.queryAdapter(context, IConfigurationFactory)
+        if factory is not None:
+            configuration = annotations[SKIN_CONFIGURATION_KEY] = factory()
+            locate(configuration, context, '++configuration++')
+    return configuration
+
+
+@adapter_config(name='configuration', context=(Interface, Interface, Interface), provides=ITALESExtension)
+class ConfigurationTalesExtension(ContextRequestViewAdapter):
+    """extension:configuration TALES expression"""
+
+    def render(self, context):
+        if context is None:
+            context = self.context
+        manager = get_parent(context, IConfigurationManager)
+        if manager is not None:
+            return IConfiguration(manager)
+
+
+@adapter_config(name='configuration', context=IConfigurationManager, provides=ITraversable)
+class ConfigurationTraverser(ContextAdapter):
+    """++configuration++ namespace traverser"""
+
+    def traverse(self, name, furtherpath=None):
+        return IConfiguration(self.context)