Added arguments to ignore cache when getting view content (typically in preview mode)
authorThierry Florac <thierry.florac@onf.fr>
Wed, 04 Jul 2018 08:56:22 +0200
changeset 796 2ef3bacda742
parent 795 d282eb691486
child 797 73ced1817195
Added arguments to ignore cache when getting view content (typically in preview mode)
src/pyams_content/shared/view/__init__.py
src/pyams_content/shared/view/interfaces/__init__.py
src/pyams_content/shared/view/merge.py
src/pyams_content/shared/view/portlet/__init__.py
src/pyams_content/shared/view/portlet/zmi/templates/view-items-list-preview.pt
--- a/src/pyams_content/shared/view/__init__.py	Tue Jul 03 18:16:59 2018 +0200
+++ b/src/pyams_content/shared/view/__init__.py	Wed Jul 04 08:56:22 2018 +0200
@@ -53,6 +53,8 @@
 VIEW_CACHE_KEY = 'view_{view}'
 VIEW_CONTEXT_CACHE_KEY = 'view_{view}.context_{context}'
 
+_MARKER = object()
+
 
 @implementer(IWfView, IPreviewTarget, IReviewTarget)
 class WfView(WfSharedContent):
@@ -87,16 +89,22 @@
             content_types |= set(self.selected_content_types)
         return list(content_types)
 
-    def get_results(self, context, sort_index=None, reverse=None, limit=None):
-        views_cache = get_cache(VIEWS_CACHE_REGION, VIEWS_CACHE_NAME)
-        if self.is_using_context:
-            cache_key = VIEW_CONTEXT_CACHE_KEY.format(view=ICacheKeyValue(self),
-                                                      context=ICacheKeyValue(context))
-        else:
-            cache_key = VIEW_CACHE_KEY.format(view=ICacheKeyValue(self))
-        try:
-            results = views_cache.get_value(cache_key)
-        except KeyError:
+    def get_results(self, context, sort_index=None, reverse=None, limit=None, ignore_cache=False):
+        results = _MARKER
+        if not ignore_cache:
+            # check for cache
+            views_cache = get_cache(VIEWS_CACHE_REGION, VIEWS_CACHE_NAME)
+            if self.is_using_context:
+                cache_key = VIEW_CONTEXT_CACHE_KEY.format(view=ICacheKeyValue(self),
+                                                          context=ICacheKeyValue(context))
+            else:
+                cache_key = VIEW_CACHE_KEY.format(view=ICacheKeyValue(self))
+            try:
+                results = views_cache.get_value(cache_key)
+            except KeyError:
+                pass
+        # Execute query
+        if results is _MARKER:
             registry = get_current_registry()
             adapter = registry.queryAdapter(self, IViewQuery, name='es')
             if adapter is None:
@@ -107,8 +115,9 @@
                                           limit or self.limit)
             intids = get_utility(IIntIds)
             cache, results = tee(results)
-            views_cache.set_value(cache_key, [intids.queryId(item) for item in cache])
-            logger.debug("Storing view items to cache key {0}".format(cache_key))
+            if not ignore_cache:
+                views_cache.set_value(cache_key, [intids.queryId(item) for item in cache])
+                logger.debug("Storing view items to cache key {0}".format(cache_key))
         else:
             results = CatalogResultSet(results)
             logger.debug("Retrieving view items from cache key {0}".format(cache_key))
--- a/src/pyams_content/shared/view/interfaces/__init__.py	Tue Jul 03 18:16:59 2018 +0200
+++ b/src/pyams_content/shared/view/interfaces/__init__.py	Wed Jul 04 08:56:22 2018 +0200
@@ -89,7 +89,7 @@
 
     is_using_context = Attribute("Check if view is using context settings")
 
-    def get_results(self, context, sort_index=None, reverse=True, limit=None):
+    def get_results(self, context, sort_index=None, reverse=True, limit=None, ignore_cache=False):
         """Get results of catalog query"""
 
 
