Added attribute in search results portlet to avoid search without criteria when needed
authorThierry Florac <thierry.florac@onf.fr>
Mon, 07 Jan 2019 11:22:34 +0100
changeset 1214 584bf988caa8
parent 1213 22b0a3be985d
child 1215 b8da9b04f74d
Added attribute in search results portlet to avoid search without criteria when needed
src/pyams_content/features/search/portlet/__init__.py
src/pyams_content/features/search/portlet/interfaces.py
src/pyams_content/features/search/portlet/zmi/templates/search-preview.pt
--- a/src/pyams_content/features/search/portlet/__init__.py	Mon Jan 07 09:58:55 2019 +0100
+++ b/src/pyams_content/features/search/portlet/__init__.py	Mon Jan 07 11:22:34 2019 +0100
@@ -36,6 +36,7 @@
     """Search results portlet settings"""
 
     title = FieldProperty(ISearchResultsPortletSettings['title'])
+    allow_empty_query = FieldProperty(ISearchResultsPortletSettings['allow_empty_query'])
     force_canonical_url = FieldProperty(ISearchResultsPortletSettings['force_canonical_url'])
 
     @staticmethod
@@ -57,15 +58,17 @@
                                            get_count=True,
                                            request=request)
 
-    @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:
+    def get_items(self, request=None, limit=None, ignore_cache=False):
+        if not (self.allow_empty_query or request.params.get('user_search', '').strip()):
             yield from iter(((), 0),)
         else:
-            yield from items
+            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):
--- a/src/pyams_content/features/search/portlet/interfaces.py	Mon Jan 07 09:58:55 2019 +0100
+++ b/src/pyams_content/features/search/portlet/interfaces.py	Mon Jan 07 11:22:34 2019 +0100
@@ -14,10 +14,11 @@
 
 from zope.schema import Bool
 
-from pyams_content import _
 from pyams_i18n.schema import I18nTextLineField
 from pyams_portal.interfaces import IPortletSettings
 
+from pyams_content import _
+
 
 class ISearchResultsPortletSettings(IPortletSettings):
     """Search results portlet settings"""
@@ -26,6 +27,12 @@
                               description=_("Portlet main title"),
                               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"),
+                             required=True,
+                             default=True)
+
     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, "
--- a/src/pyams_content/features/search/portlet/zmi/templates/search-preview.pt	Mon Jan 07 09:58:55 2019 +0100
+++ b/src/pyams_content/features/search/portlet/zmi/templates/search-preview.pt	Mon Jan 07 11:22:34 2019 +0100
@@ -3,6 +3,10 @@
 				 (items, count) settings.get_items(request, limit=10, ignore_cache=True);">
 	<strong>${i18n:settings.title}</strong>
 	<div class="padding-x-10">
+		<i class="fa fa-fw fa-${'check-' if settings.allow_empty_query else ''}square-o"></i>
+		<span i18n:translate="">Allow empty query</span>
+	</div>
+	<div class="padding-x-10">
 		<i class="fa fa-fw fa-${'check-' if settings.force_canonical_url else ''}square-o"></i>
 		<span i18n:translate="">Force canonical URL</span>
 	</div>