Merge branch 'master' into design
[mygpo.git] / mygpo / web / utils.py
blob17fadc533b07376a9e5ce81f6c55fa7d1b2c6eeb
1 import re
2 import string
3 import collections
4 from datetime import datetime
6 from django.views.decorators.cache import never_cache
7 from django.utils.html import strip_tags
8 from django.core.urlresolvers import reverse
9 from django.shortcuts import render
10 from django.http import Http404
12 from babel import Locale, UnknownLocaleError
14 from mygpo.core.models import Podcast
15 from mygpo.core.proxy import proxy_object
16 from mygpo.db.couchdb.podcast import podcast_by_id, podcasts_to_dict
19 def get_accepted_lang(request):
20 """ returns a list of language codes accepted by the HTTP request """
22 lang_str = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
23 lang_str = filter(lambda c: c in string.letters+',', lang_str)
24 langs = lang_str.split(',')
25 langs = [s[:2] for s in langs]
26 langs = map(str.strip, langs)
27 langs = filter(None, langs)
28 return list(set(langs))
31 RE_LANG = re.compile('^[a-zA-Z]{2}[-_]?.*$')
34 def sanitize_language_code(lang):
35 return lang[:2].lower()
38 def sanitize_language_codes(ls):
39 """
40 expects a list of language codes and returns a unique lost of the first
41 part of all items. obviously invalid entries are skipped
43 >>> sanitize_language_codes(['de-at', 'de-ch'])
44 ['de']
46 >>> sanitize_language_codes(['de-at', 'en', 'en-gb', '(asdf', 'Deutsch'])
47 ['de', 'en']
48 """
50 ls = [sanitize_language_code(l) for l in ls if l and RE_LANG.match(l)]
51 return list(set(ls))
54 def get_language_names(lang):
55 """
56 Takes a list of language codes and returns a list of tuples
57 with (code, name)
58 """
59 res = {}
60 for l in lang:
61 try:
62 locale = Locale(l)
63 except UnknownLocaleError:
64 continue
66 if locale.display_name:
67 res[l] = locale.display_name
69 return res
72 def get_page_list(start, total, cur, show_max):
73 """
74 returns a list of pages to be linked for navigation in a paginated view
76 >>> get_page_list(1, 100, 1, 10)
77 [1, 2, 3, 4, 5, 6, '...', 98, 99, 100]
79 >>> get_page_list(1, 100, 50, 10)
80 [1, '...', 48, 49, 50, 51, '...', 98, 99, 100]
82 >>> get_page_list(1, 100, 99, 10)
83 [1, '...', 97, 98, 99, 100]
85 >>> get_page_list(1, 3, 2, 10)
86 [1, 2, 3]
87 """
89 if show_max >= (total - start):
90 return range(start, total+1)
92 ps = []
93 if (cur - start) > show_max / 2:
94 ps.extend(range(start, show_max / 4))
95 ps.append('...')
96 ps.extend(range(cur - show_max / 4, cur))
98 else:
99 ps.extend(range(start, cur))
101 ps.append(cur)
103 if (total - cur) > show_max / 2:
104 # for the first pages, show more pages at the beginning
105 add = show_max / 2 - len(ps)
106 ps.extend(range(cur + 1, cur + show_max / 4 + add))
107 ps.append('...')
108 ps.extend(range(total - show_max / 4, total + 1))
110 else:
111 ps.extend(range(cur + 1, total + 1))
113 return ps
116 def process_lang_params(request):
118 lang = request.GET.get('lang', None)
120 if lang is None:
121 langs = get_accepted_lang(request)
122 lang = next(iter(langs), '')
124 return sanitize_language_code(lang)
127 def symbian_opml_changes(podcast):
128 podcast.description = (podcast.title or '') + '\n' + \
129 (podcast.description or '')
130 return podcast
133 @never_cache
134 def maintenance(request, *args, **kwargs):
135 resp = render(request, 'maintenance.html', {})
136 resp.status_code = 503
137 return resp
140 def get_podcast_link_target(podcast, view_name='podcast', add_args=[]):
141 """ Returns the link-target for a Podcast, preferring slugs over Ids
143 automatically distringuishes between relational Podcast objects and
144 CouchDB-based Podcasts """
146 from mygpo.core.models import Podcast
148 # we prefer slugs
149 if podcast.slug:
150 args = [podcast.slug]
151 view_name = '%s-slug-id' % view_name
153 # as a fallback we use CouchDB-IDs
154 else:
155 args = [podcast.get_id()]
156 view_name = '%s-slug-id' % view_name
158 return reverse(view_name, args=args + add_args)
161 def get_podcast_group_link_target(group, view_name, add_args=[]):
162 """ Returns the link-target for a Podcast group, preferring slugs over Ids
164 automatically distringuishes between relational Podcast objects and
165 CouchDB-based Podcasts """
167 from mygpo.core.models import PodcastGroup
169 # we prefer slugs
170 if group.slug:
171 args = [group.slug]
172 view_name = '%s-slug-id' % view_name
174 # to keep URLs short, we use use oldids
175 elif group.oldid:
176 args = [group.oldid]
178 # as a fallback we use CouchDB-IDs
179 else:
180 args = [group._id]
181 view_name = '%s-slug-id' % view_name
183 return reverse(view_name, args=args + add_args)
186 def get_episode_link_target(episode, podcast, view_name='episode',
187 add_args=[]):
188 """ Returns the link-target for an Episode, preferring slugs over Ids
190 automatically distringuishes between relational Episode objects and
191 CouchDB-based Episodes """
193 from mygpo.core.models import Podcast
195 # prefer slugs
196 if episode.slug:
197 args = [podcast.slug or podcast.get_id(), episode.slug]
198 view_name = '%s-slug-id' % view_name
200 # for short URLs, prefer oldids over CouchDB-IDs
201 elif episode.oldid:
202 args = [episode.oldid]
204 # fallback: CouchDB-IDs
205 else:
206 if not podcast:
207 if isinstance(episode.podcast, Podcast):
208 podcast = episode.podcast
209 elif isinstance(episode.podcast, basestring):
210 podcast = podcast_by_id(episode.podcast)
212 args = [podcast.slug or podcast.get_id(), episode._id]
213 view_name = '%s-slug-id' % view_name
215 return strip_tags(reverse(view_name, args=args + add_args))
218 def fetch_episode_data(episodes, podcasts={}):
220 if not podcasts:
221 podcast_ids = [episode.podcast for episode in episodes]
222 podcasts = podcasts_to_dict(podcast_ids)
224 def set_podcast(episode):
225 episode = proxy_object(episode)
226 episode.podcast = podcasts.get(episode.podcast, None)
227 return episode
229 return map(set_podcast, episodes)
232 # doesn't include the '@' because it's not stored as part of a twitter handle
233 TWITTER_CHARS = string.ascii_letters + string.digits + '_'
236 def normalize_twitter(s):
237 """ normalize user input that is supposed to be a Twitter handle """
238 return "".join(i for i in s if i in TWITTER_CHARS)
241 CCLICENSE = re.compile(r'http://(www\.)?creativecommons.org/licenses/([a-z-]+)/([0-9.]+)?/?')
242 CCPUBLICDOMAIN = re.compile(r'http://(www\.)?creativecommons.org/licenses/publicdomain/?')
243 LicenseInfo = collections.namedtuple('LicenseInfo', 'name version url')
245 def license_info(license_url):
246 """ Extracts license information from the license URL
248 >>> i = license_info('http://creativecommons.org/licenses/by/3.0/')
249 >>> i.name
250 'CC BY'
251 >>> i.version
252 '3.0'
253 >>> i.url
254 'http://creativecommons.org/licenses/by/3.0/'
256 >>> iwww = license_info('http://www.creativecommons.org/licenses/by/3.0/')
257 >>> i.name == iwww.name and i.version == iwww.version
258 True
260 >>> i = license_info('http://www.creativecommons.org/licenses/publicdomain')
261 >>> i.name
262 'Public Domain'
263 >>> i.version is None
264 True
266 >>> i = license_info('http://example.com/my-own-license')
267 >>> i.name is None
268 True
269 >>> i.version is None
270 True
271 >>> i.url
272 'http://example.com/my-own-license'
274 m = CCLICENSE.match(license_url)
275 if m:
276 _, name, version = m.groups()
277 return LicenseInfo('CC %s' % name.upper(), version, license_url)
279 m = CCPUBLICDOMAIN.match(license_url)
280 if m:
281 return LicenseInfo('Public Domain', None, license_url)
283 return LicenseInfo(None, None, license_url)
286 def check_restrictions(obj):
287 """ checks for known restrictions of the object """
288 if "hide" in obj.restrictions:
289 raise Http404