Merge branch '577_denoise_video_transcoding'
[mediagoblin.git] / mediagoblin / tools / template.py
CommitLineData
ae3bc7fa 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
ae3bc7fa
AW
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
17from math import ceil
18import jinja2
19from babel.localedata import exists
620e4e1b
SS
20from werkzeug.urls import url_quote_plus
21
ae3bc7fa
AW
22from mediagoblin import mg_globals
23from mediagoblin import messages
24from mediagoblin.tools import common
6ef75af5 25from mediagoblin.tools.translate import get_gettext_translation
ce5ae8da 26from mediagoblin.meddleware.csrf import render_csrf_form_token
ae3bc7fa 27
ee91c2b8 28
ae3bc7fa
AW
29SETUP_JINJA_ENVS = {}
30
ee91c2b8 31
ae3bc7fa
AW
32def get_jinja_env(template_loader, locale):
33 """
ee91c2b8 34 Set up the Jinja environment,
ae3bc7fa
AW
35
36 (In the future we may have another system for providing theming;
37 for now this is good enough.)
38 """
6ef75af5 39 mg_globals.thread_scope.translations = get_gettext_translation(locale)
ae3bc7fa
AW
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
f1cdd278
E
46 # jinja2.StrictUndefined will give exceptions on references
47 # to undefined/unknown variables in templates.
ae3bc7fa
AW
48 template_env = jinja2.Environment(
49 loader=template_loader, autoescape=True,
f1cdd278 50 undefined=jinja2.StrictUndefined,
ae3bc7fa
AW
51 extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
52
53 template_env.install_gettext_callables(
c80982c7
JK
54 mg_globals.thread_scope.translations.ugettext,
55 mg_globals.thread_scope.translations.ungettext)
ae3bc7fa
AW
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
53bc3975 60 # ... have access to the global and app config
ae3bc7fa 61 template_env.globals['fetch_messages'] = messages.fetch_messages
6950c6c7
CAW
62 template_env.globals['app_config'] = mg_globals.app_config
63 template_env.globals['global_config'] = mg_globals.global_config
ae3bc7fa 64
620e4e1b
SS
65 template_env.filters['urlencode'] = url_quote_plus
66
ae3bc7fa
AW
67 if exists(locale):
68 SETUP_JINJA_ENVS[locale] = template_env
69
70 return template_env
71
ee91c2b8 72
ae3bc7fa
AW
73# We'll store context information here when doing unit tests
74TEMPLATE_TEST_CONTEXT = {}
75
76
77def 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
71c6c432
E
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)
ae3bc7fa 90 rendered = template.render(context)
ee91c2b8
CAW
91
92 if common.TESTS_ENABLED:
ae3bc7fa
AW
93 TEMPLATE_TEST_CONTEXT[template_path] = context
94
95 return rendered
96
97
98def clear_test_template_context():
99 global TEMPLATE_TEST_CONTEXT
100 TEMPLATE_TEST_CONTEXT = {}