Mercurial > sat_legacy_website
annotate sat_website/templatetags/utils.py @ 83:3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 15 Jun 2015 13:52:10 +0200 |
parents | a18800261cf6 |
children | 5de2a3dd4e67 |
rev | line source |
---|---|
27
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
1 from django import template |
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
2 from django.forms import RadioSelect, CheckboxInput |
46
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
3 from django.utils.translation import get_language, ugettext_lazy |
27
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
4 register = template.Library() |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
5 |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
6 @register.filter |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 def is_tuple(value): |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 """Tell if this value is a tuple or not. |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 @param value (obj): any object |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 @return: True if value is a tuple |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
12 """ |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
13 # XXX: we would like to have is_type with an argument instead, but it would |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 # rely on a strange comparison - isinstance(value, arg) doesn't work here |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 return type(value) == tuple |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 |
29
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
17 @register.filter |
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
18 def is_radio(value): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
19 """Tell if this value is a RadioSelect or not. |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
20 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
21 @param value (obj): any object |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
22 @return: True if value is a RadioSelect |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
23 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
24 return type(value) == RadioSelect |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
25 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
26 @register.filter |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
27 def is_checkbox(value): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
28 """Tell if this value is a CheckboxInput or not. |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
29 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
30 @param value (obj): any object |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
31 @return: True if value is a CheckboxInput |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
32 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
33 return type(value) == CheckboxInput |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
34 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
35 @register.filter |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
36 def selected_label(select): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
37 """Return the label of the single selected option. |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
38 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
39 @param select: a BoundField object bound to a Select with single choice. |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
40 @return: unicode |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
41 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
42 for value, label in select.field.choices: |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
43 if value == select.value(): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
44 return label |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
45 return None |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
46 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
47 @register.filter |
29
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
48 def buffer(value, n): |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
49 """Split values in sub-lists of n elements. |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
50 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
51 @param value (list): a list object |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
52 @return: a list containing sub-lists |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
53 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
54 For example: |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
55 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
56 >>> buffer(range(10), 2) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
57 [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
58 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
59 >>> buffer(range(10), 3) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
60 [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
61 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
62 >>> buffer(range(10), 4) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
63 [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]] |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
64 """ |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
65 result = [] |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
66 index = 0 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
67 while index < len(value): |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
68 result.append(value[index:index + n]) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
69 index += n |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
70 return result |
46
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
71 |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
72 @register.filter |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
73 def metadata(data): |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
74 """Return a string representation of a metadata dict. |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
75 |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
76 @param data (dict{__proxy__: unicode}): metadata dictionnary |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
77 @return: unicode |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
78 """ |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
79 if not data: |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
80 return "" |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
81 lang = get_language() |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
82 sep = ugettext_lazy(': ') |
a18800261cf6
change some texts, add a couple of screenshots, set metadata version and year to the medias
souliane <souliane@mailoo.org>
parents:
34
diff
changeset
|
83 items = ["%s%s%s" % (key.translate(lang), sep, value) for key, value in data.iteritems()] |
83
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
84 return replace_language_tag("(%s)" % ', '.join(items)) |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
85 |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
86 @register.filter |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
87 def replace_language_tag(text): |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
88 """Replace {{LANGUAGE_CODE}} by the current language code. |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
89 |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
90 @param text (unicode): text to process |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
91 @return: unicode |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
92 """ |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
93 return text.replace("{{LANGUAGE_CODE}}", get_language()) |