Updated advanced search engine to be able to use search adapters
authorThierry Florac <tflorac@ulthar.net>
Sun, 19 Jul 2020 02:10:25 +0200
changeset 1407 85e615286c7a
parent 1406 a568b9d2f7dd
child 1408 916c81459d6b
Updated advanced search engine to be able to use search adapters
src/pyams_content/root/zmi/search.py
src/pyams_content/shared/common/zmi/search.py
--- a/src/pyams_content/root/zmi/search.py	Fri Jul 17 09:02:17 2020 +0200
+++ b/src/pyams_content/root/zmi/search.py	Sun Jul 19 02:10:25 2020 +0200
@@ -10,11 +10,10 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
-
 from hypatia.catalog import CatalogQuery
 from hypatia.interfaces import ICatalog
 from hypatia.query import Any, Contains, Eq, Ge, Le
+from pyramid.decorator import reify
 from pyramid.view import view_config
 from z3c.form import field
 from z3c.table.interfaces import IValues
@@ -31,6 +30,7 @@
 from pyams_content.shared.common.interfaces import CONTENT_TYPES_VOCABULARY
 from pyams_content.shared.common.interfaces.zmi import ISiteRootDashboardTable
 from pyams_content.zmi.interfaces import IAllContentsMenu
+from pyams_form.interfaces.form import ISearchFormFactory, ISearchFormResultsFactory
 from pyams_form.search import ISearchFields, SearchForm, SearchResultsView, SearchView
 from pyams_i18n.interfaces import INegotiator
 from pyams_pagelet.interfaces import PageletCreatedEvent
@@ -56,6 +56,9 @@
 from pyams_zmi.layer import IAdminLayer
 from pyams_zmi.view import AdminView
 
+
+__docformat__ = 'restructuredtext'
+
 from pyams_content import _
 
 
@@ -97,8 +100,10 @@
     def data_attributes(self):
         attributes = super(SiteRootQuickSearchResults, self).data_attributes
         attributes['table'] = {
-            'data-ams-datatable-sorting': '{0},{1}'.format(len(self.columns) - 1, self.dt_sort_order),
-            'data-ams-datatable-display-length': IAdminProfile(self.request.principal).table_page_length
+            'data-ams-datatable-sorting':
+                '{0},{1}'.format(len(self.columns) - 1, self.dt_sort_order),
+            'data-ams-datatable-display-length':
+                IAdminProfile(self.request.principal).table_page_length
         }
         return attributes
 
@@ -122,19 +127,20 @@
                          Any(catalog['content_type'], CONTENT_TYPES.keys())
                 query_params = Eq(catalog['oid'], sequence.get_full_oid(query))
                 negotiator = get_utility(INegotiator)
-                for lang in {self.request.registry.settings.get('pyramid.default_locale_name', 'en'),
+                for lang in {self.request.registry.settings.get('pyramid.default_locale_name',
+                                                                'en'),
                              self.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(query):
-                            query_params |= Contains(index, ' and '.join((w+'*' for w in query.split())))
+                            query_params |= Contains(index,
+                                                     ' and '.join((w+'*' for w in query.split())))
                 params &= query_params
             return unique(map(get_last_version,
-                              CatalogResultSet(CatalogQuery(catalog).query(params,
-                                                                           sort_index='modified_date',
-                                                                           reverse=True))))
+                              CatalogResultSet(CatalogQuery(catalog).query(
+                                  params, sort_index='modified_date', reverse=True))))
         else:
             return ()
 
@@ -194,9 +200,15 @@
         request.registry.notify(PageletCreatedEvent(self))
         apply_skin(self.request, 'PyAMS admin skin')
 
-    fields = field.Fields(ISiteRootAdvancedSearchFields)
-    fields['tags'].widgetFactory = ThesaurusTermsTreeFieldWidget
-    fields['collections'].widgetFactory = ThesaurusTermsTreeFieldWidget
+    fields_interface = ISiteRootAdvancedSearchFields
+
+    @property
+    def fields(self):
+        fields = field.Fields(self.fields_interface)
+        fields['tags'].widgetFactory = ThesaurusTermsTreeFieldWidget
+        fields['collections'].widgetFactory = ThesaurusTermsTreeFieldWidget
+        return fields
+
     ajax_handler = 'advanced-search-results.html'
 
     def updateWidgets(self, prefix=None):
@@ -213,7 +225,8 @@
             widget.extract_name = manager.extract_name
 
 
-@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchForm), provides=IContentSearch)
+@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchForm),
+                provides=IContentSearch)
 class SiteRootAdvancedSearchFormSearchAdapter(ContextRequestViewAdapter):
     """Site root advanced search form search adapter"""
 
