Added tags support
authorThierry Florac <thierry.florac@onf.fr>
Thu, 05 Jul 2018 08:54:10 +0200
changeset 74 b7976cad0ef0
parent 73 bf9a6e10e5ea
child 75 a4a2dc04040c
Added tags support
src/pyams_content_es/component/theme.py
--- a/src/pyams_content_es/component/theme.py	Mon Jul 02 17:12:56 2018 +0200
+++ b/src/pyams_content_es/component/theme.py	Thu Jul 05 08:54:10 2018 +0200
@@ -16,8 +16,8 @@
 # import standard library
 
 # import interfaces
-from pyams_content.component.theme.interfaces import IThemesTarget, IThemesInfo
-from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings
+from pyams_content.component.theme.interfaces import ITagsTarget, ITagsInfo, IThemesTarget, IThemesInfo
+from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewThemesSettings, IViewTagsSettings
 from pyams_content_es.interfaces import IDocumentIndexInfo
 from zope.intid.interfaces import IIntIds
 
@@ -28,6 +28,29 @@
 from pyams_utils.registry import get_utility
 
 
+@adapter_config(name='tags', context=ITagsTarget, provides=IDocumentIndexInfo)
+def tags_target_index_info(content):
+    """Tags target index info"""
+    intids = get_utility(IIntIds)
+    tags = ITagsInfo(content).tags or ()
+    return {
+        'tags': [intids.register(tag) for tag in unique(tags)]
+    }
+
+
+@adapter_config(name='tags', context=IWfView, provides=IViewQueryEsParamsExtension)
+class ViewTagsQueryEsParamsExtension(ContextAdapter):
+    """View tags query adapter """
+
+    weight = 60
+
+    def get_es_params(self, context):
+        settings = IViewTagsSettings(self.context)
+        tags = settings.get_tags_index(context)
+        if tags:
+            return Q('terms', **{'tags': tags})
+
+
 @adapter_config(name='themes', context=IThemesTarget, provides=IDocumentIndexInfo)
 def themes_target_index_info(content):
     """Themes target index info"""
@@ -57,7 +80,7 @@
 class ViewThemesQueryEsParamsExtension(ContextAdapter):
     """View themes query adapter """
 
-    weight = 60
+    weight = 62
 
     def get_es_params(self, context):
         settings = IViewThemesSettings(self.context)