Added option to extract content-type from context
authorThierry Florac <thierry.florac@onf.fr>
Thu, 22 Feb 2018 10:15:00 +0100
changeset 405 9c147733c02e
parent 404 0ba2bb1a692e
child 406 5527bc086216
Added option to extract content-type from context
src/pyams_content/shared/view/__init__.py
src/pyams_content/shared/view/interfaces/__init__.py
src/pyams_content/shared/view/zmi/properties.py
--- a/src/pyams_content/shared/view/__init__.py	Wed Feb 21 17:18:04 2018 +0100
+++ b/src/pyams_content/shared/view/__init__.py	Thu Feb 22 10:15:00 2018 +0100
@@ -59,6 +59,7 @@
     content_type = VIEW_CONTENT_TYPE
     content_name = VIEW_CONTENT_NAME
 
+    select_context_type = FieldProperty(IWfView['select_context_type'])
     selected_content_types = FieldProperty(IWfView['selected_content_types'])
     order_by = FieldProperty(IWfView['order_by'])
     reversed_order = FieldProperty(IWfView['reversed_order'])
@@ -74,6 +75,14 @@
                 return True
         return False
 
+    def get_content_types(self, context):
+        content_types = set()
+        if self.select_context_type:
+            content_types.add(context.content_type)
+        if self.selected_content_types:
+            content_types |= set(self.selected_content_types)
+        return list(content_types)
+
     def get_results(self, context):
         intids = get_utility(IIntIds)
         views_cache = get_cache(VIEWS_CACHE_REGION, VIEWS_CACHE_NAME)
@@ -115,14 +124,20 @@
         view = self.context
         catalog = get_utility(ICatalog)
         registry = get_current_registry()
-        params = Any(catalog['content_type'], view.selected_content_types)
+        # check publication dates
+        now = tztime(datetime.utcnow())
+        params = Lt(catalog['effective_date'], now)
+        params &= Gt(catalog['push_end_date'], now)
+        # check content types
+        content_types = view.get_content_types(context)
+        if content_types:
+            params &= Any(catalog['content_type'], content_types)
+        # check workflow states
         wf_params = None
         for workflow in registry.getAllUtilitiesRegisteredFor(IWorkflow):
             wf_params = or_(wf_params, Any(catalog['workflow_state'], workflow.published_states))
         params &= wf_params
-        now = tztime(datetime.utcnow())
-        params &= Lt(catalog['effective_date'], now)
-        params &= Gt(catalog['push_end_date'], now)
+        # check custom extensions
         for name, adapter in sorted(registry.getAdapters((view,), IViewQueryParamsExtension),
                                     key=lambda x: x[1].weight):
             new_params = adapter.get_params(context)
--- a/src/pyams_content/shared/view/interfaces/__init__.py	Wed Feb 21 17:18:04 2018 +0100
+++ b/src/pyams_content/shared/view/interfaces/__init__.py	Thu Feb 22 10:15:00 2018 +0100
@@ -53,11 +53,19 @@
 class IWfView(IWfSharedContent):
     """View interface"""
 
+    select_context_type = Bool(title=_("Select context type?"),
+                               description=_("If 'yes', content type will be extracted from context"),
+                               required=True,
+                               default=False)
+
     selected_content_types = List(title=_("Content types"),
                                   description=_("Selected content types; leave empty for all"),
                                   value_type=Choice(vocabulary='PyAMS content types'),
                                   required=False)
 
+    def get_content_types(self, context):
+        """Get content types for given context"""
+
     order_by = Choice(title=_("Order by"),
                       description=_("Property to use to sort results"),
                       vocabulary=VIEW_ORDER_VOCABULARY,
--- a/src/pyams_content/shared/view/zmi/properties.py	Wed Feb 21 17:18:04 2018 +0100
+++ b/src/pyams_content/shared/view/zmi/properties.py	Thu Feb 22 10:15:00 2018 +0100
@@ -38,5 +38,6 @@
     legend = _("Main view settings")
     fieldset_class = 'bordered no-x-margin margin-y-10'
 
-    fields = field.Fields(IWfView).select('selected_content_types', 'order_by', 'reversed_order', 'limit')
+    fields = field.Fields(IWfView).select('select_context_type', 'selected_content_types',
+                                          'order_by', 'reversed_order', 'limit')
     weight = 1