1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
9 from base64 import urlsafe_b64encode
10 from collections import defaultdict
11 from errno import EEXIST, ENOENT
12 from fcntl import flock, LOCK_EX
13 from os import mkdir, path, unlink
14 from urllib.parse import urljoin
15 from zipfile import ZipFile
17 from django.apps import apps
18 from django.conf import settings
19 from django.core.files.storage import DefaultStorage
20 from django.core.files.uploadedfile import UploadedFile
21 from django.http import HttpResponse
22 from django.utils.encoding import force_text
24 from reporting.utils import read_chunks
26 # Use the system (hardware-based) random number generator if it exists.
27 if hasattr(random, 'SystemRandom'):
28 randrange = random.SystemRandom().randrange
30 randrange = random.randrange
31 MAX_SESSION_KEY = 18446744073709551616 # 2 << 63
34 def get_random_hash(seed):
35 sha_digest = hashlib.sha1((
37 randrange(0, MAX_SESSION_KEY),
39 str(seed).encode('utf-8', 'replace'),
42 ).encode('utf-8')).digest()
43 return urlsafe_b64encode(sha_digest).decode('latin1').replace('=', '').replace('_', '-').lower()
46 def split_tags(*tag_lists):
47 if len(tag_lists) == 1:
48 result = defaultdict(list)
49 for tag in tag_lists[0]:
50 result[tag.category].append(tag)
52 result = defaultdict(dict)
53 for tag_list in tag_lists:
56 result[tag.category][tag.pk].count += tag.count
58 result[tag.category][tag.pk] = tag
59 for k, v in result.items():
60 result[k] = sorted(v.values(), key=lambda tag: tag.sort_key)
64 class ExistingFile(UploadedFile):
66 def __init__(self, path, *args, **kwargs):
68 super(ExistingFile, self).__init__(*args, **kwargs)
70 def temporary_file_path(self):
77 class LockFile(object):
79 A file lock monitor class; createas an ${objname}.lock
80 file in directory dir, and locks it exclusively.
81 To be used in 'with' construct.
83 def __init__(self, dir, objname):
84 self.lockname = path.join(dir, objname + ".lock")
87 self.lock = open(self.lockname, 'w')
88 flock(self.lock, LOCK_EX)
90 def __exit__(self, *err):
94 if oe.errno != ENOENT:
100 def create_zip(paths, zip_slug):
102 Creates a zip in MEDIA_ROOT/zip directory containing files from path.
103 Resulting archive filename is ${zip_slug}.zip
104 Returns it's path relative to MEDIA_ROOT (no initial slash)
106 # directory to store zip files
107 zip_path = path.join(settings.MEDIA_ROOT, 'zip')
111 except OSError as oe:
112 if oe.errno != EEXIST:
114 zip_filename = zip_slug + ".zip"
116 with LockFile(zip_path, zip_slug):
117 if not path.exists(path.join(zip_path, zip_filename)):
118 zipf = ZipFile(path.join(zip_path, zip_filename), 'w')
120 for arcname, p in paths:
122 arcname = path.basename(p)
123 zipf.write(p, arcname)
127 return 'zip/' + zip_filename
130 def remove_zip(zip_slug):
132 removes the ${zip_slug}.zip file from zip store.
134 zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip')
137 except OSError as oe:
138 if oe.errno != ENOENT:
142 class AttachmentHttpResponse(HttpResponse):
143 """Response serving a file to be downloaded.
145 def __init__(self, file_path, file_name, mimetype):
146 super(AttachmentHttpResponse, self).__init__(mimetype=mimetype)
147 self['Content-Disposition'] = 'attachment; filename=%s' % file_name
148 self.file_path = file_path
149 self.file_name = file_name
151 with open(DefaultStorage().path(self.file_path)) as f:
152 for chunk in read_chunks(f):
156 class MultiQuerySet(object):
157 def __init__(self, *args, **kwargs):
158 self.querysets = args
163 self._count = sum(len(qs) for qs in self.querysets)
169 def __getitem__(self, item):
171 (offset, stop, step) = item.indices(self.count())
172 except AttributeError:
173 # it's not a slice - make it one
174 return self[item:item + 1][0]
176 total_len = stop - offset
177 for qs in self.querysets:
181 items += list(qs[offset:stop])
182 if len(items) >= total_len:
186 stop = total_len - len(items)
190 def truncate_html_words(s, num, end_text='...'):
191 """Truncates HTML to a certain number of words (not counting tags and
192 comments). Closes opened tags if they were correctly closed in the given
193 html. Takes an optional argument of what should be used to notify that the
194 string has been truncated, defaulting to ellipsis (...).
196 Newlines in the HTML are preserved.
198 This is just a version of django.utils.text.truncate_html_words with no space before the end_text.
204 html4_singlets = ('br', 'col', 'link', 'base', 'img', 'param', 'area', 'hr', 'input')
205 # Set up regular expressions
206 re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U)
207 re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>')
208 # Count non-HTML words and keep note of open tags
213 while words <= length:
214 m = re_words.search(s, pos)
216 # Checked through whole string
220 # It's an actual non-HTML word
226 tag = re_tag.match(m.group(0))
227 if not tag or end_text_pos:
228 # Don't worry about non tags or tags after our truncate point
230 closing_tag, tagname, self_closing = tag.groups()
231 tagname = tagname.lower() # Element names are always case-insensitive
232 if self_closing or tagname in html4_singlets:
235 # Check for match in open tags list
237 i = open_tags.index(tagname)
241 # SGML: An end tag closes, back to the matching start tag,
242 # all unclosed intervening start tags with omitted end tags
243 open_tags = open_tags[i+1:]
245 # Add it to the start of the open tags list
246 open_tags.insert(0, tagname)
248 # Don't try to close tags if we don't need to truncate
250 out = s[:end_text_pos]
253 # Close any tags still open
254 for tag in open_tags:
260 def customizations_hash(customizations):
261 customizations.sort()
262 return hash(tuple(customizations))
265 def get_customized_pdf_path(book, customizations):
267 Returns a MEDIA_ROOT relative path for a customized pdf. The name will contain a hash of customization options.
269 h = customizations_hash(customizations)
270 return 'book/%s/%s-custom-%s.pdf' % (book.slug, book.slug, h)
273 def clear_custom_pdf(book):
275 Returns a list of paths to generated customized pdf of a book
277 from waiter.utils import clear_cache
278 clear_cache('book/%s' % book.slug)
281 class AppSettings(object):
282 """Allows specyfying custom settings for an app, with default values.
284 Just subclass, set some properties and instantiate with a prefix.
285 Getting a SETTING from an instance will check for prefix_SETTING
286 in project settings if set, else take the default. The value will be
287 then filtered through _more_SETTING method, if there is one.
290 def __init__(self, prefix):
291 self._prefix = prefix
293 def __getattribute__(self, name):
294 if name.startswith('_'):
295 return object.__getattribute__(self, name)
296 value = getattr(settings, "%s_%s" % (self._prefix, name), object.__getattribute__(self, name))
297 more = "_more_%s" % name
298 if hasattr(self, more):
299 value = getattr(self, more)(value)
303 def delete_from_cache_by_language(cache, key_template):
304 cache.delete_many([key_template % lc for lc, ln in settings.LANGUAGES])
307 def gallery_path(slug):
308 return os.path.join(settings.MEDIA_ROOT, settings.IMAGE_DIR, slug) + '/'
311 def gallery_url(slug):
312 return '%s%s%s/' % (settings.MEDIA_URL, settings.IMAGE_DIR, slug)
315 def absolute_url(url):
316 Site = apps.get_model('sites', 'Site')
317 site = Site.objects.get_current()
318 base_url = '%s://%s' % (
319 'https' if settings.SESSION_COOKIE_SECURE else 'http',
322 return urljoin(base_url, url)
325 def get_mp3_length(path):
326 from mutagen.mp3 import MP3
327 return int(MP3(path).info.length)
330 def set_file_permissions(self, fieldfile):
331 if fieldfile.instance.preview:
332 fieldfile.set_readable(False)