Use iterators to build query params
authorThierry Florac <thierry.florac@onf.fr>
Mon, 26 Nov 2018 10:16:32 +0100
changeset 1119 7cf3b2b7841e
parent 1118 ed551ca7c4de
child 1120 52ed8cc1c947
Use iterators to build query params
src/pyams_content/shared/view/reference.py
src/pyams_content/shared/view/theme.py
--- a/src/pyams_content/shared/view/reference.py	Mon Nov 26 09:34:08 2018 +0100
+++ b/src/pyams_content/shared/view/reference.py	Mon Nov 26 10:16:32 2018 +0100
@@ -20,7 +20,7 @@
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
 
-from pyams_catalog.query import CatalogResultSet, and_
+from pyams_catalog.query import CatalogResultSet
 from pyams_content.shared.view.interfaces import ALWAYS_REFERENCE_MODE, IViewInternalReferencesSettings, \
     IViewQueryFilterExtension, IViewQueryParamsExtension, IViewSettings, IWfView, VIEW_REFERENCES_SETTINGS_KEY, \
     ONLY_REFERENCE_MODE
@@ -59,19 +59,17 @@
 
     def get_params(self, context):
         settings = IViewInternalReferencesSettings(self.context)
-        params = None
-        # check view references mode
-        if settings.references_mode == ONLY_REFERENCE_MODE:
-            catalog = get_utility(ICatalog)
-            params = Any(catalog['oid'], settings.references)
         # check view settings
         if settings.exclude_context:
             sequence = ISequentialIdInfo(context, None)
             if sequence is not None:
                 oid = sequence.hex_oid
                 catalog = get_utility(ICatalog)
-                params = and_(params, NotEq(catalog['oid'], oid))
-        return params, settings.references_mode != ONLY_REFERENCE_MODE
+                yield NotEq(catalog['oid'], oid)
+        # check view references mode
+        if settings.references_mode == ONLY_REFERENCE_MODE:
+            catalog = get_utility(ICatalog)
+            yield Any(catalog['oid'], settings.references), False
 
 
 @adapter_config(name='references', context=IWfView, provides=IViewQueryFilterExtension)
--- a/src/pyams_content/shared/view/theme.py	Mon Nov 26 09:34:08 2018 +0100
+++ b/src/pyams_content/shared/view/theme.py	Mon Nov 26 10:16:32 2018 +0100
@@ -20,15 +20,15 @@
 from zope.intid.interfaces import IIntIds
 from zope.schema.fieldproperty import FieldProperty
 
-from pyams_content.component.theme import ICollectionsInfo
+from pyams_content.component.theme import ICollectionsInfo, ITagsManager
 from pyams_content.component.theme.interfaces import ITagsInfo, IThemesInfo
+from pyams_content.features.search import SearchFolderQuery
 from pyams_content.shared.view.interfaces import IViewCollectionsSettings, IViewQueryParamsExtension, IViewSettings, \
-    IViewTagsSettings, IViewThemesSettings, IWfView, VIEW_COLLECTIONS_SETTINGS_KEY, VIEW_TAGS_SETTINGS_KEY, \
-    VIEW_THEMES_SETTINGS_KEY
+    IViewTagsSettings, IViewThemesSettings, IViewUserQuery, IWfView, VIEW_COLLECTIONS_SETTINGS_KEY, \
+    VIEW_TAGS_SETTINGS_KEY, VIEW_THEMES_SETTINGS_KEY
+from pyams_thesaurus.interfaces.thesaurus import IThesaurus
 from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
-from pyams_utils.registry import get_utility
-
-from pyams_catalog.query import and_
+from pyams_utils.registry import get_utility, query_utility
 
 
 #
@@ -78,12 +78,31 @@
     def get_params(self, context):
         catalog = get_utility(ICatalog)
         settings = IViewTagsSettings(self.context)
-        params = None
         # check tags
         tags = settings.get_tags_index(context)
         if tags:
-            params = and_(params, Any(catalog['tags'], tags))
-        return params
+            yield Any(catalog['tags'], tags)
+
+
+@adapter_config(name='tags', context=SearchFolderQuery, provides=IViewUserQuery)
+class SearchFolderTagQuery(ContextAdapter):
+    """Search folder tags query"""
+
+    @staticmethod
+    def get_user_params(request):
+        tag = request.params.get('tag')
+        if tag:
+            manager = ITagsManager(request.root, None)
+            if manager is None:
+                raise StopIteration
+            thesaurus = query_utility(IThesaurus, name=manager.thesaurus_name)
+            if thesaurus is None:
+                raise StopIteration
+            term = thesaurus.terms.get(tag)
+            if term is not None:
+                catalog = get_utility(ICatalog)
+                intids = query_utility(IIntIds)
+                yield Any(catalog['tags'], (intids.queryId(term),))
 
 
 #
@@ -133,12 +152,10 @@
     def get_params(self, context):
         catalog = get_utility(ICatalog)
         settings = IViewThemesSettings(self.context)
-        params = None
         # check themes
         themes = settings.get_themes_index(context)
         if themes:
-            params = and_(params, Any(catalog['themes'], themes))
-        return params
+            yield Any(catalog['themes'], themes)
 
 
 #
@@ -188,9 +205,7 @@
     def get_params(self, context):
         catalog = get_utility(ICatalog)
         settings = IViewCollectionsSettings(self.context)
-        params = None
         # check collections
         collections = settings.get_collections_index(context)
         if collections:
-            params = and_(params, Any(catalog['collections'], collections))
-        return params
+            yield Any(catalog['collections'], collections)