From cb8b7cd2f42e7f8837f8880b07ecc0e90a0334cd Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 26 Jan 2015 15:12:10 +0100 Subject: [PATCH] If data is cached and uptodate, do not use ajax rendering --- src/cmsplugin_blurp/renderers/base.py | 3 ++ src/cmsplugin_blurp/renderers/data_source.py | 44 +++++++++++++++++--- src/cmsplugin_blurp/utils.py | 2 +- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/cmsplugin_blurp/renderers/base.py b/src/cmsplugin_blurp/renderers/base.py index ae5e85b..5c4f327 100644 --- a/src/cmsplugin_blurp/renderers/base.py +++ b/src/cmsplugin_blurp/renderers/base.py @@ -40,3 +40,6 @@ class BaseRenderer(object): def render_template(self): '''Return a template path or a Template object''' pass + + def use_ajax(self, context): + return self.config.get('ajax', False) diff --git a/src/cmsplugin_blurp/renderers/data_source.py b/src/cmsplugin_blurp/renderers/data_source.py index 183ad6a..5a7a765 100644 --- a/src/cmsplugin_blurp/renderers/data_source.py +++ b/src/cmsplugin_blurp/renderers/data_source.py @@ -56,6 +56,7 @@ class Renderer(template.TemplateRenderer): } ''' + __sources = None @classmethod def check_config(cls, config): @@ -80,12 +81,17 @@ class Renderer(template.TemplateRenderer): yield 'missing signature_key string' def get_sources(self, context): - for source in self.config['sources']: - slug = '{0}.{1}'.format(self.slug, source['slug']) - data = Data(slug, self.config, source, context) - yield source['slug'], data - if settings.TEMPLATE_DEBUG: - yield 'blurp_debug__', '\n'.join(self.debug_content(context)) + if self.__sources is None: + sources = [] + for source in self.config['sources']: + slug = '{0}.{1}'.format(self.slug, source['slug']) + data = Data(slug, self.config, source, context) + sources.append((source['slug'], data)) + if settings.TEMPLATE_DEBUG: + sources.append(('blurp_debug__', + '\n'.join(self.debug_content(context)))) + self.__sources = sources + return self.__sources def debug_content(self, context): try: @@ -102,6 +108,15 @@ class Renderer(template.TemplateRenderer): except Exception, e: yield u'slug {0!r}: pformat failed {1!r}'.format(slug, e) + def use_ajax(self, context): + '''Only use ajax if some content is not cached''' + if super(Renderer, self).use_ajax(context): + for source in self.get_sources(): + if not source.content_is_cached(): + return False + else: + return False + def render(self, context): for slug, source in self.get_sources(context): context[slug] = source @@ -276,6 +291,23 @@ class Data(object): UPDATE_THREADS = {} CONDITIONS = {} + def content_is_cached(self, ignore_stale_content=True): + '''Test if some content is in cache''' + if self.__content is self.__CACHE_SENTINEL: + content, until = cache.get(self.key, (self.__CACHE_SENTINEL, None)) + if not ignore_stale_content: + self.__content = content + if until is not None and ignore_stale_content and until < self.now: + return False + if self.refresh <= 0: + return False + if self.request and 'updatecache' in self.request.GET: + return False + self.__content = content + return True + else: + return True + def get_content(self): if self.__content is not self.__CACHE_SENTINEL: return self.__content diff --git a/src/cmsplugin_blurp/utils.py b/src/cmsplugin_blurp/utils.py index d6337fd..2b4c47e 100644 --- a/src/cmsplugin_blurp/utils.py +++ b/src/cmsplugin_blurp/utils.py @@ -45,7 +45,7 @@ def render_blurp(context, slug, ajax=True): logger.warning('renderer %s does not exist', slug) return None, None # Ajax rendering is canceled in the ajax view - ajax = ajax and renderer.config.get('ajax', False) + ajax = ajax and renderer.use_ajax(context) if ajax: request = context.get('request') context['slug'] = renderer.slug