Updated views interfaces to add optional "request" argument to params and results getters methods
authorThierry Florac <tflorac@ulthar.net>
Fri, 14 Dec 2018 11:12:42 +0100
changeset 115 6387a1195db4
parent 114 2f69b008b2e4
child 116 23855cc6ae6d
Updated views interfaces to add optional "request" argument to params and results getters methods
src/pyams_content_es/component/theme.py
src/pyams_content_es/shared/view/__init__.py
src/pyams_content_es/shared/view/reference.py
--- a/src/pyams_content_es/component/theme.py	Fri Nov 30 14:46:38 2018 +0100
+++ b/src/pyams_content_es/component/theme.py	Fri Dec 14 11:12:42 2018 +0100
@@ -47,7 +47,7 @@
 
     weight = 60
 
-    def get_es_params(self, context):
+    def get_es_params(self, context, request=None):
         settings = IViewTagsSettings(self.context)
         tags = settings.get_tags_index(context)
         if tags:
@@ -109,7 +109,7 @@
 
     weight = 62
 
-    def get_es_params(self, context):
+    def get_es_params(self, context, request=None):
         settings = IViewThemesSettings(self.context)
         themes = settings.get_themes_index(context)
         if themes:
@@ -136,7 +136,7 @@
 
     weight = 64
 
-    def get_es_params(self, context):
+    def get_es_params(self, context, request=None):
         settings = IViewCollectionsSettings(self.context)
         collections = settings.get_collections_index(context)
         if collections:
--- a/src/pyams_content_es/shared/view/__init__.py	Fri Nov 30 14:46:38 2018 +0100
+++ b/src/pyams_content_es/shared/view/__init__.py	Fri Dec 14 11:12:42 2018 +0100
@@ -24,7 +24,6 @@
     IViewUserQuery, IWfView, RELEVANCE_ORDER
 from pyams_utils.adapter import ContextAdapter, adapter_config
 from pyams_utils.list import unique_iter
-from pyams_utils.request import check_request
 from pyams_utils.timezone import tztime
 from pyams_workflow.interfaces import IWorkflow
 
@@ -33,7 +32,7 @@
 class EsViewQuery(ContextAdapter):
     """View query for Elasticsearch"""
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         view = self.context
         registry = get_current_registry()
         # check publication dates
@@ -48,7 +47,7 @@
         do_search = True
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryEsParamsExtension),
                                     key=lambda x: x[1].weight):
-            for new_params in adapter.get_es_params(context):
+            for new_params in adapter.get_es_params(context, request):
                 if isinstance(new_params, tuple):
                     new_params, do_search = new_params
                 if new_params:
@@ -73,15 +72,20 @@
                 params &= Q('terms', **{'data_type': data_types})
         return params
 
-    def get_results(self, context, sort_index, reverse, limit):
-        request = check_request()
+    def get_results(self, context, sort_index, reverse, limit, request=None):
         registry = request.registry
         client = get_client(request)
-        params = self.get_params(context)
+        params = self.get_params(context, request)
         search = Search(using=client.es, index=client.index) \
             .query(params) \
             .source(['internal_id'])
-        if sort_index != RELEVANCE_ORDER:
+        if (not sort_index) or (sort_index == RELEVANCE_ORDER):
+            search = search.sort({
+                '_score': {
+                    'order': 'desc'
+                }
+            })
+        else:
             search = search.sort({
                 'workflow.{0}'.format(sort_index): {
                     'order': 'desc' if reverse else 'asc',
@@ -95,7 +99,7 @@
         items = CatalogResultSet([result.internal_id for result in search])
         for name, adapter in sorted(registry.getAdapters((self.context,), IViewQueryFilterExtension),
                                     key=lambda x: x[1].weight):
-            items = adapter.filter(context, items)
+            items = adapter.filter(context, items, request)
         return unique_iter(items), search.count()
 
 
@@ -103,9 +107,8 @@
 class EsSearchFolderQuery(EsViewQuery):
     """Search folder view query for Elasticsearch"""
 
-    def get_params(self, context):
-        params = super(EsSearchFolderQuery, self).get_params(context)
-        request = check_request()
+    def get_params(self, context, request=None):
+        params = super(EsSearchFolderQuery, self).get_params(context, request)
         registry = request.registry
         for name, adapter in registry.getAdapters((self,), IViewUserQuery):
             for user_param in adapter.get_user_params(request):
--- a/src/pyams_content_es/shared/view/reference.py	Fri Nov 30 14:46:38 2018 +0100
+++ b/src/pyams_content_es/shared/view/reference.py	Fri Dec 14 11:12:42 2018 +0100
@@ -27,7 +27,7 @@
 
     weight = 10
 
-    def get_es_params(self, context):
+    def get_es_params(self, context, request=None):
         settings = IViewInternalReferencesSettings(self.context)
         # check view settings
         if settings.exclude_context: