# HG changeset patch # User Thierry Florac # Date 1546856554 -3600 # Node ID 584bf988caa88d1b282a4f7b79e7f2882a516f1c # Parent 22b0a3be985d5f72420021c31191ba856418c344 Added attribute in search results portlet to avoid search without criteria when needed diff -r 22b0a3be985d -r 584bf988caa8 src/pyams_content/features/search/portlet/__init__.py --- 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): diff -r 22b0a3be985d -r 584bf988caa8 src/pyams_content/features/search/portlet/interfaces.py --- 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, " diff -r 22b0a3be985d -r 584bf988caa8 src/pyams_content/features/search/portlet/zmi/templates/search-preview.pt --- 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);"> ${i18n:settings.title}
+ + Allow empty query +
+
Force canonical URL