Added theme query support
authorThierry Florac <tflorac@ulthar.net>
Mon, 11 Jan 2021 16:48:43 +0100 (2021-01-11)
changeset 174 812bfc53bf5c
parent 173 c69e460b5d73
child 175 17aaf1ec3390
Added theme query support
src/pyams_content_es/shared/view/theme.py
--- a/src/pyams_content_es/shared/view/theme.py	Fri Nov 13 16:33:07 2020 +0100
+++ b/src/pyams_content_es/shared/view/theme.py	Mon Jan 11 16:48:43 2021 +0100
@@ -15,7 +15,8 @@
 from elasticsearch_dsl import Q
 from zope.intid.interfaces import IIntIds
 
-from pyams_content.component.theme import ICollectionsManager, ITagsManager
+from pyams_content.component.theme import ICollectionsManager, ITagsManager, IThemesManager, \
+    IThemesManagerTarget
 from pyams_content.shared.view.interfaces import IViewCollectionsSettings, \
     IViewQueryEsParamsExtension, IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
 from pyams_content_es.shared.view import EsSearchFolderQuery
@@ -27,6 +28,8 @@
 #
 # Tags query
 #
+from pyams_utils.traversing import get_parent
+
 
 @adapter_config(name='tags', context=IWfView, provides=IViewQueryEsParamsExtension)
 class ViewTagsQueryEsParamsExtension(ContextAdapter):
@@ -49,18 +52,19 @@
 
     @staticmethod
     def get_user_params(request):
-        tag = request.params.get('tag')
-        if tag:
+        tags = request.params.getall('tag')
+        if tags:
             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)})
+            intids = query_utility(IIntIds)
+            for tag in tags:
+                term = thesaurus.terms.get(tag)
+                if term is not None:
+                    yield Q('term', **{'tags': intids.queryId(term)})
 
 
 #
@@ -82,6 +86,29 @@
             yield None
 
 
+@adapter_config(name='themes', context=EsSearchFolderQuery, provides=IViewUserQuery)
+class EsSearchFolderThemesQuery(ContextAdapter):
+    """Search folder themes query for Elasticsearch"""
+
+    def get_user_params(self, request):
+        themes = request.params.getall('theme')
+        if themes:
+            themes_target = get_parent(self.context.context, IThemesManagerTarget)
+            if themes_target is None:
+                raise StopIteration
+            manager = IThemesManager(themes_target, None)
+            if manager is None:
+                raise StopIteration
+            thesaurus = query_utility(IThesaurus, name=manager.thesaurus_name)
+            if thesaurus is None:
+                raise StopIteration
+            intids = query_utility(IIntIds)
+            for theme in themes:
+                term = thesaurus.terms.get(theme)
+                if term is not None:
+                    yield Q('term', **{'themes.terms': intids.queryId(term)})
+
+
 #
 # Collections query
 #