src/pyams_content/shared/view/theme.py
changeset 829 f933926ed0a1
parent 800 2b5460ecb254
child 1090 d6d041577ae0
--- a/src/pyams_content/shared/view/theme.py	Tue Jul 10 16:59:55 2018 +0200
+++ b/src/pyams_content/shared/view/theme.py	Wed Jul 11 10:18:10 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_content.component.theme import ICollectionsInfo
 
 __docformat__ = 'restructuredtext'
 
@@ -19,7 +20,8 @@
 from hypatia.interfaces import ICatalog
 from pyams_content.component.theme.interfaces import ITagsInfo, IThemesInfo
 from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, \
-    IViewQueryParamsExtension, VIEW_THEMES_SETTINGS_KEY, IViewTagsSettings, VIEW_TAGS_SETTINGS_KEY
+    IViewQueryParamsExtension, VIEW_THEMES_SETTINGS_KEY, IViewTagsSettings, VIEW_TAGS_SETTINGS_KEY, \
+    IViewCollectionsSettings, VIEW_COLLECTIONS_SETTINGS_KEY
 from zope.intid.interfaces import IIntIds
 
 # import packages
@@ -141,3 +143,58 @@
         if themes:
             params = and_(params, Any(catalog['themes'], themes))
         return params
+
+
+#
+# Collections management
+#
+
+@implementer(IViewCollectionsSettings)
+class ViewCollectionsSettings(Persistent, Contained):
+    """View collections settings"""
+
+    select_context_collections = FieldProperty(IViewCollectionsSettings['select_context_collections'])
+    collections = FieldProperty(IViewCollectionsSettings['collections'])
+
+    @property
+    def is_using_context(self):
+        return self.select_context_collections
+
+    def get_collections(self, context):
+        collections = set()
+        if self.select_context_collections:
+            collections_info = ICollectionsInfo(context, None)
+            if collections_info is not None:
+                collections |= set(collections_info.collections or ())
+        if self.collections:
+            collections |= set(self.collections)
+        return collections
+
+    def get_collections_index(self, context):
+        intids = get_utility(IIntIds)
+        return [intids.register(term) for term in self.get_collections(context)]
+
+
+@adapter_config(context=IWfView, provides=IViewCollectionsSettings)
+@adapter_config(name='collections', context=IWfView, provides=IViewSettings)
+def view_collections_settings_factory(view):
+    """View collections settings factory"""
+    return get_annotation_adapter(view, VIEW_COLLECTIONS_SETTINGS_KEY, ViewCollectionsSettings,
+                                  name='++view:collections++')
+
+
+@adapter_config(name='collections', context=IWfView, provides=IViewQueryParamsExtension)
+class ViewCollectionsQueryParamsExtension(ContextAdapter):
+    """View collections query params extension"""
+
+    weight = 54
+
+    def get_params(self, context):
+        catalog = get_utility(ICatalog)
+        settings = IViewCollectionsSettings(self.context)
+        params = None
+        # check collections
+        collections = settings.get_collections_index(context)
+        if collections:
+            params = and_(params, Any(catalog['collections'], collections))
+        return params