src/pyams_content/shared/view/reference.py
changeset 617 2e4dbe6fa156
parent 558 d9c6b1d7fefa
child 783 e34cc04e9786
--- 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