src/pyams_content/shared/view/__init__.py
changeset 337 9a3e4f9cc8f5
parent 267 9d42d0a6e945
child 405 9c147733c02e
--- a/src/pyams_content/shared/view/__init__.py	Fri Jan 26 16:42:19 2018 +0100
+++ b/src/pyams_content/shared/view/__init__.py	Fri Jan 26 16:43:14 2018 +0100
@@ -24,7 +24,7 @@
 from pyams_content.features.preview.interfaces import IPreviewTarget
 from pyams_content.features.review.interfaces import IReviewTarget
 from pyams_content.shared.view.interfaces import IView, IWfView, IViewQuery, IViewQueryParamsExtension, \
-    IViewQueryFilterExtension, VIEW_CONTENT_TYPE, VIEW_CONTENT_NAME
+    IViewQueryFilterExtension, VIEW_CONTENT_TYPE, VIEW_CONTENT_NAME, IViewSettings
 from zope.intid.interfaces import IIntIds
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
 
@@ -36,7 +36,7 @@
 from pyams_content.shared.common import WfSharedContent, register_content_type, SharedContent
 from pyams_utils.adapter import adapter_config, ContextAdapter
 from pyams_utils.list import unique
-from pyams_utils.registry import get_utility
+from pyams_utils.registry import get_utility, get_global_registry
 from pyams_utils.timezone import tztime
 from pyams_workflow.interfaces import IWorkflow
 from pyramid.events import subscriber
@@ -47,7 +47,9 @@
 
 VIEWS_CACHE_REGION = 'views'
 VIEWS_CACHE_NAME = 'PyAMS::view'
-VIEWS_CACHE_KEY = 'view_{view}.context_{context}'
+
+VIEW_CACHE_KEY = 'view_{view}'
+VIEW_CONTEXT_CACHE_KEY = 'view_{view}.context_{context}'
 
 
 @implementer(IWfView, IPreviewTarget, IReviewTarget)
@@ -62,11 +64,24 @@
     reversed_order = FieldProperty(IWfView['reversed_order'])
     limit = FieldProperty(IWfView['limit'])
 
+    @property
+    def is_using_context(self):
+        registry = get_global_registry()
+        for name, adapter in registry.getAdapters((self,), IViewSettings):
+            if not name:
+                continue
+            if adapter.is_using_context:
+                return True
+        return False
+
     def get_results(self, context):
         intids = get_utility(IIntIds)
         views_cache = get_cache(VIEWS_CACHE_REGION, VIEWS_CACHE_NAME)
-        cache_key = VIEWS_CACHE_KEY.format(view=intids.queryId(self),
-                                           context=intids.queryId(context))
+        if self.is_using_context:
+            cache_key = VIEW_CONTEXT_CACHE_KEY.format(view=intids.queryId(self),
+                                                      context=intids.queryId(context))
+        else:
+            cache_key = VIEW_CACHE_KEY.format(view=intids.queryId(self))
         try:
             results = views_cache.get_value(cache_key)
         except KeyError:
@@ -131,7 +146,13 @@
 
 
 @subscriber(IObjectModifiedEvent, context_selector=IWfView)
-def handle_modified_view(view):
+def handle_modified_view(event):
     """Invalidate views cache when a view is modified"""
+    view = event.object
     views_cache = get_cache(VIEWS_CACHE_REGION, VIEWS_CACHE_NAME)
-    views_cache.clear()
+    if view.is_using_context:
+        views_cache.clear()
+    else:
+        intids = get_utility(IIntIds)
+        cache_key = VIEW_CACHE_KEY.format(view=intids.queryId(view))
+        views_cache.remove(cache_key)