@@ -199,5 +199,5 @@
     Mergers are used to merge results of several views.
     """
 
-    def get_results(self, views, context):
+    def get_results(self, views, context, ignore_cache=False):
         """Merge results of several views together"""
--- a/src/pyams_content/shared/view/merge.py	Tue Jul 03 18:16:59 2018 +0200
+++ b/src/pyams_content/shared/view/merge.py	Wed Jul 04 08:56:22 2018 +0200
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_utils.list import random_iter
 
 __docformat__ = 'restructuredtext'
 
@@ -60,8 +59,8 @@
     label = _("Concatenate views items in order")
 
     @classmethod
-    def get_results(cls, views, context):
-        results = (view.get_results(context) for view in views)
+    def get_results(cls, views, context, ignore_cache=False):
+        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
         yield from chain(*results)
 
 
@@ -72,8 +71,8 @@
     label = _("Extract items randomly")
 
     @classmethod
-    def get_results(cls, views, context):
-        results = (view.get_results(context) for view in views)
+    def get_results(cls, views, context, ignore_cache=False):
+        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
         results = list(chain(*results))
         shuffle(results)
         yield from iter(results)
@@ -86,8 +85,8 @@
     label = _("Take items from views one by one, in views order")
 
     @classmethod
-    def get_results(cls, views, context):
-        results = (view.get_results(context) for view in views)
+    def get_results(cls, views, context, ignore_cache=False):
+        results = (view.get_results(context, ignore_cache=ignore_cache) for view in views)
         for array in zip_longest(*results):
             yield from filter(lambda x: x is not None, array)
 
@@ -99,8 +98,8 @@
     label = _("Take items from views one by one, in random order")
 
     @classmethod
-    def get_results(cls, views, context):
-        results = [view.get_results(context) for view in views]
+    def get_results(cls, views, context, ignore_cache=False):
+        results = [view.get_results(context, ignore_cache=ignore_cache) for view in views]
         shuffle(results)
         for array in zip_longest(*results):
             yield from filter(lambda x: x is not None, array)
@@ -113,10 +112,11 @@
     sort_key = None
 
     @classmethod
-    def get_results(cls, views, context):
+    def get_results(cls, views, context, ignore_cache=False):
         results = (view.get_results(context,
                                     sort_index=cls.sort_index,
-                                    reverse=True)
+                                    reverse=True,
+                                    ignore_cache=ignore_cache)
                    for view in views)
         yield from merge(*results, key=cls.sort_key)
 
--- a/src/pyams_content/shared/view/portlet/__init__.py	Tue Jul 03 18:16:59 2018 +0200
+++ b/src/pyams_content/shared/view/portlet/__init__.py	Wed Jul 04 08:56:22 2018 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_portal.interfaces import PREVIEW_MODE
 
 __docformat__ = 'restructuredtext'
 
@@ -59,21 +60,27 @@
             if view is not None:
                 yield view
 
-    def get_items(self, request=None):
+    def get_items(self, request=None, ignore_cache=False):
         if request is None:
             request = check_request()
         if self.views_context == VIEW_DISPLAY_CONTEXT:
             context = request.annotations.get(DISPLAY_CONTEXT, request.root)
         else:
             context = request.context
+        if not ignore_cache:
+            ignore_cache = request.annotations.get(PREVIEW_MODE, False)
         if len(self.views or ()) < 2:  # single view
             for view in self.get_views():
                 if view is not None:
-                    yield from islice(view.get_results(context), self.limit)
+                    yield from islice(view.get_results(context, ignore_cache=ignore_cache),
+                                      self.limit)
         else:  # several views merged together
             merger = self.get_merger(request)
             if merger is not None:
-                yield from islice(unique_iter(merger.get_results(self.get_views(), context)), self.limit)
+                yield from islice(unique_iter(merger.get_results(self.get_views(),
+                                                                 context,
+                                                                 ignore_cache=ignore_cache)),
+                                  self.limit)
 
 
 @portlet_config(permission=VIEW_PERMISSION)
--- a/src/pyams_content/shared/view/portlet/zmi/templates/view-items-list-preview.pt	Tue Jul 03 18:16:59 2018 +0200
+++ b/src/pyams_content/shared/view/portlet/zmi/templates/view-items-list-preview.pt	Wed Jul 04 08:56:22 2018 +0200
@@ -1,5 +1,5 @@
 <tal:var define="settings view.settings; global count 0;" i18n:domain="pyams_content">
-	<div tal:repeat="item settings.get_items(request)">
+	<div tal:repeat="item settings.get_items(request, ignore_cache=True)">
 		<tal:var content="i18n:item.title" />
 		<tal:var define="global count count+1" />
 	</div>