From fa16975d2d8dbafa793e7a0a5f84c5483a923e63 Mon Sep 17 00:00:00 2001 From: amercader Date: Fri, 10 Jun 2016 13:59:18 +0100 Subject: [PATCH] Quick and dirty changes to make lib.base.render work on Flask This needs to be properly investigated, specially all the logic around caching Conflicts: ckan/lib/base.py --- ckan/lib/activity_streams.py | 12 ++++++--- ckan/lib/base.py | 51 +++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/ckan/lib/activity_streams.py b/ckan/lib/activity_streams.py index dc054e8bfd8..60b731c3fe0 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 # get_snippet_*() functions replace placeholders like {user}, {dataset}, etc. # in activity strings with HTML representations of particular users, datasets, @@ -251,5 +251,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(): + 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 7af53824962..17dc6dc49f9 100644 --- a/ckan/lib/base.py +++ b/ckan/lib/base.py @@ -7,7 +7,7 @@ import logging import time -from pylons import cache, config, session +from pylons import cache, config from pylons.controllers import WSGIController from pylons.controllers.util import abort as _abort from pylons.controllers.util import redirect_to, redirect @@ -16,6 +16,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 @@ -30,7 +31,8 @@ # These imports are for legacy usages and will be removed soon these should # be imported directly from ckan.common for internal ckan code and via the # plugins.toolkit for extensions. -from ckan.common import json, _, ungettext, c, g, request, response +from ckan.common import (json, _, ungettext, c, g, request, response, + is_flask, session) log = logging.getLogger(__name__) @@ -131,10 +133,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: @@ -151,7 +166,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 @@ -159,23 +174,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(): + 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(): + 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' %