Added property to views to only select contents stored inside context
authorThierry Florac <tflorac@ulthar.net>
Wed, 28 Nov 2018 09:39:16 +0100
changeset 1134 35e8b9cd9070
parent 1133 52a1a1db5ec5
child 1135 af500b35394c
Added property to views to only select contents stored inside context
src/pyams_content/shared/view/__init__.py
src/pyams_content/shared/view/interfaces.py
src/pyams_content/shared/view/zmi/properties.py
--- a/src/pyams_content/shared/view/__init__.py	Tue Nov 27 17:21:47 2018 +0100
+++ b/src/pyams_content/shared/view/__init__.py	Wed Nov 28 09:39:16 2018 +0100
@@ -18,7 +18,7 @@
 
 from hypatia.catalog import CatalogQuery
 from hypatia.interfaces import ICatalog
-from hypatia.query import Any, Gt, Lt
+from hypatia.query import Any, Gt, Lt, Eq
 from pyramid.events import subscriber
 from pyramid.threadlocal import get_current_registry
 from zope.interface import implementer, provider
@@ -66,6 +66,7 @@
     handle_header = False
     handle_description = False
 
+    select_context_path = FieldProperty(IWfView['select_context_path'])
     select_context_type = FieldProperty(IWfView['select_context_type'])
     selected_content_types = FieldProperty(IWfView['selected_content_types'])
     select_context_datatype = FieldProperty(IWfView['select_context_datatype'])
@@ -76,7 +77,7 @@
 
     @property
     def is_using_context(self):
-        if self.select_context_type:
+        if self.select_context_path or self.select_context_type:
             return True
         registry = get_global_registry()
         for name, adapter in registry.getAdapters((self,), IViewSettings):
@@ -86,6 +87,11 @@
                 return True
         return False
 
+    def get_content_path(self, context):
+        if self.select_context_path:
+            intids = get_utility(IIntIds)
+            return intids.queryId(context)
+
     def get_content_types(self, context):
         content_types = set()
         if self.select_context_type and IWfSharedContent.providedBy(context):
@@ -104,8 +110,8 @@
             data_types |= set(self.selected_datatypes)
         return list(data_types)
 
-    def get_results(self, context, sort_index=None, reverse=None,
-                    limit=None, start=0, length=999, ignore_cache=False, get_count=False):
+    def get_results(self, context, sort_index=None, reverse=None, limit=None,
+                    start=0, length=999, ignore_cache=False, get_count=False):
         results = _MARKER
         if not ignore_cache:
             # check for cache
@@ -191,6 +197,10 @@
         # activate search
         if do_search:
             params &= Gt(catalog['push_end_date'], now)
+            # check content path
+            content_path = view.get_content_path(context)
+            if content_path is not None:
+                params &= Eq(catalog['parents'], content_path)
             # check content types
             content_types = view.get_content_types(context)
             if content_types:
--- a/src/pyams_content/shared/view/interfaces.py	Tue Nov 27 17:21:47 2018 +0100
+++ b/src/pyams_content/shared/view/interfaces.py	Wed Nov 28 09:39:16 2018 +0100
@@ -70,6 +70,14 @@
 class IWfView(IWfSharedContent):
     """View interface"""
 
+    select_context_path = Bool(title=_("Select context path?"),
+                               description=_("If 'yes', only contents located inside context will be selected"),
+                               required=True,
+                               default=False)
+
+    def get_content_path(self, context):
+        """Get context path internal ID"""
+
     select_context_type = Bool(title=_("Select context type?"),
                                description=_("If 'yes', content type will be extracted from context"),
                                required=True,
--- a/src/pyams_content/shared/view/zmi/properties.py	Tue Nov 27 17:21:47 2018 +0100
+++ b/src/pyams_content/shared/view/zmi/properties.py	Wed Nov 28 09:39:16 2018 +0100
@@ -43,9 +43,9 @@
     legend = _("Main view settings")
     fieldset_class = 'bordered no-x-margin margin-y-10'
 
-    fields = field.Fields(IWfView).select('select_context_type', 'selected_content_types',
-                                          'select_context_datatype', 'selected_datatypes',
-                                          'order_by', 'reversed_order', 'limit')
+    fields = field.Fields(IWfView).select('select_context_path', 'select_context_type',
+                                          'selected_content_types', 'select_context_datatype',
+                                          'selected_datatypes', 'order_by', 'reversed_order', 'limit')
     fields['selected_datatypes'].widgetFactory = HiddenSelect2FieldWidget
 
     weight = 1