Add some simple collection test.
[mediagoblin.git] / mediagoblin / tools / template.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 from math import ceil
18 import jinja2
19 from babel.localedata import exists
20 from werkzeug.urls import url_quote_plus
21
22 from mediagoblin import mg_globals
23 from mediagoblin import messages
24 from mediagoblin.tools import common
25 from mediagoblin.tools.translate import get_gettext_translation
26 from mediagoblin.meddleware.csrf import render_csrf_form_token
27
28
29 SETUP_JINJA_ENVS = {}
30
31
32 def get_jinja_env(template_loader, locale):
33 """
34 Set up the Jinja environment,
35
36 (In the future we may have another system for providing theming;
37 for now this is good enough.)
38 """
39 mg_globals.thread_scope.translations = get_gettext_translation(locale)
40
41 # If we have a jinja environment set up with this locale, just
42 # return that one.
43 if SETUP_JINJA_ENVS.has_key(locale):
44 return SETUP_JINJA_ENVS[locale]
45
46 # jinja2.StrictUndefined will give exceptions on references
47 # to undefined/unknown variables in templates.
48 template_env = jinja2.Environment(
49 loader=template_loader, autoescape=True,
50 undefined=jinja2.StrictUndefined,
51 extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
52
53 template_env.install_gettext_callables(
54 mg_globals.thread_scope.translations.ugettext,
55 mg_globals.thread_scope.translations.ungettext)
56
57 # All templates will know how to ...
58 # ... fetch all waiting messages and remove them from the queue
59 # ... construct a grid of thumbnails or other media
60 # ... have access to the global and app config
61 template_env.globals['fetch_messages'] = messages.fetch_messages
62 template_env.globals['app_config'] = mg_globals.app_config
63 template_env.globals['global_config'] = mg_globals.global_config
64
65 template_env.filters['urlencode'] = url_quote_plus
66
67 if exists(locale):
68 SETUP_JINJA_ENVS[locale] = template_env
69
70 return template_env
71
72
73 # We'll store context information here when doing unit tests
74 TEMPLATE_TEST_CONTEXT = {}
75
76
77 def render_template(request, template_path, context):
78 """
79 Render a template with context.
80
81 Always inserts the request into the context, so you don't have to.
82 Also stores the context if we're doing unit tests. Helpful!
83 """
84 template = request.template_env.get_template(
85 template_path)
86 context['request'] = request
87 rendered_csrf_token = render_csrf_form_token(request)
88 if rendered_csrf_token is not None:
89 context['csrf_token'] = render_csrf_form_token(request)
90 rendered = template.render(context)
91
92 if common.TESTS_ENABLED:
93 TEMPLATE_TEST_CONTEXT[template_path] = context
94
95 return rendered
96
97
98 def clear_test_template_context():
99 global TEMPLATE_TEST_CONTEXT
100 TEMPLATE_TEST_CONTEXT = {}