Updated views interfaces to add optional "request" argument to params ans results getters methods
authorThierry Florac <tflorac@ulthar.net>
Fri, 14 Dec 2018 11:08:28 +0100
changeset 1167 04429c162338
parent 1166 32668a609c7b
child 1168 3b177d6db9db
Updated views interfaces to add optional "request" argument to params ans results getters methods
src/pyams_content/features/search/__init__.py
src/pyams_content/features/search/portlet/__init__.py
src/pyams_content/shared/view/__init__.py
src/pyams_content/shared/view/interfaces.py
src/pyams_content/shared/view/merge.py
src/pyams_content/shared/view/portlet/__init__.py
src/pyams_content/shared/view/reference.py
src/pyams_content/shared/view/theme.py
--- a/src/pyams_content/features/search/__init__.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/features/search/__init__.py	Fri Dec 14 11:08:28 2018 +0100
@@ -61,12 +61,12 @@
         return True
 
     def get_results(self, context, sort_index=None, reverse=None, limit=None,
-                    start=0, length=None, ignore_cache=False, get_count=False):
+                    start=0, length=None, ignore_cache=False, get_count=False, request=None):
         if not ignore_cache:
             request = check_request()
             ignore_cache = bool(request.params)
-        return super(SearchFolder, self).get_results(context, sort_index, reverse, limit,
-                                                     start, length, ignore_cache, get_count)
+        return super(SearchFolder, self).get_results(context, sort_index, reverse, limit, start,
+                                                     length, ignore_cache, get_count, request)
 
 
 @adapter_config(context=ISearchFolder, provides=IFormContextPermissionChecker)
@@ -80,9 +80,8 @@
 class SearchFolderQuery(ViewQuery):
     """Search folder query adapter"""
 
-    def get_params(self, context):
-        params = super(SearchFolderQuery, self).get_params(context)
-        request = check_request()
+    def get_params(self, context, request=None):
+        params = super(SearchFolderQuery, 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/features/search/portlet/__init__.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/features/search/portlet/__init__.py	Fri Dec 14 11:08:28 2018 +0100
@@ -54,7 +54,8 @@
                                            start=int(params.get('start', 0)),
                                            length=int(params.get('length', 10)),
                                            ignore_cache=ignore_cache,
-                                           get_count=True)
+                                           get_count=True,
+                                           request=request)
 
     @staticmethod
     def get_items(request=None, limit=None, ignore_cache=False):
--- a/src/pyams_content/shared/view/__init__.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/__init__.py	Fri Dec 14 11:08:28 2018 +0100
@@ -111,7 +111,7 @@
         return list(data_types)
 
     def get_results(self, context, sort_index=None, reverse=None, limit=None,
-                    start=0, length=999, ignore_cache=False, get_count=False):
+                    start=0, length=999, ignore_cache=False, get_count=False, request=None):
         results = _MARKER
         if not ignore_cache:
             # check for cache
@@ -132,14 +132,13 @@
             adapter = registry.queryAdapter(self, IViewQuery, name='es')
             if adapter is None:
                 adapter = registry.getAdapter(self, IViewQuery)
-            if (not sort_index) or (sort_index == RELEVANCE_ORDER):
-                sort_index = None  # keep natural order
-            else:
+            if not sort_index:
                 sort_index = self.order_by
             results, count = adapter.get_results(context,
                                                  sort_index,
                                                  reverse if reverse is not None else self.reversed_order,
-                                                 limit or self.limit)
+                                                 limit or self.limit,
+                                                 request=request)
             count = min(count, limit or self.limit or 999)
             cache, results = tee(islice(results, start, start + length))
             if not ignore_cache:
@@ -171,7 +170,7 @@
 class ViewQuery(ContextAdapter):
     """View query"""
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         view = self.context
         catalog = get_utility(ICatalog)
         registry = get_current_registry()
@@ -187,7 +186,7 @@
         do_search = True
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryParamsExtension),
                                     key=lambda x: x[1].weight):
-            for new_params in adapter.get_params(context):
+            for new_params in adapter.get_params(context, request):
                 if isinstance(new_params, tuple):
                     new_params, do_search = new_params
                 if new_params:
@@ -211,11 +210,11 @@
                 params &= Any(catalog['data_type'], data_types)
         return params
 
-    def get_results(self, context, sort_index, reverse, limit):
+    def get_results(self, context, sort_index, reverse, limit, request=None):
         view = self.context
         catalog = get_utility(ICatalog)
         registry = get_current_registry()
-        params = self.get_params(context)
+        params = self.get_params(context, request)
         if sort_index == RELEVANCE_ORDER:
             sort_index = None
         query = CatalogQuery(catalog).query(params,
@@ -226,7 +225,7 @@
         items = CatalogResultSet(query)
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryFilterExtension),
                                     key=lambda x: x[1].weight):
-            items = adapter.filter(context, items)
+            items = adapter.filter(context, items, request)
         return unique_iter(items), total_count
 
 
--- a/src/pyams_content/shared/view/interfaces.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/interfaces.py	Fri Dec 14 11:08:28 2018 +0100
@@ -123,7 +123,7 @@
     is_using_context = Attribute("Check if view is using context settings")
 
     def get_results(self, context, sort_index=None, reverse=True, limit=None,
-                    start=0, length=999, ignore_cache=False, get_count=False):
+                    start=0, length=999, ignore_cache=False, get_count=False, request=None):
         """Get results of catalog query"""
 
 
@@ -144,17 +144,17 @@
 class IViewQuery(Interface):
     """View query interface"""
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         """Get static view query params"""
 
