Mercurial > sat_legacy_website
annotate sat_website/templatetags/utils.py @ 160:ff48d544bfdd default tip
gallery: replaced <video> tag by <img> for videos thumbnails
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 23 Sep 2018 16:41:30 +0200 |
parents | c56b6ad62153 |
children |
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 _ |
136
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
4 import urllib2 |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
5 import urlparse |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
6 import re |
27
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 register = template.Library() |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 @register.filter |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 def is_tuple(value): |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 """Tell if this value is a tuple or not. |
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 @param value (obj): any object |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 @return: True if value is a tuple |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 """ |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 # 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
|
17 # 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
|
18 return type(value) == tuple |
eda4deefecd1
reorganisation of the menu using submenus
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
19 |
29
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
20 @register.filter |
34
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
21 def is_radio(value): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
22 """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
|
23 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
24 @param value (obj): any object |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
25 @return: True if value is a RadioSelect |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
26 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
27 return type(value) == RadioSelect |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
28 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
29 @register.filter |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
30 def is_checkbox(value): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
31 """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
|
32 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
33 @param value (obj): any object |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
34 @return: True if value is a CheckboxInput |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
35 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
36 return type(value) == CheckboxInput |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
37 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
38 @register.filter |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
39 def selected_label(select): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
40 """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
|
41 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
42 @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
|
43 @return: unicode |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
44 """ |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
45 for value, label in select.field.choices: |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
46 if value == select.value(): |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
47 return label |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
48 return None |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
49 |
9d553570cc61
add adhesion_form.html and thank_you.html
souliane <souliane@mailoo.org>
parents:
29
diff
changeset
|
50 @register.filter |
29
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
51 def buffer(value, n): |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
52 """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
|
53 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
54 @param value (list): a list object |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
55 @return: a list containing sub-lists |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
56 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
57 For example: |
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), 2) |
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), 3) |
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 >>> buffer(range(10), 4) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
66 [[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
|
67 """ |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
68 result = [] |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
69 index = 0 |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
70 while index < len(value): |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
71 result.append(value[index:index + n]) |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
72 index += n |
b45621706d83
use Bootstrap carousels to display images and videos galeries:
souliane <souliane@mailoo.org>
parents:
27
diff
changeset
|
73 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
|
74 |
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 @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
|
76 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
|
77 """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
|
78 |
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
|
79 @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
|
80 @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
|
81 """ |
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 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
|
83 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
|
84 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
|
85 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
|
86 |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
87 @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
|
88 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
|
89 """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
|
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 @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
|
92 @return: unicode |
3d04a955bec4
add membership campaign's video (for now ogv, will eventually change to webm)
souliane <souliane@mailoo.org>
parents:
46
diff
changeset
|
93 """ |
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
|
94 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
|
95 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
|
96 |
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 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
|
98 return "%s%s%s" % (key, sep, value) |
93
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
99 |
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
100 items = [] |
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
101 for key, value in entry.data.iteritems(): |
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
102 if key == "subtitles": # is used as another dict key in media.py |
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
103 key = _(u"subtitles") |
9ae3d9c8b28a
actually display the subtitles (with Firefox it is used only when it's the first <track /> of the list, and when the "default" option is present)
souliane <souliane@mailoo.org>
parents:
90
diff
changeset
|
104 items.append(get_item(key.translate(lang), value)) |
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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 return "(%s)" % ', '.join(items) |
136
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
112 |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
113 @register.simple_tag |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
114 def insert_blog_content(url): |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
115 # XXX: quick and dirty |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
116 url = url.encode("utf-8") |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
117 parse_result = urlparse.urlparse(url, "http") |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
118 base_url = urlparse.urljoin("%s://%s" % (parse_result.scheme, parse_result.netloc), "blog") |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
119 text = urllib2.urlopen(url).read() |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
120 subs = [] |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
121 for match in re.finditer(r"(href=|src=)\"([^\"]*)\"", text): |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
122 if re.match("^[^:]*://", match.group(2)): # keep absolute link |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
123 continue |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
124 # complete relative link |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
125 subs.append((match.group(0), match.group(1) + '"%s"' % urlparse.urljoin(base_url, match.group(2)))) |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
126 for url, new_url in subs: |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
127 text = text.replace(url, new_url) |
c56b6ad62153
add section News with the official news feed
souliane <souliane@mailoo.org>
parents:
93
diff
changeset
|
128 return text |