If data is cached and uptodate, do not use ajax rendering
This commit is contained in:
parent
ac855ee9bb
commit
cb8b7cd2f4
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in New Issue