Extended view's internal references settings to automatically exclude context from results list
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Jun 2018 15:18:58 +0200
changeset 617 2e4dbe6fa156
parent 616 ec087f6b3b9a
child 618 3c48622a2937
Extended view's internal references settings to automatically exclude context from results list
src/pyams_content/shared/view/interfaces/__init__.py
src/pyams_content/shared/view/reference.py
--- a/src/pyams_content/shared/view/interfaces/__init__.py	Mon Jun 11 15:18:14 2018 +0200
+++ b/src/pyams_content/shared/view/interfaces/__init__.py	Mon Jun 11 15:18:58 2018 +0200
@@ -161,6 +161,11 @@
                              required=True,
                              default=ALWAYS_REFERENCE_MODE)
 
+    exclude_context = Bool(title=_("Exclude context?"),
+                           description=_("If 'yes', context will be excluded from results list"),
+                           required=True,
+                           default=True)
+
 
 VIEW_THEMES_SETTINGS_KEY = 'pyams_content.view.themes'
 
--- a/src/pyams_content/shared/view/reference.py	Mon Jun 11 15:18:14 2018 +0200
+++ b/src/pyams_content/shared/view/reference.py	Mon Jun 11 15:18:58 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_sequence.interfaces import ISequentialIdInfo
 
 __docformat__ = 'restructuredtext'
 
@@ -19,12 +20,12 @@
 # import interfaces
 from hypatia.interfaces import ICatalog
 from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewInternalReferencesSettings, \
-    IViewQueryFilterExtension, VIEW_REFERENCES_SETTINGS_KEY, ALWAYS_REFERENCE_MODE
+    IViewQueryParamsExtension, IViewQueryFilterExtension, VIEW_REFERENCES_SETTINGS_KEY, ALWAYS_REFERENCE_MODE
 
 # import packages
 from hypatia.catalog import CatalogQuery
-from hypatia.query import Any
-from pyams_catalog.query import CatalogResultSet
+from hypatia.query import Any, Not, NotEq
+from pyams_catalog.query import CatalogResultSet, and_
 from pyams_content.workflow import VISIBLE_STATES
 from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
 from pyams_utils.registry import get_utility
@@ -39,6 +40,7 @@
 
     references = FieldProperty(IViewInternalReferencesSettings['references'])
     references_mode = FieldProperty(IViewInternalReferencesSettings['references_mode'])
+    exclude_context = FieldProperty(IViewInternalReferencesSettings['exclude_context'])
 
     @property
     def is_using_context(self):
@@ -53,6 +55,23 @@
                                   name='++view:references++')
 
 
+@adapter_config(name='references', context=IWfView, provides=IViewQueryParamsExtension)
+class ViewThemesQueryParamsExtension(ContextAdapter):
+    """View internal references query params extension"""
+
+    weight = 50
+
+    def get_params(self, context):
+        catalog = get_utility(ICatalog)
+        settings = IViewInternalReferencesSettings(self.context)
+        params = None
+        # check themes
+        if settings.exclude_context:
+            oid = ISequentialIdInfo(context).hex_oid
+            params = and_(params, NotEq(catalog['oid'], oid))
+        return params
+
+
 @adapter_config(name='references', context=IWfView, provides=IViewQueryFilterExtension)
 class ViewInternalReferencesQueryFilterExtension(ContextAdapter):
     """View internal references filter extension"""
@@ -65,6 +84,7 @@
             return items
         if (not items) or (settings.references_mode == ALWAYS_REFERENCE_MODE):
             catalog = get_utility(ICatalog)
-            params = Any(catalog['oid'], settings.references) & Any(catalog['workflow_state'], VISIBLE_STATES)
+            params = Any(catalog['oid'], settings.references) & \
+                     Any(catalog['workflow_state'], VISIBLE_STATES)
             items.prepend(CatalogResultSet(CatalogQuery(catalog).query(params)))
         return items