Use iterators to build query params
authorThierry Florac <thierry.florac@onf.fr>
Mon, 26 Nov 2018 10:16:04 +0100
changeset 109 b9885a695f88
parent 108 4c2168458680
child 110 d1122192d6f8
Use iterators to build query params
src/pyams_content_es/component/theme.py
src/pyams_content_es/shared/view/reference.py
--- a/src/pyams_content_es/component/theme.py	Mon Nov 26 10:15:23 2018 +0100
+++ b/src/pyams_content_es/component/theme.py	Mon Nov 26 10:16:04 2018 +0100
@@ -9,25 +9,22 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_content.component.theme import ICollectionsTarget, ICollectionsInfo
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
-from pyams_content.component.theme.interfaces import ITagsTarget, ITagsInfo, IThemesTarget, IThemesInfo
-from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings, \
-    IViewTagsSettings, IViewCollectionsSettings
-from pyams_content_es.interfaces import IDocumentIndexInfo
+from elasticsearch_dsl import Q
 from zope.intid.interfaces import IIntIds
 
-# import packages
-from elasticsearch_dsl import Q
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_content.component.theme import ICollectionsInfo, ICollectionsTarget, ITagsManager
+from pyams_content.component.theme.interfaces import ITagsInfo, ITagsTarget, IThemesInfo, IThemesTarget
+from pyams_content.shared.view.interfaces import IViewCollectionsSettings, IViewQueryEsParamsExtension, \
+    IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
+from pyams_content_es.interfaces import IDocumentIndexInfo
+from pyams_content_es.shared.view import EsSearchFolderQuery
+from pyams_thesaurus.interfaces.thesaurus import IThesaurus
+from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.list import unique
-from pyams_utils.registry import get_utility
+from pyams_utils.registry import get_utility, query_utility
 
 
 #
@@ -54,7 +51,27 @@
         settings = IViewTagsSettings(self.context)
         tags = settings.get_tags_index(context)
         if tags:
-            return Q('terms', **{'tags': tags})
+            yield Q('terms', **{'tags': tags})
+
+
+@adapter_config(name='tag', context=EsSearchFolderQuery, provides=IViewUserQuery)
+class EsSearchFolderTagQuery(ContextAdapter):
+    """Search folder tag query for Elasticsearch"""
+
+    @staticmethod
+    def get_user_params(request):
+        tag = request.params.get('tag')
+        if tag:
+            manager = ITagsManager(request.root, None)
+            if manager is None:
+                raise StopIteration
+            thesaurus = query_utility(IThesaurus, name=manager.thesaurus_name)
+            if thesaurus is None:
+                raise StopIteration
+            term = thesaurus.terms.get(tag)
+            if term is not None:
+                intids = query_utility(IIntIds)
+                yield Q('term', **{'tags': intids.queryId(term)})
 
 
 #
@@ -96,7 +113,7 @@
         settings = IViewThemesSettings(self.context)
         themes = settings.get_themes_index(context)
         if themes:
-            return Q('terms', **{'themes.terms': themes})
+            yield Q('terms', **{'themes.terms': themes})
 
 
 #
@@ -123,4 +140,4 @@
         settings = IViewCollectionsSettings(self.context)
         collections = settings.get_collections_index(context)
         if collections:
-            return Q('terms', **{'collections': collections})
+            yield Q('terms', **{'collections': collections})
--- a/src/pyams_content_es/shared/view/reference.py	Mon Nov 26 10:15:23 2018 +0100
+++ b/src/pyams_content_es/shared/view/reference.py	Mon Nov 26 10:16:04 2018 +0100
@@ -29,16 +29,10 @@
 
     def get_es_params(self, context):
         settings = IViewInternalReferencesSettings(self.context)
-        # check view references mode
-        query = None
-        if settings.references_mode == ONLY_REFERENCE_MODE:
-            query = Q('terms', **{'reference_id': settings.references})
         # check view settings
         if settings.exclude_context:
             intids = get_utility(IIntIds)
-            context_query = Q('bool', must_not=Q('term', internal_id=intids.register(context)))
-            if query is None:
-                query = context_query
-            else:
-                query &= context_query
-        return query, settings.references_mode != ONLY_REFERENCE_MODE
+            yield Q('bool', must_not=Q('term', internal_id=intids.register(context)))
+        # check view references mode
+        if settings.references_mode == ONLY_REFERENCE_MODE:
+            yield Q('terms', **{'reference_id': settings.references}), False