@@ -261,9 +274,8 @@
             tags = [intids.register(term) for term in data['collections']]
             params &= Any(catalog['collections'], tags)
         return unique(map(get_last_version,
-                          CatalogResultSet(CatalogQuery(catalog).query(params,
-                                                                       sort_index='modified_date',
-                                                                       reverse=True))))
+                          CatalogResultSet(CatalogQuery(catalog).query(
+                              params, sort_index='modified_date', reverse=True))))
 
 
 @pagelet_config(name='advanced-search.html', context=ISiteRoot, layer=IPyAMSLayer,
@@ -274,7 +286,8 @@
     search_form_factory = SiteRootAdvancedSearchForm
 
 
-@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchView), provides=IPageHeader)
+@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchView),
+                provides=IPageHeader)
 class SiteRootAdvancedSearchHeaderAdapter(DefaultPageHeaderAdapter):
     """Site root advanced search header adapter"""
 
@@ -300,21 +313,34 @@
         super(SiteRootAdvancedSearchResultsView, self).__init__(context, request)
         request.registry.notify(PageletCreatedEvent(self))
 
+    @reify
+    def search_form(self):
+        form = self.request.registry.queryMultiAdapter((self.context, self.request, self),
+                                                       ISearchFormResultsFactory)
+        if form is None:
+            form = self.request.registry.queryMultiAdapter((self.context, self.request, self),
+                                                           ISearchFormFactory)
+        if form is None:
+            form = self.search_form_factory(self.context, self.request)
+        return form
+
     @property
     def data_attributes(self):
         attributes = super(SiteRootAdvancedSearchResultsView, self).data_attributes
         attributes['table'] = {
-            'data-ams-datatable-sorting': "{0},{1}".format(len(self.columns) - 1, self.dt_sort_order),
-            'data-ams-datatable-display-length': IAdminProfile(self.request.principal).table_page_length
+            'data-ams-datatable-sorting':
+                "{0},{1}".format(len(self.columns) - 1, self.dt_sort_order),
+            'data-ams-datatable-display-length':
+                IAdminProfile(self.request.principal).table_page_length
         }
         return attributes
 
 
-@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchResultsView), provides=IValues)
+@adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchResultsView),
+                provides=IValues)
 class SearchResultsViewValuesAdapter(ContextRequestViewAdapter):
     """Search results view values adapter"""
 
     @property
     def values(self):
-        form = self.view.search_form_factory(self.context, self.request)
-        return form.get_search_results() or ()
+        return self.view.search_form.get_search_results() or ()
--- a/src/pyams_content/shared/common/zmi/search.py	Fri Jul 17 09:02:17 2020 +0200
+++ b/src/pyams_content/shared/common/zmi/search.py	Sun Jul 19 02:10:25 2020 +0200
@@ -13,6 +13,7 @@
 from hypatia.catalog import CatalogQuery
 from hypatia.interfaces import ICatalog
 from hypatia.query import Any, Contains, Eq, Ge, Le
+from pyramid.decorator import reify
 from pyramid.view import view_config
 from z3c.form import field
 from z3c.table.interfaces import IValues
@@ -31,6 +32,7 @@
 from pyams_content.shared.common.interfaces.types import DATA_TYPES_VOCABULARY, ITypedSharedTool
 from pyams_content.shared.common.interfaces.zmi import ISharedToolDashboardTable
 from pyams_content.zmi.interfaces import IAllContentsMenu
+from pyams_form.interfaces.form import ISearchFormFactory, ISearchFormResultsFactory
 from pyams_form.search import ISearchFields, SearchForm, SearchResultsView, SearchView
 from pyams_i18n.interfaces import INegotiator
 from pyams_pagelet.interfaces import PageletCreatedEvent
@@ -220,10 +222,12 @@
         request.registry.notify(PageletCreatedEvent(self))
         apply_skin(self.request, 'PyAMS admin skin')
 
+    fields_interface = ISharedToolAdvancedSearchFields
+
     @property
     def fields(self):
         workflow = IWorkflow(self.context)
-        fields = field.Fields(ISharedToolAdvancedSearchFields)
+        fields = field.Fields(self.fields_interface)
         fields['status'].vocabulary = workflow.states
         if not ITypedSharedTool.providedBy(self.context):
             fields = fields.omit('data_type')
@@ -352,6 +356,17 @@
         super(SharedToolAdvancedSearchResultsView, self).__init__(context, request)
         request.registry.notify(PageletCreatedEvent(self))
 
+    @reify
+    def search_form(self):
+        form = self.request.registry.queryMultiAdapter((self.context, self.request, self),
+                                                       ISearchFormResultsFactory)
+        if form is None:
+            form = self.request.registry.queryMultiAdapter((self.context, self.request, self),
+                                                           ISearchFormFactory)
+        if form is None:
+            form = self.search_form_factory(self.context, self.request)
+        return form
+
     @property
     def data_attributes(self):
         attributes = super(SharedToolAdvancedSearchResultsView, self).data_attributes
@@ -371,5 +386,4 @@
 
     @property
     def values(self):
-        form = self.view.search_form_factory(self.context, self.request)
-        return form.get_search_results() or ()
+        return self.view.search_form.get_search_results() or ()