Commit | Line | Data |
---|---|---|
ae3bc7fa AW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011 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 babel.support import LazyProxy | |
21 | from mediagoblin import mg_globals | |
22 | from mediagoblin import messages | |
23 | from mediagoblin.tools import common | |
24 | from mediagoblin.tools.translate import setup_gettext | |
25 | ||
26 | SETUP_JINJA_ENVS = {} | |
27 | ||
28 | def get_jinja_env(template_loader, locale): | |
29 | """ | |
30 | Set up the Jinja environment, | |
31 | ||
32 | (In the future we may have another system for providing theming; | |
33 | for now this is good enough.) | |
34 | """ | |
35 | setup_gettext(locale) | |
36 | ||
37 | # If we have a jinja environment set up with this locale, just | |
38 | # return that one. | |
39 | if SETUP_JINJA_ENVS.has_key(locale): | |
40 | return SETUP_JINJA_ENVS[locale] | |
41 | ||
42 | template_env = jinja2.Environment( | |
43 | loader=template_loader, autoescape=True, | |
44 | extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape']) | |
45 | ||
46 | template_env.install_gettext_callables( | |
47 | mg_globals.translations.ugettext, | |
48 | mg_globals.translations.ungettext) | |
49 | ||
50 | # All templates will know how to ... | |
51 | # ... fetch all waiting messages and remove them from the queue | |
52 | # ... construct a grid of thumbnails or other media | |
53 | template_env.globals['fetch_messages'] = messages.fetch_messages | |
54 | template_env.globals['gridify_list'] = gridify_list | |
55 | template_env.globals['gridify_cursor'] = gridify_cursor | |
56 | ||
57 | if exists(locale): | |
58 | SETUP_JINJA_ENVS[locale] = template_env | |
59 | ||
60 | return template_env | |
61 | ||
62 | # We'll store context information here when doing unit tests | |
63 | TEMPLATE_TEST_CONTEXT = {} | |
64 | ||
65 | ||
66 | def render_template(request, template_path, context): | |
67 | """ | |
68 | Render a template with context. | |
69 | ||
70 | Always inserts the request into the context, so you don't have to. | |
71 | Also stores the context if we're doing unit tests. Helpful! | |
72 | """ | |
73 | template = request.template_env.get_template( | |
74 | template_path) | |
75 | context['request'] = request | |
76 | rendered = template.render(context) | |
77 | ||
78 | if common.TESTS_ENABLED: | |
79 | TEMPLATE_TEST_CONTEXT[template_path] = context | |
80 | ||
81 | return rendered | |
82 | ||
83 | ||
84 | def clear_test_template_context(): | |
85 | global TEMPLATE_TEST_CONTEXT | |
86 | TEMPLATE_TEST_CONTEXT = {} | |
87 | ||
88 | def gridify_list(this_list, num_cols=5): | |
89 | """ | |
90 | Generates a list of lists where each sub-list's length depends on | |
91 | the number of columns in the list | |
92 | """ | |
93 | grid = [] | |
94 | ||
95 | # Figure out how many rows we should have | |
96 | num_rows = int(ceil(float(len(this_list)) / num_cols)) | |
97 | ||
98 | for row_num in range(num_rows): | |
99 | slice_min = row_num * num_cols | |
100 | slice_max = (row_num + 1) * num_cols | |
101 | ||
102 | row = this_list[slice_min:slice_max] | |
103 | ||
104 | grid.append(row) | |
105 | ||
106 | return grid | |
107 | ||
108 | ||
109 | def gridify_cursor(this_cursor, num_cols=5): | |
110 | """ | |
111 | Generates a list of lists where each sub-list's length depends on | |
112 | the number of columns in the list | |
113 | """ | |
114 | return gridify_list(list(this_cursor), num_cols) |