From 99911be091bf68ed258cac277056ab46deac7605 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 5 Sep 2016 14:48:43 +0100 Subject: [PATCH] [#3229] Temporary fixes to allow templates to be rendered in Flask 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. --- ckan/lib/activity_streams.py | 12 ++++++--- ckan/lib/base.py | 48 +++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/ckan/lib/activity_streams.py b/ckan/lib/activity_streams.py index e5807b2cc3e..2a5cdf5df29 100644 --- a/ckan/lib/activity_streams.py +++ b/ckan/lib/activity_streams.py @@ -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, @@ -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)) diff --git a/ckan/lib/base.py b/ckan/lib/base.py index b5c6b07441d..477c03a1676 100644 --- a/ckan/lib/base.py +++ b/ckan/lib/base.py @@ -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 @@ -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__) @@ -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: @@ -150,7 +164,7 @@ 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 @@ -158,23 +172,23 @@ def render_template(): # 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' %