forked from gpodder/mygpo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
292 lines (206 loc) · 7.54 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import re
import math
import string
import collections
from datetime import datetime
from django.utils.translation import ungettext
from django.views.decorators.cache import never_cache
from django.utils.html import strip_tags
from django.core.urlresolvers import reverse
from django.shortcuts import render
from django.http import Http404
from babel import Locale, UnknownLocaleError
from mygpo.podcasts.models import Podcast
def get_accepted_lang(request):
""" returns a list of language codes accepted by the HTTP request """
lang_str = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
lang_str = ''.join([c for c in lang_str if c in string.ascii_letters+','])
langs = lang_str.split(',')
langs = [s[:2] for s in langs]
langs = list(map(str.strip, langs))
langs = [_f for _f in langs if _f]
return list(set(langs))
RE_LANG = re.compile('^[a-zA-Z]{2}[-_]?.*$')
def sanitize_language_code(lang):
return lang[:2].lower()
def sanitize_language_codes(ls):
"""
expects a list of language codes and returns a unique lost of the first
part of all items. obviously invalid entries are skipped
>>> sanitize_language_codes(['de-at', 'de-ch'])
['de']
>>> set(sanitize_language_codes(['de-at', 'en', 'en-gb', '(asdf', 'Deutsch'])) == {'de', 'en'}
True
"""
ls = [sanitize_language_code(l) for l in ls if l and RE_LANG.match(l)]
return list(set(ls))
def get_language_names(lang):
"""
Takes a list of language codes and returns a list of tuples
with (code, name)
"""
res = {}
for l in lang:
try:
locale = Locale(l)
except UnknownLocaleError:
continue
if locale.display_name:
res[l] = locale.display_name
return res
def get_page_list(start, total, cur, show_max):
"""
returns a list of pages to be linked for navigation in a paginated view
>>> get_page_list(1, 100, 1, 10)
[1, 2, 3, 4, 5, 6, '...', 98, 99, 100]
>>> get_page_list(1, 995/10, 1, 10)
[1, 2, 3, 4, 5, 6, '...', 98, 99, 100]
>>> get_page_list(1, 100, 50, 10)
[1, '...', 48, 49, 50, 51, '...', 98, 99, 100]
>>> get_page_list(1, 100, 99, 10)
[1, '...', 97, 98, 99, 100]
>>> get_page_list(1, 3, 2, 10)
[1, 2, 3]
"""
# if we get "total" as a float (eg from total_entries / entries_per_page)
# we round up
total = math.ceil(total)
if show_max >= (total - start):
return list(range(start, total+1))
ps = []
if (cur - start) > show_max / 2:
ps.extend(list(range(start, int(show_max / 4))))
ps.append('...')
ps.extend(list(range(cur - int(show_max / 4), cur)))
else:
ps.extend(list(range(start, cur)))
ps.append(cur)
if (total - cur) > show_max / 2:
# for the first pages, show more pages at the beginning
add = math.ceil(show_max / 2 - len(ps))
ps.extend(list(range(cur + 1, cur + int(show_max / 4) + add)))
ps.append('...')
ps.extend(list(range(total - int(show_max / 4), total + 1)))
else:
ps.extend(list(range(cur + 1, total + 1)))
return ps
def process_lang_params(request):
lang = request.GET.get('lang', None)
if lang is None:
langs = get_accepted_lang(request)
lang = next(iter(langs), '')
return sanitize_language_code(lang)
def symbian_opml_changes(podcast):
podcast.description = podcast.display_title + '\n' + \
(podcast.description or '')
return podcast
@never_cache
def maintenance(request, *args, **kwargs):
resp = render(request, 'maintenance.html', {})
resp.status_code = 503
return resp
def get_podcast_link_target(podcast, view_name='podcast', add_args=[]):
""" Returns the link-target for a Podcast, preferring slugs over Ids """
# we prefer slugs
if podcast.slug:
args = [podcast.slug]
view_name = '%s-slug' % view_name
# as a fallback we use UUIDs
else:
args = [podcast.get_id()]
view_name = '%s-id' % view_name
return reverse(view_name, args=args + add_args)
def get_podcast_group_link_target(group, view_name, add_args=[]):
""" the link-target for a Podcast group, preferring slugs over Ids """
args = [group.slug]
view_name = '%s-slug-id' % view_name
return reverse(view_name, args=args + add_args)
def get_episode_link_target(episode, podcast, view_name='episode',
add_args=[]):
""" Returns the link-target for an Episode, preferring slugs over Ids """
# prefer slugs
if episode.slug:
args = [podcast.slug, episode.slug]
view_name = '%s-slug' % view_name
# fallback: UUIDs
else:
podcast = podcast or episode.podcast
args = [podcast.get_id(), episode.get_id()]
view_name = '%s-id' % view_name
return strip_tags(reverse(view_name, args=args + add_args))
# doesn't include the '@' because it's not stored as part of a twitter handle
TWITTER_CHARS = string.ascii_letters + string.digits + '_'
def normalize_twitter(s):
""" normalize user input that is supposed to be a Twitter handle """
return "".join(i for i in s if i in TWITTER_CHARS)
CCLICENSE = re.compile(r'http://(www\.)?creativecommons.org/licenses/([a-z-]+)/([0-9.]+)?/?')
CCPUBLICDOMAIN = re.compile(r'http://(www\.)?creativecommons.org/licenses/publicdomain/?')
LicenseInfo = collections.namedtuple('LicenseInfo', 'name version url')
def license_info(license_url):
""" Extracts license information from the license URL
>>> i = license_info('http://creativecommons.org/licenses/by/3.0/')
>>> i.name
'CC BY'
>>> i.version
'3.0'
>>> i.url
'http://creativecommons.org/licenses/by/3.0/'
>>> iwww = license_info('http://www.creativecommons.org/licenses/by/3.0/')
>>> i.name == iwww.name and i.version == iwww.version
True
>>> i = license_info('http://www.creativecommons.org/licenses/publicdomain')
>>> i.name
'Public Domain'
>>> i.version is None
True
>>> i = license_info('http://example.com/my-own-license')
>>> i.name is None
True
>>> i.version is None
True
>>> i.url
'http://example.com/my-own-license'
"""
m = CCLICENSE.match(license_url)
if m:
_, name, version = m.groups()
return LicenseInfo('CC %s' % name.upper(), version, license_url)
m = CCPUBLICDOMAIN.match(license_url)
if m:
return LicenseInfo('Public Domain', None, license_url)
return LicenseInfo(None, None, license_url)
def check_restrictions(obj):
""" checks for known restrictions of the object """
restrictions = obj.restrictions.split(',')
if "hide" in restrictions:
raise Http404
if "hide-author" in restrictions:
obj.author = None
return obj
def hours_to_str(hours_total):
""" returns a human-readable string representation of some hours
>>> hours_to_str(1)
'1 hour'
>>> hours_to_str(5)
'5 hours'
>>> hours_to_str(100)
'4 days, 4 hours'
>>> hours_to_str(960)
'5 weeks, 5 days'
>>> hours_to_str(961)
'5 weeks, 5 days, 1 hour'
"""
weeks = int(hours_total / 24 / 7)
days = int(hours_total / 24) % 7
hours = hours_total % 24
strs = []
if weeks:
strs.append(ungettext('%(weeks)d week', '%(weeks)d weeks', weeks) %
{ 'weeks': weeks})
if days:
strs.append(ungettext('%(days)d day', '%(days)d days', days) %
{ 'days': days})
if hours:
strs.append(ungettext('%(hours)d hour', '%(hours)d hours', hours) %
{ 'hours': hours})
return ', '.join(strs)