Skip to content

Commit

Permalink
[#3229] Temporary fixes to allow templates to be rendered in Flask
Browse files Browse the repository at this point in the history
There is one action (`dashboard_activity_list_html`) that calls the
template rendering logic. This needs proper work to cleanup and be made
compatible with both Flask and Pylons, but given that this will be the
only instance of a template being rendered in Flask, it makes sense to
put this temporary changes in place until the whole template rendering
logic can be sorted out properly.
  • Loading branch information
amercader committed Sep 5, 2016
1 parent 308758c commit 99911be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
12 changes: 9 additions & 3 deletions ckan/lib/activity_streams.py
Expand Up @@ -8,7 +8,7 @@
import ckan.lib.base as base
import ckan.logic as logic

from ckan.common import _
from ckan.common import _, is_flask_request

# get_snippet_*() functions replace placeholders like {user}, {dataset}, etc.
# in activity strings with HTML representations of particular users, datasets,
Expand Down Expand Up @@ -252,5 +252,11 @@ def activity_list_to_html(context, activity_stream, extra_vars):
'timestamp': activity['timestamp'],
'is_new': activity.get('is_new', False)})
extra_vars['activities'] = activity_list
return literal(base.render('activity_streams/activity_stream_items.html',
extra_vars=extra_vars))

# TODO: Do this properly without having to check if it's Flask or not
if is_flask_request():
return base.render('activity_streams/activity_stream_items.html',
extra_vars=extra_vars)
else:
return literal(base.render('activity_streams/activity_stream_items.html',
extra_vars=extra_vars))
48 changes: 31 additions & 17 deletions ckan/lib/base.py
Expand Up @@ -15,6 +15,7 @@
from pylons.templating import cached_template, pylons_globals
from webhelpers.html import literal

from flask import render_template as flask_render_template
import ckan.exceptions
import ckan
import ckan.lib.i18n as i18n
Expand All @@ -33,7 +34,7 @@
# be imported directly from ckan.common for internal ckan code and via the
# plugins.toolkit for extensions.
from ckan.common import (json, _, ungettext, c, request, response, config,
session)
session, is_flask_request)

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -130,10 +131,23 @@ def render_template():
del globs['config']
return render_jinja2(template_name, globs)

if 'Pragma' in response.headers:
del response.headers["Pragma"]
def set_pylons_response_headers(allow_cache):
if 'Pragma' in response.headers:
del response.headers["Pragma"]
if allow_cache:
response.headers["Cache-Control"] = "public"
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.
response.headers["Cache-Control"] = "private"

# Caching Logic

## Caching Logic
allow_cache = True
# Force cache or not if explicit.
if cache_force is not None:
Expand All @@ -150,31 +164,31 @@ def render_template():
# 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.
# 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:
response.headers["Cache-Control"] = "public"
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.
response.headers["Cache-Control"] = "private"
# TODO: replicate this logic in Flask once we start looking at the
# rendering for the frontend controllers
if not is_flask_request():
set_pylons_response_headers(allow_cache)

if not allow_cache:
# Prevent any further rendering from being cached.
request.environ['__no_cache__'] = True

# Render Time :)
try:
return cached_template(template_name, render_template)
# TODO: investigate and test this properly
if is_flask_request():
return flask_render_template(template_name)
else:
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' %
Expand Down

0 comments on commit 99911be

Please sign in to comment.