Added views marker interface. Updated cache management for views not using context.
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 16:43:14 +0100
changeset 337 9a3e4f9cc8f5
parent 336 d9f37252a42d
child 338 f37b5995a48c
Added views marker interface. Updated cache management for views not using context.
src/pyams_content/shared/view/__init__.py
src/pyams_content/shared/view/interfaces/__init__.py
src/pyams_content/shared/view/reference.py
src/pyams_content/shared/view/theme.py
--- 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)
--- a/src/pyams_content/shared/view/interfaces/__init__.py	Fri Jan 26 16:42:19 2018 +0100
+++ b/src/pyams_content/shared/view/interfaces/__init__.py	Fri Jan 26 16:43:14 2018 +0100
@@ -73,6 +73,8 @@
                 description=_("Maximum number of results that the view may retrieve"),
                 required=False)
 
+    is_using_context = Attribute("Check if view is using context settings")
+
     def get_results(self, context):
         """Get results of catalog query"""
 
@@ -81,6 +83,12 @@
     """Workflow managed view interface"""
 
 
+class IViewSettings(Interface):
+    """Base interface for view settings adapters"""
+
+    is_using_context = Attribute("Check if view settings are using context")
+
+
 class IViewQuery(Interface):
     """View query interface"""
 
@@ -128,7 +136,7 @@
                                                 for v, t in REFERENCES_MODES.items()])
 
 
-class IViewInternalReferencesSettings(IInternalReferencesList):
+class IViewInternalReferencesSettings(IViewSettings, IInternalReferencesList):
     """View internal references settings"""
 
     references_mode = Choice(title=_("Internal references usage"),
@@ -141,7 +149,7 @@
 VIEW_THEMES_SETTINGS_KEY = 'pyams_content.view.themes'
 
 
-class IViewThemesSettings(Interface):
+class IViewThemesSettings(IViewSettings):
     """View themess ettings"""
 
     select_context_themes = Bool(title=_("Select context themes?"),
--- a/src/pyams_content/shared/view/reference.py	Fri Jan 26 16:42:19 2018 +0100
+++ b/src/pyams_content/shared/view/reference.py	Fri Jan 26 16:43:14 2018 +0100
@@ -18,8 +18,8 @@
 
 # import interfaces
 from hypatia.interfaces import ICatalog
-from pyams_content.shared.view.interfaces import IViewInternalReferencesSettings, IWfView, VIEW_REFERENCES_SETTINGS_KEY, \
-    IViewQueryFilterExtension, ALWAYS_REFERENCE_MODE
+from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewInternalReferencesSettings, \
+    IViewQueryFilterExtension, VIEW_REFERENCES_SETTINGS_KEY, ALWAYS_REFERENCE_MODE
 from zope.annotation.interfaces import IAnnotations
 
 # import packages
@@ -44,8 +44,13 @@
     references = FieldProperty(IViewInternalReferencesSettings['references'])
     references_mode = FieldProperty(IViewInternalReferencesSettings['references_mode'])
 
+    @property
+    def is_using_context(self):
+        return False
+
 
 @adapter_config(context=IWfView, provides=IViewInternalReferencesSettings)
+@adapter_config(name='references', context=IWfView, provides=IViewSettings)
 def ViewInternalReferencesSettingsFactory(view):
     """View internal references settings factory"""
     annotations = IAnnotations(view)
--- a/src/pyams_content/shared/view/theme.py	Fri Jan 26 16:42:19 2018 +0100
+++ b/src/pyams_content/shared/view/theme.py	Fri Jan 26 16:43:14 2018 +0100
@@ -17,7 +17,7 @@
 
 # import interfaces
 from pyams_content.component.theme.interfaces import IThemesInfo
-from pyams_content.shared.view.interfaces import IViewThemesSettings, IWfView, VIEW_THEMES_SETTINGS_KEY
+from pyams_content.shared.view.interfaces import IWfView, IViewSettings, IViewThemesSettings, VIEW_THEMES_SETTINGS_KEY
 from zope.annotation.interfaces import IAnnotations
 
 # import packages
@@ -38,6 +38,10 @@
     select_context_themes = FieldProperty(IViewThemesSettings['select_context_themes'])
     themes = FieldProperty(IViewThemesSettings['themes'])
 
+    @property
+    def is_using_context(self):
+        return self.select_context_themes
+
     def get_themes(self, context):
         themes = set()
         if self.select_context_themes:
@@ -53,6 +57,7 @@
 
 
 @adapter_config(context=IWfView, provides=IViewThemesSettings)
+@adapter_config(name='themes', context=IWfView, provides=IViewSettings)
 def ViewThemesSettingsFactory(view):
     """View themes settings factory"""
     annotations = IAnnotations(view)