annotate sat_website/templatetags/utils.py @ 90:5de2a3dd4e67

change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
author souliane <souliane@mailoo.org>
date Tue, 16 Jun 2015 17:23:15 +0200
parents 3d04a955bec4
children 9ae3d9c8b28a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
3 from django.utils.translation import get_language, ugettext_lazy as _
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
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
73 def select_video_path(paths):
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
74 """Return the video path matching the current language.
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
75
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
76 @param paths (dict): dict of video paths.
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
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 lang = get_language()
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
80 if lang in paths:
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
81 return paths[lang]
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
82 return paths.values()[0]
83
3d04a955bec4 add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents: 46
diff changeset
83
3d04a955bec4 add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents: 46
diff changeset
84 @register.filter
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
85 def metadata(entry):
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
86 """Return a string representation of the video metadata.
83
3d04a955bec4 add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents: 46
diff changeset
87
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
88 @param entry (VideoDesc): video entry
83
3d04a955bec4 add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents: 46
diff changeset
89 @return: unicode
3d04a955bec4 add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents: 46
diff changeset
90 """
90
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
91 sep = _(': ')
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
92 lang = get_language()
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
93
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
94 def get_item(key, value):
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
95 return "%s%s%s" % (key, sep, value)
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
96 items = [get_item(key.translate(lang), value) for key, value in entry.data.iteritems()]
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
97 try:
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
98 lang_key = _(u"language").translate(lang)
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
99 lang_value = lang if lang in entry.paths else entry.paths.keys()[0]
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
100 items.append(get_item(lang_key, lang_value))
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
101 except AttributeError:
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
102 pass
5de2a3dd4e67 change VideoDesc attribute path to paths, a dict where you can specify a different path for each language
souliane <souliane@mailoo.org>
parents: 83
diff changeset
103 return "(%s)" % ', '.join(items)