summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2015-01-26 14:12:10 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2015-03-30 10:33:46 (GMT)
commitcb8b7cd2f42e7f8837f8880b07ecc0e90a0334cd (patch)
tree07ba7132a2ef74f5c21378f834ce4389f561dee8
parentac855ee9bb465d88db5bd9369aea354eb2505f4e (diff)
downloadcmsplugin-blurp-wip/bdauvergne.zip
cmsplugin-blurp-wip/bdauvergne.tar.gz
cmsplugin-blurp-wip/bdauvergne.tar.bz2
If data is cached and uptodate, do not use ajax renderingwip/bdauvergne
-rw-r--r--src/cmsplugin_blurp/renderers/base.py3
-rw-r--r--src/cmsplugin_blurp/renderers/data_source.py44
-rw-r--r--src/cmsplugin_blurp/utils.py2
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