Add view query extension for themes
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Jun 2018 12:43:43 +0200
changeset 606 118d32d0ac38
parent 605 d3d50fcbc62c
child 607 5f4b5b3d5d0d
Add view query extension for themes
src/pyams_content/shared/view/theme.py
--- a/src/pyams_content/shared/view/theme.py	Mon Jun 11 11:45:02 2018 +0200
+++ b/src/pyams_content/shared/view/theme.py	Mon Jun 11 12:43:43 2018 +0200
@@ -16,12 +16,18 @@
 # import standard library
 
 # import interfaces
+from hypatia.interfaces import ICatalog
 from pyams_content.component.theme.interfaces import IThemesInfo
-from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, VIEW_THEMES_SETTINGS_KEY
+from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, \
+    IViewQueryParamsExtension, VIEW_THEMES_SETTINGS_KEY
+from zope.intid.interfaces import IIntIds
 
 # import packages
+from hypatia.query import Any
 from persistent import Persistent
-from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from pyams_catalog.query import and_
+from pyams_utils.adapter import adapter_config, get_annotation_adapter, ContextAdapter
+from pyams_utils.registry import get_utility
 from zope.container.contained import Contained
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
@@ -49,7 +55,8 @@
         return themes
 
     def get_themes_index(self, context):
-        return [theme.label for theme in self.get_themes(context)]
+        intids = get_utility(IIntIds)
+        return [intids.register(term) for term in self.get_themes(context)]
 
 
 @adapter_config(context=IWfView, provides=IViewThemesSettings)
@@ -58,3 +65,20 @@
     """View themes settings factory"""
     return get_annotation_adapter(view, VIEW_THEMES_SETTINGS_KEY, ViewThemesSettings,
                                   name='++view:themes++')
+
+
+@adapter_config(name='themes', context=IWfView, provides=IViewQueryParamsExtension)
+class ViewThemesQueryParamsExtension(ContextAdapter):
+    """View themes query params extension"""
+
+    weight = 50
+
+    def get_params(self, context):
+        catalog = get_utility(ICatalog)
+        settings = IViewThemesSettings(self.context)
+        params = None
+        # check themes
+        themes = settings.get_themes_index(context)
+        if themes:
+            params = and_(params, Any(catalog['themes'], themes))
+        return params