X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=mediagoblin%2Ftools%2Ftemplate.py;h=d9c6e654d2e3fa7d27a5d3b3ac696afb022dfa85;hb=2783c659880372601d603157b17e69cab96b6b4e;hp=905a36df8fb920048f239fd0c299cca98805fde2;hpb=817066506d846d06d0489dfb42b8200b86f61153;p=mediagoblin.git diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py index 905a36df..d9c6e654 100644 --- a/mediagoblin/tools/template.py +++ b/mediagoblin/tools/template.py @@ -1,5 +1,5 @@ # GNU MediaGoblin -- federated, autonomous media hosting -# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS. +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by @@ -17,11 +17,13 @@ from math import ceil import jinja2 from babel.localedata import exists +from werkzeug.urls import url_quote_plus + from mediagoblin import mg_globals from mediagoblin import messages from mediagoblin.tools import common -from mediagoblin.tools.translate import setup_gettext -from mediagoblin.middleware.csrf import render_csrf_form_token +from mediagoblin.tools.translate import get_gettext_translation +from mediagoblin.meddleware.csrf import render_csrf_form_token SETUP_JINJA_ENVS = {} @@ -34,27 +36,33 @@ def get_jinja_env(template_loader, locale): (In the future we may have another system for providing theming; for now this is good enough.) """ - setup_gettext(locale) + mg_globals.thread_scope.translations = get_gettext_translation(locale) # If we have a jinja environment set up with this locale, just # return that one. if SETUP_JINJA_ENVS.has_key(locale): return SETUP_JINJA_ENVS[locale] + # jinja2.StrictUndefined will give exceptions on references + # to undefined/unknown variables in templates. template_env = jinja2.Environment( loader=template_loader, autoescape=True, + undefined=jinja2.StrictUndefined, extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape']) template_env.install_gettext_callables( - mg_globals.translations.ugettext, - mg_globals.translations.ungettext) + mg_globals.thread_scope.translations.ugettext, + mg_globals.thread_scope.translations.ungettext) # All templates will know how to ... # ... fetch all waiting messages and remove them from the queue # ... construct a grid of thumbnails or other media + # ... have access to the global and app config template_env.globals['fetch_messages'] = messages.fetch_messages - template_env.globals['gridify_list'] = gridify_list - template_env.globals['gridify_cursor'] = gridify_cursor + template_env.globals['app_config'] = mg_globals.app_config + template_env.globals['global_config'] = mg_globals.global_config + + template_env.filters['urlencode'] = url_quote_plus if exists(locale): SETUP_JINJA_ENVS[locale] = template_env @@ -76,7 +84,9 @@ def render_template(request, template_path, context): template = request.template_env.get_template( template_path) context['request'] = request - context['csrf_token'] = render_csrf_form_token(request) + rendered_csrf_token = render_csrf_form_token(request) + if rendered_csrf_token is not None: + context['csrf_token'] = render_csrf_form_token(request) rendered = template.render(context) if common.TESTS_ENABLED: @@ -88,32 +98,3 @@ def render_template(request, template_path, context): def clear_test_template_context(): global TEMPLATE_TEST_CONTEXT TEMPLATE_TEST_CONTEXT = {} - - -def gridify_list(this_list, num_cols=5): - """ - Generates a list of lists where each sub-list's length depends on - the number of columns in the list - """ - grid = [] - - # Figure out how many rows we should have - num_rows = int(ceil(float(len(this_list)) / num_cols)) - - for row_num in range(num_rows): - slice_min = row_num * num_cols - slice_max = (row_num + 1) * num_cols - - row = this_list[slice_min:slice_max] - - grid.append(row) - - return grid - - -def gridify_cursor(this_cursor, num_cols=5): - """ - Generates a list of lists where each sub-list's length depends on - the number of columns in the list - """ - return gridify_list(list(this_cursor), num_cols)