From: Christopher Allan Webber Date: Sun, 2 Oct 2011 02:27:36 +0000 (-0500) Subject: Merge remote branch 'remotes/aaronw/bug444_fix_utils_py_redux' X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b43b17fc2686f5524413a66f8e98f3ab0cc11a60;p=mediagoblin.git Merge remote branch 'remotes/aaronw/bug444_fix_utils_py_redux' Conflicts: mediagoblin/util.py --- b43b17fc2686f5524413a66f8e98f3ab0cc11a60 diff --cc mediagoblin/gmg_commands/__init__.py index 0071c65b,0071c65b..92ae840e --- a/mediagoblin/gmg_commands/__init__.py +++ b/mediagoblin/gmg_commands/__init__.py @@@ -16,7 -16,7 +16,7 @@@ import argparse --from mediagoblin import util as mg_util ++from mediagoblin.tools.common import import_component SUBCOMMAND_MAP = { @@@ -67,8 -67,8 +67,8 @@@ def main_cli() else: subparser = subparsers.add_parser(command_name) -- setup_func = mg_util.import_component(command_struct['setup']) -- exec_func = mg_util.import_component(command_struct['func']) ++ setup_func = import_component(command_struct['setup']) ++ exec_func = import_component(command_struct['func']) setup_func(subparser) diff --cc mediagoblin/tools/template.py index 00000000,c346c33d..a773ca99 mode 000000,100644..100644 --- a/mediagoblin/tools/template.py +++ b/mediagoblin/tools/template.py @@@ -1,0 -1,114 +1,116 @@@ + # GNU MediaGoblin -- federated, autonomous media hosting + # Copyright (C) 2011 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 + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # This program is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU Affero General Public License for more details. + # + # You should have received a copy of the GNU Affero General Public License + # along with this program. If not, see . + + from math import ceil + import jinja2 + from babel.localedata import exists + from babel.support import LazyProxy + 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 + + SETUP_JINJA_ENVS = {} + + def get_jinja_env(template_loader, locale): + """ + Set up the Jinja environment, + + (In the future we may have another system for providing theming; + for now this is good enough.) + """ + setup_gettext(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] + + template_env = jinja2.Environment( + loader=template_loader, autoescape=True, + extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape']) + + template_env.install_gettext_callables( + mg_globals.translations.ugettext, + mg_globals.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 + template_env.globals['fetch_messages'] = messages.fetch_messages + template_env.globals['gridify_list'] = gridify_list + template_env.globals['gridify_cursor'] = gridify_cursor + + if exists(locale): + SETUP_JINJA_ENVS[locale] = template_env + + return template_env + + # We'll store context information here when doing unit tests + TEMPLATE_TEST_CONTEXT = {} + + + def render_template(request, template_path, context): + """ + Render a template with context. + + Always inserts the request into the context, so you don't have to. + Also stores the context if we're doing unit tests. Helpful! + """ + template = request.template_env.get_template( + template_path) + context['request'] = request ++ context['csrf_token'] = render_csrf_form_token(request) + rendered = template.render(context) + + if common.TESTS_ENABLED: + TEMPLATE_TEST_CONTEXT[template_path] = context + + return rendered + + + 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)