Updated "references" view extension to handle new references mode
authorThierry Florac <tflorac@ulthar.net>
Wed, 14 Nov 2018 17:48:03 +0100
changeset 102 2af087625f6f
parent 101 50d04e6c3704
child 103 c1329f9be074
Updated "references" view extension to handle new references mode
src/pyams_content_es/shared/view/__init__.py
src/pyams_content_es/shared/view/reference.py
--- a/src/pyams_content_es/shared/view/__init__.py	Wed Nov 14 17:46:37 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Wed Nov 14 17:48:03 2018 +0100
@@ -38,27 +38,34 @@
         # check publication dates
         now = tztime(datetime.utcnow())
         params = Q('range', **{'workflow.effective_date': {'lte': now}})
-        params &= Q('bool', must=Q('range', **{'workflow.push_end_date': {'gte': now}})) | \
-                  Q('bool', must_not=Q('exists', **{'field': 'workflow.push_end_date'}))
-        # check content types
-        content_types = view.get_content_types(context)
-        if content_types:
-            params &= Q('terms', **{'content_type': content_types})
-        # check data types
-        data_types = view.get_data_types(context)
-        if data_types:
-            params &= Q('terms', **{'data_type': data_types})
         # check workflow states
         wf_params = []
         for workflow in registry.getAllUtilitiesRegisteredFor(IWorkflow):
             wf_params.extend(workflow.published_states)
         params &= Q('terms', **{'workflow.status': wf_params})
         # check custom extensions
+        do_search = True
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension),
                                     key=lambda x: x[1].weight):
             new_params = adapter.get_es_params(context)
+            if isinstance(new_params, tuple):
+                new_params, do_search = new_params
             if new_params:
                 params &= new_params
+            if not do_search:
+                break
+        # activate search
+        if do_search:
+            params &= Q('bool', must=Q('range', **{'workflow.push_end_date': {'gte': now}})) | \
+                      Q('bool', must_not=Q('exists', **{'field': 'workflow.push_end_date'}))
+            # check content types
+            content_types = view.get_content_types(context)
+            if content_types:
+                params &= Q('terms', **{'content_type': content_types})
+            # check data types
+            data_types = view.get_data_types(context)
+            if data_types:
+                params &= Q('terms', **{'data_type': data_types})
         return params
 
     def get_results(self, context, sort_index, reverse, limit):
--- a/src/pyams_content_es/shared/view/reference.py	Wed Nov 14 17:46:37 2018 +0100
+++ b/src/pyams_content_es/shared/view/reference.py	Wed Nov 14 17:48:03 2018 +0100
@@ -12,16 +12,12 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
-
-# import interfaces
 from elasticsearch_dsl import Q
-from pyams_content.shared.view.interfaces import IWfView, IViewQueryEsParamsExtension, IViewInternalReferencesSettings
 from zope.intid.interfaces import IIntIds
 
-# import packages
-from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_content.shared.view.interfaces import IViewInternalReferencesSettings, IViewQueryEsParamsExtension, \
+    IWfView, ONLY_REFERENCE_MODE
+from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.registry import get_utility
 
 
@@ -29,10 +25,20 @@
 class ViewInternalReferencesQueryEsParamsExtension(ContextAdapter):
     """View internal references query params extension"""
 
-    weight = 50
+    weight = 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)
-            return Q('bool', must_not=Q('term', internal_id=intids.register(context)))
+            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