src/pyams_content_es/shared/view/theme.py
changeset 138 739f21526374
parent 137 fc81a5fb2225
child 159 bc34f44a9711
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content_es/shared/view/theme.py	Tue Jan 08 14:05:53 2019 +0100
@@ -0,0 +1,101 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+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_es.shared.view import EsSearchFolderQuery
+from pyams_thesaurus.interfaces.thesaurus import IThesaurus
+from pyams_utils.adapter import ContextAdapter, adapter_config
+from pyams_utils.registry import query_utility
+
+
+#
+# Tags query
+#
+
+@adapter_config(name='tags', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewTagsQueryEsParamsExtension(ContextAdapter):
+    """View tags query adapter """
+
+    weight = 60
+
+    def get_es_params(self, context, request=None):
+        settings = IViewTagsSettings(self.context)
+        tags = settings.get_tags_index(context)
+        if tags:
+            yield Q('terms', **{'tags': tags})
+        elif settings.select_context_tags:
+            yield None
+
+
+@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)})
+
+
+#
+# Themes query
+#
+
+@adapter_config(name='themes', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewThemesQueryEsParamsExtension(ContextAdapter):
+    """View themes query adapter """
+
+    weight = 62
+
+    def get_es_params(self, context, request=None):
+        settings = IViewThemesSettings(self.context)
+        themes = settings.get_themes_index(context)
+        if themes:
+            yield Q('terms', **{'themes.terms': themes})
+        elif settings.select_context_themes:
+            yield None
+
+
+#
+# Collections query
+#
+
+@adapter_config(name='collections', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewCollectionsQueryEsParamsExtension(ContextAdapter):
+    """View collections query adapter """
+
+    weight = 64
+
+    def get_es_params(self, context, request=None):
+        settings = IViewCollectionsSettings(self.context)
+        collections = settings.get_collections_index(context)
+        if collections:
+            yield Q('terms', **{'collections': collections})
+        elif settings.select_context_collections:
+            yield None