Added Elasticsearch query extension to check for themes
authorThierry Florac <thierry.florac@onf.fr>
Tue, 27 Jun 2017 13:35:40 +0200
changeset 12 7e02b1d62ed6
parent 11 2dfb793b4e0f
child 13 81395a2919f5
Added Elasticsearch query extension to check for themes
src/pyams_content_es/component/theme.py
--- a/src/pyams_content_es/component/theme.py	Tue Jun 27 13:34:29 2017 +0200
+++ b/src/pyams_content_es/component/theme.py	Tue Jun 27 13:35:40 2017 +0200
@@ -17,10 +17,12 @@
 
 # import interfaces
 from pyams_content.component.theme.interfaces import IThemesTarget, IThemesInfo
+from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings
 from pyams_content_es.interfaces import IDocumentIndexInfo
 
 # import packages
-from pyams_utils.adapter import adapter_config
+from elasticsearch_dsl import Q
+from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyams_utils.list import unique
 
 
@@ -42,3 +44,16 @@
                        'parents': unique(parents),
                        'synonyms': unique(synonyms),
                        'associations': unique(associations)}}
+
+
+@adapter_config(name='themes', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewThemesQueryEsParamsExtension(ContextAdapter):
+    """View themes query adapter """
+
+    weight = 60
+
+    def get_es_params(self, context):
+        settings = IViewThemesSettings(self.context)
+        themes = settings.get_themes_index(context)
+        if themes:
+            return Q('terms', **{'themes.terms': themes})