Skip to content

Commit

Permalink
lib.base.render() cleanups and remove template caching
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Mar 19, 2012
1 parent 33a7544 commit 0a0b595
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions ckan/lib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def render_template():
globs.update(pylons_globals())
globs['actions'] = model.Action

# Using pylons.url() directly destroys the
# localisation stuff so we remove it so any bad templates crash
# and burn
# Using pylons.url() directly destroys the localisation stuff so
# we remove it so any bad templates crash and burn
del globs['url']

template = globs['app_globals'].genshi_loader.load(template_name,
Expand All @@ -71,56 +70,47 @@ def render_template():
if 'Pragma' in response.headers:
del response.headers["Pragma"]

if asbool(config.get('ckan.template_cache_enabled')):
allow_cache = True
# force cache or not if explicit
if cache_force is not None:
allow_cache = cache_force
# do not allow caching of pages for logged in users/flash messages etc
elif session.last_accessed:
allow_cache = False
# don't cache if based on a non-cachable template used in this
elif request.environ.get('__no_cache__'):
allow_cache = False
# don't cache if we have set the __no_cache__ param in the query string
elif request.params.get('__no_cache__'):
allow_cache = False
# don't cache if we have extra vars containing data
elif extra_vars:
for k, v in extra_vars.iteritems():
allow_cache = False
break
else:
## Caching Logic
allow_cache = True
# Force cache or not if explicit.
if cache_force is not None:
allow_cache = cache_force
# Do not allow caching of pages for logged in users/flash messages etc.
elif session.last_accessed:
allow_cache = False
# Tests etc.
elif 'REMOTE_USER' in request.environ:
allow_cache = False
# Don't cache if based on a non-cachable template used in this.
elif request.environ.get('__no_cache__'):
allow_cache = False
# Don't cache if we have set the __no_cache__ param in the query string.
elif request.params.get('__no_cache__'):
allow_cache = False
# Don't cache if we have extra vars containing data.
elif extra_vars:
for k, v in extra_vars.iteritems():
allow_cache = False
break
# Record cachability for the page cache if enabled
request.environ['CKAN_PAGE_CACHABLE'] = allow_cache

if allow_cache:
request.environ['CKAN_PAGE_CACHABLE'] = True
response.headers["Cache-Control"] = "public"

if cache_expire is None:
try:
cache_expire = int(config.get('ckan.cache_expires'))
cache_type = config.get('ckan.cache_type', 'memory')
except ValueError:
pass
if cache_expire is not None:
response.headers["Cache-Control"] += ",max-age=%s, must-revalidate" % cache_expire
if cache_expire or cache_type or cache_key:
if cache_key:
cache_key += '_' + h.lang()
else:
cache_key = h.lang()
cache_key += '_?_' + request.environ.get('QUERY_STRING', '')
try:
cache_expire = int(config.get('ckan.cache_expires', 0))
response.headers["Cache-Control"] += ", max-age=%s, must-revalidate" % cache_expire
except ValueError:
pass
else:
# we do not want caching
# We do not want caching.
response.headers["Cache-Control"] = "private"
cache_key = cache_type = cache_expire = None
# Prevent any further rendering from being cached.
request.environ['__no_cache__'] = True

# render time :)
# Render Time :)
try:
return cached_template(template_name, render_template, cache_key=cache_key,
cache_type=cache_type, cache_expire=cache_expire)
return cached_template(template_name, render_template)
except ckan.exceptions.CkanUrlException, e:
raise ckan.exceptions.CkanUrlException('\nAn Exception has been raised for template %s\n%s'
% (template_name, e.message))
Expand Down

0 comments on commit 0a0b595

Please sign in to comment.