src/pyams_content/features/search/__init__.py
changeset 1121 852aa448da04
parent 1061 d1db251eeea3
child 1167 04429c162338
--- a/src/pyams_content/features/search/__init__.py	Mon Nov 26 17:06:03 2018 +0100
+++ b/src/pyams_content/features/search/__init__.py	Tue Nov 27 08:50:02 2018 +0100
@@ -12,17 +12,23 @@
 
 __docformat__ = 'restructuredtext'
 
+from hypatia.interfaces import ICatalog
+from hypatia.query import Contains, Or
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.component.illustration import IIllustrationTarget, ILinkIllustrationTarget
 from pyams_content.features.preview.interfaces import IPreviewTarget
 from pyams_content.features.search.interfaces import ISearchFolder, ISearchFolderRoles
-from pyams_content.interfaces import MANAGE_SITE_PERMISSION, MANAGER_ROLE, GUEST_ROLE
-from pyams_content.shared.view import WfView
+from pyams_content.interfaces import GUEST_ROLE, MANAGER_ROLE, MANAGE_SITE_PERMISSION
+from pyams_content.shared.view import IViewQuery, ViewQuery, WfView
+from pyams_content.shared.view.interfaces import IViewUserQuery
 from pyams_form.interfaces.form import IFormContextPermissionChecker
-from pyams_portal.interfaces import IPortalContext, DESIGNER_ROLE
+from pyams_i18n.interfaces import INegotiator
+from pyams_portal.interfaces import DESIGNER_ROLE, IPortalContext
 from pyams_utils.adapter import ContextAdapter, adapter_config
+from pyams_utils.registry import get_utility
+from pyams_utils.request import check_request
 
 from pyams_content import _
 
@@ -37,7 +43,6 @@
 
     content_name = _("Search folder")
 
-    handle_content_url = True
     handle_header = True
     handle_description = True
 
@@ -47,6 +52,7 @@
     selected_content_types = FieldProperty(ISearchFolder['selected_content_types'])
     selected_datatypes = FieldProperty(ISearchFolder['selected_datatypes'])
 
+    order_by = FieldProperty(ISearchFolder['order_by'])
     visible_in_list = FieldProperty(ISearchFolder['visible_in_list'])
     navigation_title = FieldProperty(ISearchFolder['navigation_title'])
 
@@ -54,9 +60,54 @@
     def is_deletable():
         return True
 
+    def get_results(self, context, sort_index=None, reverse=None, limit=None,
+                    start=0, length=None, ignore_cache=False, get_count=False):
+        if not ignore_cache:
+            request = check_request()
+            ignore_cache = bool(request.params)
+        return super(SearchFolder, self).get_results(context, sort_index, reverse, limit,
+                                                     start, length, ignore_cache, get_count)
+
 
 @adapter_config(context=ISearchFolder, provides=IFormContextPermissionChecker)
 class SearchFolderPermissionChecker(ContextAdapter):
     """Search folder edit permission checker"""
 
     edit_permission = MANAGE_SITE_PERMISSION
+
+
+@adapter_config(context=ISearchFolder, provides=IViewQuery)
+class SearchFolderQuery(ViewQuery):
+    """Search folder query adapter"""
+
+    def get_params(self, context):
+        params = super(SearchFolderQuery, self).get_params(context)
+        request = check_request()
+        registry = request.registry
+        for name, adapter in registry.getAdapters((self,), IViewUserQuery):
+            for user_param in adapter.get_user_params(request):
+                params &= user_param
+        return params
+
+
+@adapter_config(name='user_search', context=SearchFolderQuery, provides=IViewUserQuery)
+class SearchFolderUserQuery(ContextAdapter):
+    """Search folder user query"""
+
+    @staticmethod
+    def get_user_params(request):
+        params = request.params
+        fulltext = params.get('user_search')
+        if fulltext:
+            catalog = get_utility(ICatalog)
+            negotiator = get_utility(INegotiator)
+            query_params = []
+            for lang in {request.registry.settings.get('pyramid.default_locale_name', 'en'),
+                         request.locale_name,
+                         negotiator.server_language} | negotiator.offered_languages:
+                index_name = 'title:{0}'.format(lang)
+                if index_name in catalog:
+                    index = catalog[index_name]
+                    if index.check_query(fulltext):
+                        query_params.append(Contains(index, ' and '.join((w + '*' for w in fulltext.split()))))
+            yield Or(*query_params)