Updated search results portlet API
authorThierry Florac <tflorac@ulthar.net>
Wed, 25 Sep 2019 09:52:05 +0200
changeset 1352 8242968d86b1
parent 1351 045be80a5645
child 1353 5940373ec65c
Updated search results portlet API
src/pyams_content/features/search/portlet/__init__.py
src/pyams_content/features/search/portlet/interfaces.py
--- a/src/pyams_content/features/search/portlet/__init__.py	Wed Sep 25 09:50:05 2019 +0200
+++ b/src/pyams_content/features/search/portlet/__init__.py	Wed Sep 25 09:52:05 2019 +0200
@@ -45,7 +45,7 @@
         return params.get('user_search', '').strip() or params.get('tag', '').strip()
 
     @staticmethod
-    def _get_items(request=None, limit=None, ignore_cache=False):
+    def _get_items(request=None, start=0, length=10, limit=None, ignore_cache=False):
         context = get_parent(request.context, ISearchFolder)
         if context is None:
             raise StopIteration
@@ -54,34 +54,36 @@
                 request = check_request()
             params = request.params
             order_by = params.get('order_by', context.order_by)
-            if (order_by == RELEVANCE_ORDER) and not SearchResultsPortletSettings.has_user_query(request):
+            if (order_by == RELEVANCE_ORDER) and \
+                    not SearchResultsPortletSettings.has_user_query(request):
                 request.GET['order_by'] = order_by = VISIBLE_PUBLICATION_DATE_ORDER
             yield from context.get_results(context, order_by,
                                            reverse=order_by != RELEVANCE_ORDER,
                                            limit=limit,
-                                           start=int(params.get('start', 0)),
-                                           length=int(params.get('length', 10)),
+                                           start=int(start),
+                                           length=int(length),
                                            ignore_cache=ignore_cache,
                                            get_count=True,
                                            request=request)
 
-    def get_items(self, request=None, limit=None, ignore_cache=False):
+    def get_items(self, request=None, start=0, length=10, limit=None, ignore_cache=False):
         if not (self.allow_empty_query or self.has_user_query(request)):
-            yield from iter(((), 0),)
+            yield from iter(((), 0), )
         else:
-            check, items = tee(SearchResultsPortletSettings._get_items(request, limit, ignore_cache))
+            check, items = tee(
+                SearchResultsPortletSettings._get_items(request, start, length, limit,
+                                                        ignore_cache))
             try:
                 next(check)
             except StopIteration:
-                yield from iter(((), 0),)
+                yield from iter(((), 0), )
             else:
                 yield from items
 
     @staticmethod
-    def get_pages(request, count):
-        params = request.params
-        start = int(params.get('start', 0)) + 1
-        length = int(params.get('length', 10))
+    def get_pages(start, length, count):
+        start = int(start) + 1
+        length = int(length)
         current = math.ceil(start / length)
         nb_pages = math.ceil(count / length)
         return current, nb_pages
--- a/src/pyams_content/features/search/portlet/interfaces.py	Wed Sep 25 09:50:05 2019 +0200
+++ b/src/pyams_content/features/search/portlet/interfaces.py	Wed Sep 25 09:52:05 2019 +0200
@@ -27,17 +27,21 @@
                               required=False)
 
     allow_empty_query = Bool(title=_("Allow empty query?"),
-                             description=_("If 'no', no result will be displayed if user didn't entered a search "
-                                           "string"),
+                             description=_("If 'no', no result will be displayed if user didn't "
+                                           "entered a search string"),
                              required=True,
                              default=True)
 
     def has_user_query(self):
         """Check if user entered custom search arguments"""
 
+    def get_items(self, request=None, limit=None, ingore_cache=False):
+        """Get search results"""
+
     force_canonical_url = Bool(title=_("Force canonical URL?"),
-                               description=_("By default, internal links use a \"relative\" URL, which tries to "
-                                             "display link target in the current context; by using a canonical URL, "
-                                             "you can display target in it's attachment context (if defined)"),
+                               description=_("By default, internal links use a \"relative\" URL, "
+                                             "which tries to display link target in the current "
+                                             "context; by using a canonical URL, you can display "
+                                             "target in it's attachment context (if defined)"),
                                required=False,
                                default=False)