Switch search results sort on publication date if relevance is used without user query
authorThierry Florac <tflorac@ulthar.net>
Fri, 25 Jan 2019 11:42:01 +0100
changeset 1261 c9f9e7036daf
parent 1260 cae08a8096fa
child 1262 0833a105b06b
Switch search results sort on publication date if relevance is used without user query
src/pyams_content/features/search/portlet/__init__.py
--- a/src/pyams_content/features/search/portlet/__init__.py	Thu Jan 24 11:38:45 2019 +0100
+++ b/src/pyams_content/features/search/portlet/__init__.py	Fri Jan 25 11:42:01 2019 +0100
@@ -20,7 +20,7 @@
 from pyams_content import _
 from pyams_content.features.search import ISearchFolder
 from pyams_content.features.search.portlet.interfaces import ISearchResultsPortletSettings
-from pyams_content.shared.view.interfaces import RELEVANCE_ORDER
+from pyams_content.shared.view.interfaces import RELEVANCE_ORDER, VISIBLE_PUBLICATION_DATE_ORDER
 from pyams_portal.portlet import Portlet, PortletSettings, portlet_config
 from pyams_utils.factory import factory_config
 from pyams_utils.interfaces import VIEW_PERMISSION
@@ -53,9 +53,11 @@
             if request is None:
                 request = check_request()
             params = request.params
-            sort_index = params.get('order_by', context.order_by)
-            yield from context.get_results(context, sort_index,
-                                           reverse=sort_index != RELEVANCE_ORDER,
+            order_by = params.get('order_by', context.order_by)
+            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)),