Added search adapter for collections
authorThierry Florac <tflorac@ulthar.net>
Fri, 26 Jun 2020 12:55:05 +0200
changeset 159 bc34f44a9711
parent 158 4ae24ec1db78
child 160 7ed5f188e9df
Added search adapter for collections
src/pyams_content_es/component/theme.py
src/pyams_content_es/shared/view/theme.py
--- a/src/pyams_content_es/component/theme.py	Thu Sep 26 11:57:40 2019 +0200
+++ b/src/pyams_content_es/component/theme.py	Fri Jun 26 12:55:05 2020 +0200
@@ -15,7 +15,8 @@
 from zope.intid.interfaces import IIntIds
 
 from pyams_content.component.theme import ICollectionsInfo, ICollectionsTarget
-from pyams_content.component.theme.interfaces import ITagsInfo, ITagsTarget, IThemesInfo, IThemesTarget
+from pyams_content.component.theme.interfaces import ITagsInfo, ITagsTarget, IThemesInfo, \
+    IThemesTarget
 from pyams_content_es.interfaces import IDocumentIndexInfo
 from pyams_utils.adapter import adapter_config
 from pyams_utils.list import unique
--- a/src/pyams_content_es/shared/view/theme.py	Thu Sep 26 11:57:40 2019 +0200
+++ b/src/pyams_content_es/shared/view/theme.py	Fri Jun 26 12:55:05 2020 +0200
@@ -15,9 +15,9 @@
 from elasticsearch_dsl import Q
 from zope.intid.interfaces import IIntIds
 
-from pyams_content.component.theme import ITagsManager
-from pyams_content.shared.view.interfaces import IViewCollectionsSettings, IViewQueryEsParamsExtension, \
-    IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
+from pyams_content.component.theme import ICollectionsManager, ITagsManager
+from pyams_content.shared.view.interfaces import IViewCollectionsSettings, \
+    IViewQueryEsParamsExtension, IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView
 from pyams_content_es.shared.view import EsSearchFolderQuery
 from pyams_thesaurus.interfaces.thesaurus import IThesaurus
 from pyams_utils.adapter import ContextAdapter, adapter_config
@@ -99,3 +99,23 @@
             yield Q('terms', **{'collections': collections})
         elif settings.select_context_collections:
             yield None
+
+
+@adapter_config(name='collection', context=EsSearchFolderQuery, provides=IViewUserQuery)
+class EsSearchFolderCollectionQuery(ContextAdapter):
+    """Search folder collection query for Elasticsearch"""
+
+    @staticmethod
+    def get_user_params(request):
+        collection = request.params.get('collection')
+        if collection:
+            manager = ICollectionsManager(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(collection)
+            if term is not None:
+                intids = query_utility(IIntIds)
+                yield Q('term', **{'collections': intids.queryId(term)})