1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
19 from babel
.localedata
import exists
20 from werkzeug
.urls
import url_quote_plus
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
32 def get_jinja_env(template_loader
, locale
):
34 Set up the Jinja environment,
36 (In the future we may have another system for providing theming;
37 for now this is good enough.)
39 mg_globals
.thread_scope
.translations
= get_gettext_translation(locale
)
41 # If we have a jinja environment set up with this locale, just
43 if SETUP_JINJA_ENVS
.has_key(locale
):
44 return SETUP_JINJA_ENVS
[locale
]
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'])
53 template_env
.install_gettext_callables(
54 mg_globals
.thread_scope
.translations
.ugettext
,
55 mg_globals
.thread_scope
.translations
.ungettext
)
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
65 template_env
.filters
['urlencode'] = url_quote_plus
68 SETUP_JINJA_ENVS
[locale
] = template_env
73 # We'll store context information here when doing unit tests
74 TEMPLATE_TEST_CONTEXT
= {}
77 def render_template(request
, template_path
, context
):
79 Render a template with context.
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!
84 template
= request
.template_env
.get_template(
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
)
92 if common
.TESTS_ENABLED
:
93 TEMPLATE_TEST_CONTEXT
[template_path
] = context
98 def clear_test_template_context():
99 global TEMPLATE_TEST_CONTEXT
100 TEMPLATE_TEST_CONTEXT
= {}