Merge branch 'production' into pretty
[wolnelektury.git] / apps / catalogue / utils.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from __future__ import with_statement
6
7 import random
8 import time
9 from base64 import urlsafe_b64encode
10
11 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
12 from django.core.files.uploadedfile import UploadedFile
13 from django.core.files.base import File
14 from django.core.files.storage import DefaultStorage
15 from django.utils.hashcompat import sha_constructor
16 from django.conf import settings
17 from celery.task import task
18 from os import mkdir, path, unlink
19 from errno import EEXIST, ENOENT
20 from fcntl import flock, LOCK_EX
21 from zipfile import ZipFile
22
23 from reporting.utils import read_chunks
24 from celery.task import task
25 import catalogue.models
26
27 # Use the system (hardware-based) random number generator if it exists.
28 if hasattr(random, 'SystemRandom'):
29     randrange = random.SystemRandom().randrange
30 else:
31     randrange = random.randrange
32 MAX_SESSION_KEY = 18446744073709551616L     # 2 << 63
33
34
35 def get_random_hash(seed):
36     sha_digest = sha_constructor('%s%s%s%s' %
37         (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
38         settings.SECRET_KEY)).digest()
39     return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
40
41
42 def split_tags(tags):
43     result = {}
44     for tag in tags:
45         result.setdefault(tag.category, []).append(tag)
46     return result
47
48
49 class ExistingFile(UploadedFile):
50
51     def __init__(self, path, *args, **kwargs):
52         self.path = path
53         return super(ExistingFile, self).__init__(*args, **kwargs)
54
55     def temporary_file_path(self):
56         return self.path
57
58     def close(self):
59         pass
60
61
62 class LockFile(object):
63     """
64     A file lock monitor class; createas an ${objname}.lock
65     file in directory dir, and locks it exclusively.
66     To be used in 'with' construct.
67     """
68     def __init__(self, dir, objname):
69         self.lockname = path.join(dir, objname + ".lock")
70
71     def __enter__(self):
72         self.lock = open(self.lockname, 'w')
73         flock(self.lock, LOCK_EX)
74
75     def __exit__(self, *err):
76         try:
77             unlink(self.lockname)
78         except OSError as oe:
79             if oe.errno != oe.EEXIST:
80                 raise oe
81         self.lock.close()
82
83
84 @task
85 def create_zip(paths, zip_slug):
86     """
87     Creates a zip in MEDIA_ROOT/zip directory containing files from path.
88     Resulting archive filename is ${zip_slug}.zip
89     Returns it's path relative to MEDIA_ROOT (no initial slash)
90     """
91     # directory to store zip files
92     zip_path = path.join(settings.MEDIA_ROOT, 'zip')
93
94     try:
95         mkdir(zip_path)
96     except OSError as oe:
97         if oe.errno != EEXIST:
98             raise oe
99     zip_filename = zip_slug + ".zip"
100
101     with LockFile(zip_path, zip_slug):
102         if not path.exists(path.join(zip_path, zip_filename)):
103             zipf = ZipFile(path.join(zip_path, zip_filename), 'w')
104             try:
105                 for arcname, p in paths:
106                     if arcname is None:
107                         arcname = path.basename(p)
108                     zipf.write(p, arcname)
109             finally:
110                 zipf.close()
111
112         return 'zip/' + zip_filename
113
114
115 def remove_zip(zip_slug):
116     """
117     removes the ${zip_slug}.zip file from zip store.
118     """
119     zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip')
120     try:
121         unlink(zip_file)
122     except OSError as oe:
123         if oe.errno != ENOENT:
124             raise oe
125
126
127 class AttachmentHttpResponse(HttpResponse):
128     """Response serving a file to be downloaded.
129     """
130     def __init__ (self, file_path, file_name, mimetype):
131         super(AttachmentHttpResponse, self).__init__(mimetype=mimetype)
132         self['Content-Disposition'] = 'attachment; filename=%s' % file_name
133         self.file_path = file_path
134         self.file_name = file_name
135
136         with open(DefaultStorage().path(self.file_path)) as f:
137             for chunk in read_chunks(f):
138                 self.write(chunk)
139
140 @task
141 def async_build_pdf(book_id, customizations, file_name):
142     """
143     A celery task to generate pdf files.
144     Accepts the same args as Book.build_pdf, but with book id as first parameter
145     instead of Book instance
146     """
147     book = catalogue.models.Book.objects.get(id=book_id)
148     print "will gen %s" % DefaultStorage().path(file_name)
149     if not DefaultStorage().exists(file_name):
150         book.build_pdf(customizations=customizations, file_name=file_name)
151     print "done."
152
153
154 class MultiQuerySet(object):
155     def __init__(self, *args, **kwargs):
156         self.querysets = args
157         self._count = None
158     
159     def count(self):
160         if not self._count:
161             self._count = sum(len(qs) for qs in self.querysets)
162         return self._count
163     
164     def __len__(self):
165         return self.count()
166         
167     def __getitem__(self, item):
168         try:
169             indices = (offset, stop, step) = item.indices(self.count())
170         except AttributeError:
171             # it's not a slice - make it one
172             return self[item : item + 1][0]
173         items = []
174         total_len = stop - offset
175         for qs in self.querysets:
176             if len(qs) < offset:
177                 offset -= len(qs)
178             else:
179                 items += list(qs[offset:stop])
180                 if len(items) >= total_len:
181                     return items
182                 else:
183                     offset = 0
184                     stop = total_len - len(items)
185                     continue