-    def get_results(self, context, sort_index, reverse, limit):
+    def get_results(self, context, sort_index, reverse, limit, request=None):
         """Get tuple of limited results and total results count"""
 
 
 class IViewUserQuery(Interface):
     """View user search query interface"""
 
-    def get_user_params(self, request):
+    def get_user_params(self, request=None):
         """Get dynamic user query params"""
 
 
@@ -167,21 +167,21 @@
 class IViewQueryParamsExtension(IViewQueryExtension):
     """View query extension interface"""
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         """Add params to catalog query"""
 
 
 class IViewQueryEsParamsExtension(IViewQueryExtension):
     """View query parameters extension for Elasticsearch"""
 
-    def get_es_params(self, context):
+    def get_es_params(self, context, request=None):
         """Add params to Elasticsearch query"""
 
 
 class IViewQueryFilterExtension(IViewQueryExtension):
     """View query filter extension"""
 
-    def filter(self, context, items):
+    def filter(self, context, items, request=None):
         """Filter items after catalog query"""
 
 
@@ -287,5 +287,5 @@
     Mergers are used to merge results of several views.
     """
 
-    def get_results(self, views, context, ignore_cache=False):
+    def get_results(self, views, context, ignore_cache=False, request=None):
         """Merge results of several views together"""
--- a/src/pyams_content/shared/view/merge.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/merge.py	Fri Dec 14 11:08:28 2018 +0100
@@ -59,8 +59,8 @@
     label = _("Concatenate views items in order")
 
     @classmethod
-    def get_results(cls, views, context, ignore_cache=False):
-        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
+    def get_results(cls, views, context, ignore_cache=False, request=None):
+        results = (view.get_results(context, ignore_cache=ignore_cache, request=request) for view in views)
         yield from chain(*results)
 
 
@@ -71,8 +71,8 @@
     label = _("Extract items randomly")
 
     @classmethod
-    def get_results(cls, views, context, ignore_cache=False):
-        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
+    def get_results(cls, views, context, ignore_cache=False, request=None):
+        results = (view.get_results(context, ignore_cache=ignore_cache, request=request) for view in views)
         results = list(chain(*results))
         shuffle(results)
         yield from iter(results)
@@ -85,8 +85,8 @@
     label = _("Take items from views one by one, in views order")
 
     @classmethod
-    def get_results(cls, views, context, ignore_cache=False):
-        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
+    def get_results(cls, views, context, ignore_cache=False, request=None):
+        results = (view.get_results(context, ignore_cache=ignore_cache, request=request) for view in views)
         for array in zip_longest(*results):
             yield from filter(lambda x: x is not None, array)
 
@@ -98,8 +98,8 @@
     label = _("Take items from views one by one, in random order")
 
     @classmethod
-    def get_results(cls, views, context, ignore_cache=False):
-        results = [view.get_results(context, ignore_cache=ignore_cache) for view in views]
+    def get_results(cls, views, context, ignore_cache=False, request=None):
+        results = [view.get_results(context, ignore_cache=ignore_cache, request=request) for view in views]
         shuffle(results)
         for array in zip_longest(*results):
             yield from filter(lambda x: x is not None, array)
@@ -112,9 +112,8 @@
     sort_key = None
 
     @classmethod
-    def get_results(cls, views, context, ignore_cache=False):
-        results = (sorted(view.get_results(context,
-                                           ignore_cache=ignore_cache),
+    def get_results(cls, views, context, ignore_cache=False, request=None):
+        results = (sorted(view.get_results(context, ignore_cache=ignore_cache, request=request),
                           key=cls.sort_key, reverse=True)
                    for view in views)
         yield from merge(*results, key=cls.sort_key, reverse=True)
--- a/src/pyams_content/shared/view/portlet/__init__.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/portlet/__init__.py	Fri Dec 14 11:08:28 2018 +0100
@@ -73,7 +73,8 @@
             start = int(request.params.get('start', 0))
             yield from islice(unique_iter(merger.get_results(self.get_views(),
                                                              context,
-                                                             ignore_cache=ignore_cache)),
+                                                             ignore_cache=ignore_cache,
+                                                             request=request)),
                               start + (self.start or 1) - 1,
                               min(limit or 999, self.limit or 999))
 
--- a/src/pyams_content/shared/view/reference.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/reference.py	Fri Dec 14 11:08:28 2018 +0100
@@ -57,7 +57,7 @@
 
     weight = 10
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         settings = IViewInternalReferencesSettings(self.context)
         # check view settings
         if settings.exclude_context:
@@ -78,7 +78,7 @@
 
     weight = 999
 
-    def filter(self, context, items):
+    def filter(self, context, items, request=None):
         settings = IViewInternalReferencesSettings(self.context)
         if not settings.references:
             return items
--- a/src/pyams_content/shared/view/theme.py	Fri Dec 14 09:01:01 2018 +0100
+++ b/src/pyams_content/shared/view/theme.py	Fri Dec 14 11:08:28 2018 +0100
@@ -75,7 +75,7 @@
 
     weight = 50
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         catalog = get_utility(ICatalog)
         settings = IViewTagsSettings(self.context)
         # check tags
@@ -149,7 +149,7 @@
 
     weight = 52
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         catalog = get_utility(ICatalog)
         settings = IViewThemesSettings(self.context)
         # check themes
@@ -202,7 +202,7 @@
 
     weight = 54
 
-    def get_params(self, context):
+    def get_params(self, context, request=None):
         catalog = get_utility(ICatalog)
         settings = IViewCollectionsSettings(self.context)
         # check collections