Handle case where portlet preview context is not bound to a search folder
authorThierry Florac <tflorac@ulthar.net>
Wed, 12 Dec 2018 17:22:59 +0100
changeset 1161 10bb070ff1d6
parent 1160 33de88e935cf
child 1162 d8d05f5c83ba
Handle case where portlet preview context is not bound to a search folder
src/pyams_content/features/search/portlet/__init__.py
--- a/src/pyams_content/features/search/portlet/__init__.py	Wed Dec 12 16:03:05 2018 +0100
+++ b/src/pyams_content/features/search/portlet/__init__.py	Wed Dec 12 17:22:59 2018 +0100
@@ -13,6 +13,7 @@
 __docformat__ = 'restructuredtext'
 
 import math
+from itertools import tee
 
 from zope.schema.fieldproperty import FieldProperty
 
@@ -38,21 +39,32 @@
     title = FieldProperty(ISearchResultsPortletSettings['title'])
 
     @staticmethod
-    def get_items(request=None, limit=None, ignore_cache=False):
+    def _get_items(request=None, limit=None, ignore_cache=False):
         context = get_parent(request.context, ISearchFolder)
         if context is None:
             raise StopIteration
-        if request is None:
-            request = check_request()
-        params = request.params
-        sort_index = params.get('order_by', RELEVANCE_ORDER)
-        yield from context.get_results(context, sort_index,
-                                       reverse=sort_index != RELEVANCE_ORDER,
-                                       limit=limit,
-                                       start=int(params.get('start', 0)),
-                                       length=int(params.get('length', 10)),
-                                       ignore_cache=ignore_cache,
-                                       get_count=True)
+        else:
+            if request is None:
+                request = check_request()
+            params = request.params
+            sort_index = params.get('order_by', RELEVANCE_ORDER)
+            yield from context.get_results(context, sort_index,
+                                           reverse=sort_index != RELEVANCE_ORDER,
+                                           limit=limit,
+                                           start=int(params.get('start', 0)),
+                                           length=int(params.get('length', 10)),
+                                           ignore_cache=ignore_cache,
+                                           get_count=True)
+
+    @staticmethod
+    def get_items(request=None, limit=None, ignore_cache=False):
+        check, items = tee(SearchResultsPortletSettings._get_items(request, limit, ignore_cache))
+        try:
+            next(check)
+        except StopIteration:
+            yield from iter(((), 0),)
+        else:
+            yield from items
 
     @staticmethod
     def get_pages(request, count):