Django-magic to prevent cross-site request forgery for POST requests
[mygpo.git] / mygpo / web / utils.py
blob93ced9d8f4c43f0b6350447aab974080798ac16b
1 from mygpo.api.models import Podcast
2 from babel import Locale, UnknownLocaleError
3 import re
5 def get_accepted_lang(request):
6 return list(set([s[:2] for s in request.META.get('HTTP_ACCEPT_LANGUAGE', '').split(',')]))
8 def get_podcast_languages():
9 """
10 Returns all 2-letter language codes that are used by podcasts.
12 It filters obviously invalid strings, but does not check if any
13 of these codes is contained in ISO 639.
14 """
16 r = '^[a-zA-Z]{2}[-_]?.*$'
18 langs = [x['language'] for x in Podcast.objects.values('language').distinct()]
19 sane_lang = list(set([l[:2] for l in langs if l and re.match(r, l)]))
21 sane_lang.sort()
23 return sane_lang
25 def get_language_names(lang):
26 """
27 Takes a list of language codes and returns a list of tuples
28 with (code, name)
29 """
30 res = {}
31 for l in lang:
32 try:
33 locale = Locale(l)
34 except UnknownLocaleError:
35 continue
37 if locale.display_name:
38 res[l] = locale.display_name
40 return res
43 class UpdatedException(Exception):
44 """Base exception with additional payload"""
45 def __init__(self, data):
46 Exception.__init__(self)
47 self.data = data