src/pyams_content_es/component/theme.py
changeset 75 a4a2dc04040c
parent 74 b7976cad0ef0
child 109 b9885a695f88
--- a/src/pyams_content_es/component/theme.py	Thu Jul 05 08:54:10 2018 +0200
+++ b/src/pyams_content_es/component/theme.py	Wed Jul 11 15:21:02 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_content.component.theme import ICollectionsTarget, ICollectionsInfo
 
 __docformat__ = 'restructuredtext'
 
@@ -17,7 +18,8 @@
 
 # import interfaces
 from pyams_content.component.theme.interfaces import ITagsTarget, ITagsInfo, IThemesTarget, IThemesInfo
-from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings, IViewTagsSettings
+from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings, \
+    IViewTagsSettings, IViewCollectionsSettings
 from pyams_content_es.interfaces import IDocumentIndexInfo
 from zope.intid.interfaces import IIntIds
 
@@ -28,6 +30,10 @@
 from pyams_utils.registry import get_utility
 
 
+#
+# Tags index
+#
+
 @adapter_config(name='tags', context=ITagsTarget, provides=IDocumentIndexInfo)
 def tags_target_index_info(content):
     """Tags target index info"""
@@ -51,6 +57,10 @@
             return Q('terms', **{'tags': tags})
 
 
+#
+# Themes index
+#
+
 @adapter_config(name='themes', context=IThemesTarget, provides=IDocumentIndexInfo)
 def themes_target_index_info(content):
     """Themes target index info"""
@@ -87,3 +97,30 @@
         themes = settings.get_themes_index(context)
         if themes:
             return Q('terms', **{'themes.terms': themes})
+
+
+#
+# Collections index
+#
+
+@adapter_config(name='collections', context=ICollectionsTarget, provides=IDocumentIndexInfo)
+def collections_target_index_info(content):
+    """Collections target index info"""
+    intids = get_utility(IIntIds)
+    collections = ICollectionsInfo(content).collections or ()
+    return {
+        'collections': [intids.register(tag) for tag in unique(collections)]
+    }
+
+
+@adapter_config(name='collections', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewCollectionsQueryEsParamsExtension(ContextAdapter):
+    """View collections query adapter """
+
+    weight = 64
+
+    def get_es_params(self, context):
+        settings = IViewCollectionsSettings(self.context)
+        collections = settings.get_collections_index(context)
+        if collections:
+            return Q('terms', **{'collections': collections})