1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 mediagoblin
import mg_globals
21 from mediagoblin
import messages
22 from mediagoblin
.tools
import common
23 from mediagoblin
.tools
.translate
import setup_gettext
24 from mediagoblin
.meddleware
.csrf
import render_csrf_form_token
30 def get_jinja_env(template_loader
, locale
):
32 Set up the Jinja environment,
34 (In the future we may have another system for providing theming;
35 for now this is good enough.)
39 # If we have a jinja environment set up with this locale, just
41 if SETUP_JINJA_ENVS
.has_key(locale
):
42 return SETUP_JINJA_ENVS
[locale
]
44 # jinja2.StrictUndefined will give exceptions on references
45 # to undefined/unknown variables in templates.
46 template_env
= jinja2
.Environment(
47 loader
=template_loader
, autoescape
=True,
48 undefined
=jinja2
.StrictUndefined
,
49 extensions
=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
51 template_env
.install_gettext_callables(
52 mg_globals
.translations
.ugettext
,
53 mg_globals
.translations
.ungettext
)
55 # All templates will know how to ...
56 # ... fetch all waiting messages and remove them from the queue
57 # ... construct a grid of thumbnails or other media
58 # ... have access to the global and app config
59 template_env
.globals['fetch_messages'] = messages
.fetch_messages
60 template_env
.globals['gridify_list'] = gridify_list
61 template_env
.globals['gridify_cursor'] = gridify_cursor
62 template_env
.globals['app_config'] = mg_globals
.app_config
63 template_env
.globals['global_config'] = mg_globals
.global_config
66 SETUP_JINJA_ENVS
[locale
] = template_env
71 # We'll store context information here when doing unit tests
72 TEMPLATE_TEST_CONTEXT
= {}
75 def render_template(request
, template_path
, context
):
77 Render a template with context.
79 Always inserts the request into the context, so you don't have to.
80 Also stores the context if we're doing unit tests. Helpful!
82 template
= request
.template_env
.get_template(
84 context
['request'] = request
85 rendered_csrf_token
= render_csrf_form_token(request
)
86 if rendered_csrf_token
is not None:
87 context
['csrf_token'] = render_csrf_form_token(request
)
88 rendered
= template
.render(context
)
90 if common
.TESTS_ENABLED
:
91 TEMPLATE_TEST_CONTEXT
[template_path
] = context
96 def clear_test_template_context():
97 global TEMPLATE_TEST_CONTEXT
98 TEMPLATE_TEST_CONTEXT
= {}
101 def gridify_list(this_list
, num_cols
=5):
103 Generates a list of lists where each sub-list's length depends on
104 the number of columns in the list
108 # Figure out how many rows we should have
109 num_rows
= int(ceil(float(len(this_list
)) / num_cols
))
111 for row_num
in range(num_rows
):
112 slice_min
= row_num
* num_cols
113 slice_max
= (row_num
+ 1) * num_cols
115 row
= this_list
[slice_min
:slice_max
]
122 def gridify_cursor(this_cursor
, num_cols
=5):
124 Generates a list of lists where each sub-list's length depends on
125 the number of columns in the list
127 return gridify_list(list(this_cursor
), num_cols
)