src/pyams_content/shared/view/theme.py
changeset 92 3facc843c06f
child 337 9a3e4f9cc8f5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/view/theme.py	Tue Jun 27 11:49:01 2017 +0200
@@ -0,0 +1,64 @@
+#
+# 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 interfaces
+from pyams_content.component.theme.interfaces import IThemesInfo
+from pyams_content.shared.view.interfaces import IViewThemesSettings, IWfView, VIEW_THEMES_SETTINGS_KEY
+from zope.annotation.interfaces import IAnnotations
+
+# import packages
+from persistent import Persistent
+from pyams_utils.adapter import adapter_config
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.lifecycleevent import ObjectCreatedEvent
+from zope.location import locate
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IViewThemesSettings)
+class ViewThemesSettings(Persistent, Contained):
+    """View themes settings"""
+
+    select_context_themes = FieldProperty(IViewThemesSettings['select_context_themes'])
+    themes = FieldProperty(IViewThemesSettings['themes'])
+
+    def get_themes(self, context):
+        themes = set()
+        if self.select_context_themes:
+            themes_info = IThemesInfo(context, None)
+            if themes_info is not None:
+                themes |= set(themes_info.themes or ())
+        if self.themes:
+            themes |= set(self.themes)
+        return themes
+
+    def get_themes_index(self, context):
+        return [theme.label for theme in self.get_themes(context)]
+
+
+@adapter_config(context=IWfView, provides=IViewThemesSettings)
+def ViewThemesSettingsFactory(view):
+    """View themes settings factory"""
+    annotations = IAnnotations(view)
+    settings = annotations.get(VIEW_THEMES_SETTINGS_KEY)
+    if settings is None:
+        settings = annotations[VIEW_THEMES_SETTINGS_KEY] = ViewThemesSettings()
+        get_current_registry().notify(ObjectCreatedEvent(settings))
+        locate(settings, view, '++view:themes++')
+    return settings