Added collections to search engines
authorThierry Florac <tflorac@ulthar.net>
Fri, 03 Jul 2020 12:27:49 +0200
changeset 1395 8184dadefc8b
parent 1394 ed31bfceddda
child 1396 c870db363aaf
Added collections to search engines
src/pyams_content/locales/fr/LC_MESSAGES/pyams_content.mo
src/pyams_content/locales/fr/LC_MESSAGES/pyams_content.po
src/pyams_content/root/zmi/search.py
src/pyams_content/root/zmi/templates/advanced-search.pt
src/pyams_content/shared/common/zmi/search.py
Binary file src/pyams_content/locales/fr/LC_MESSAGES/pyams_content.mo has changed
--- a/src/pyams_content/locales/fr/LC_MESSAGES/pyams_content.po	Wed Jul 01 18:05:38 2020 +0200
+++ b/src/pyams_content/locales/fr/LC_MESSAGES/pyams_content.po	Fri Jul 03 12:27:49 2020 +0200
@@ -3379,11 +3379,11 @@
 
 #: src/pyams_content/features/search/zmi/manager.py:45
 msgid "Search settings..."
-msgstr "Moteur de recherche"
+msgstr "Moteurs de recherche"
 
 #: src/pyams_content/features/search/zmi/manager.py:59
 msgid "Search engine settings"
-msgstr "Paramétrage du moteur de recherche"
+msgstr "Paramétrage des moteurs de recherche"
 
 #: src/pyams_content/features/search/zmi/manager.py:74
 msgid "Enable search by tag"
--- a/src/pyams_content/root/zmi/search.py	Wed Jul 01 18:05:38 2020 +0200
+++ b/src/pyams_content/root/zmi/search.py	Fri Jul 03 12:27:49 2020 +0200
@@ -23,6 +23,7 @@
 from zope.schema import Choice, Datetime, List
 
 from pyams_catalog.query import CatalogResultSet
+from pyams_content.component.theme import ICollectionsManager
 from pyams_content.component.theme.interfaces import ITagsManager
 from pyams_content.profile.interfaces import IAdminProfile
 from pyams_content.root import ISiteRoot
@@ -177,6 +178,9 @@
     tags = ThesaurusTermsListField(title=_("Tags"),
                                    required=False)
 
+    collections = ThesaurusTermsListField(title=_("Collections"),
+                                          required=False)
+
 
 @template_config(template='templates/advanced-search.pt', layer=IPyAMSLayer)
 @implementer(IInnerPage)
@@ -192,6 +196,7 @@
 
     fields = field.Fields(ISiteRootAdvancedSearchFields)
     fields['tags'].widgetFactory = ThesaurusTermsTreeFieldWidget
+    fields['collections'].widgetFactory = ThesaurusTermsTreeFieldWidget
     ajax_handler = 'advanced-search-results.html'
 
     def updateWidgets(self, prefix=None):
@@ -201,6 +206,11 @@
             manager = ITagsManager(self.request.root)
             widget.thesaurus_name = manager.thesaurus_name
             widget.extract_name = manager.extract_name
+        if 'collections' in self.widgets:
+            widget = self.widgets['collections']
+            manager = ICollectionsManager(self.request.root)
+            widget.thesaurus_name = manager.thesaurus_name
+            widget.extract_name = manager.extract_name
 
 
 @adapter_config(context=(ISiteRoot, IPyAMSLayer, SiteRootAdvancedSearchForm), provides=IContentSearch)
@@ -223,14 +233,16 @@
             else:
                 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
         if data.get('owner'):
             params &= Eq(catalog['role:owner'], data['owner'])
@@ -245,6 +257,9 @@
         if data.get('tags'):
             tags = [intids.register(term) for term in data['tags']]
             params &= Any(catalog['tags'], tags)
+        if data.get('collections'):
+            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',
--- a/src/pyams_content/root/zmi/templates/advanced-search.pt	Wed Jul 01 18:05:38 2020 +0200
+++ b/src/pyams_content/root/zmi/templates/advanced-search.pt	Fri Jul 03 12:27:49 2020 +0200
@@ -184,6 +184,13 @@
 									<input tal:replace="structure widget.render()" />
 								</div>
 							</fieldset>
+							<fieldset tal:condition="'collections' in view.widgets">
+								<legend class="switcher" i18n:translate="">Collections</legend>
+								<div class="input"
+									 tal:define="widget view.widgets['collections']">
+									<input tal:replace="structure widget.render()" />
+								</div>
+							</fieldset>
 						</fieldset>
 					</tal:loop>
 					<div class="widgets-suffix"
--- a/src/pyams_content/shared/common/zmi/search.py	Wed Jul 01 18:05:38 2020 +0200
+++ b/src/pyams_content/shared/common/zmi/search.py	Fri Jul 03 12:27:49 2020 +0200
@@ -9,10 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_content.shared.common.interfaces.types import DATA_TYPES_VOCABULARY, ITypedSharedTool
-
-
-__docformat__ = 'restructuredtext'
 
 from hypatia.catalog import CatalogQuery
 from hypatia.interfaces import ICatalog
@@ -26,11 +22,13 @@
 from zope.schema import Choice, Datetime
 
 from pyams_catalog.query import CatalogResultSet
-from pyams_content.component.theme.interfaces import ICollectionsManager, ICollectionsManagerTarget, ITagsManager, \
-    IThemesManager, IThemesManagerTarget
+from pyams_content.component.theme.interfaces import ICollectionsManager, \
+    ICollectionsManagerTarget, ITagsManager, IThemesManager, IThemesManagerTarget
 from pyams_content.profile.interfaces import IAdminProfile
 from pyams_content.shared.common import CONTENT_TYPES
-from pyams_content.shared.common.interfaces import IBaseSharedTool, SHARED_TOOL_WORKFLOW_STATES_VOCABULARY
+from pyams_content.shared.common.interfaces import IBaseSharedTool, \
+    SHARED_TOOL_WORKFLOW_STATES_VOCABULARY
+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.search import ISearchFields, SearchForm, SearchResultsView, SearchView
@@ -61,6 +59,9 @@
 from pyams_zmi.layer import IAdminLayer
 from pyams_zmi.view import AdminView
 
+
+__docformat__ = 'restructuredtext'
+
 from pyams_content import _
 
 
@@ -227,10 +228,7 @@
             fields['themes'].widgetFactory = ThesaurusTermsTreeFieldWidget
         else:
             fields = fields.omit('themes')
-        if ICollectionsManagerTarget.providedBy(self.context):
-            fields['collections'].widgetFactory = ThesaurusTermsTreeFieldWidget
-        else:
-            fields = fields.omit('collections')
+        fields['collections'].widgetFactory = ThesaurusTermsTreeFieldWidget
         return fields
 
     def updateWidgets(self, prefix=None):
@@ -247,7 +245,7 @@
             widget.extract_name = manager.extract_name
         if 'collections' in self.widgets:
             widget = self.widgets['collections']
-            manager = ICollectionsManager(self.context)
+            manager = ICollectionsManager(self.request.root)
             widget.thesaurus_name = manager.thesaurus_name
             widget.extract_name = manager.extract_name