src/pyams_content/shared/view/__init__.py
changeset 405 9c147733c02e
parent 337 9a3e4f9cc8f5
child 485 252eeec4e59f
--- 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)