Skip to content

Commit

Permalink
Quick and dirty changes to make lib.base.render work on Flask
Browse files Browse the repository at this point in the history
This needs to be properly investigated, specially all the logic around
caching

Conflicts:
	ckan/lib/base.py
  • Loading branch information
amercader committed Jun 15, 2016
1 parent 2f2e972 commit fa16975
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 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

# get_snippet_*() functions replace placeholders like {user}, {dataset}, etc.
# in activity strings with HTML representations of particular users, datasets,
Expand Down Expand Up @@ -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))
51 changes: 33 additions & 18 deletions ckan/lib/base.py
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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:
Expand All @@ -151,31 +166,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():
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' %
Expand Down

0 comments on commit fa16975

Please sign in to comment.