acbd778cd2f075112e69e8f42b130abbdf1fc4ee
[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 librarian import DocProvider
24 from reporting.utils import read_chunks
25 from celery.task import task
26 import catalogue.models
27
28 # Use the system (hardware-based) random number generator if it exists.
29 if hasattr(random, 'SystemRandom'):
30     randrange = random.SystemRandom().randrange
31 else:
32     randrange = random.randrange
33 MAX_SESSION_KEY = 18446744073709551616L     # 2 << 63
34
35
36 def get_random_hash(seed):
37     sha_digest = sha_constructor('%s%s%s%s' %
38         (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
39         settings.SECRET_KEY)).digest()
40     return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
41
42
43 def split_tags(tags):
44     result = {}
45     for tag in tags:
46         result.setdefault(tag.category, []).append(tag)
47     return result
48
49
50 class ExistingFile(UploadedFile):
51
52     def __init__(self, path, *args, **kwargs):
53         self.path = path
54         return super(ExistingFile, self).__init__(*args, **kwargs)
55
56     def temporary_file_path(self):
57         return self.path
58
59     def close(self):
60         pass
61
62
63 class ORMDocProvider(DocProvider):
64     """Used for getting books' children."""
65
66     def __init__(self, book):
67         self.book = book
68
69     def by_slug_and_lang(self, slug, language):
70         if slug == self.book.slug and language == self.language:
71             return open(self.book.xml_file.path)
72         else:
73             return type(self.book).objects.get(
74                     slug=slug, language=language).xml_file
75
76
77 class LockFile(object):
78     """
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.
82     """
83     def __init__(self, dir, objname):
84         self.lockname = path.join(dir, objname + ".lock")
85
86     def __enter__(self):
87         self.lock = open(self.lockname, 'w')
88         flock(self.lock, LOCK_EX)
89
90     def __exit__(self, *err):
91         try:
92             unlink(self.lockname)
93         except OSError as oe:
94             if oe.errno != oe.EEXIST:
95                 raise oe
96         self.lock.close()
97
98
99 @task
100 def create_zip(paths, zip_slug):
101     """
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)
105     """
106     # directory to store zip files
107     zip_path = path.join(settings.MEDIA_ROOT, 'zip')
108
109     try:
110         mkdir(zip_path)
111     except OSError as oe:
112         if oe.errno != EEXIST:
113             raise oe
114     zip_filename = zip_slug + ".zip"
115
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')
119             try:
120                 for arcname, p in paths:
121                     if arcname is None:
122                         arcname = path.basename(p)
123                     zipf.write(p, arcname)
124             finally:
125                 zipf.close()
126
127         return 'zip/' + zip_filename
128
129
130 def remove_zip(zip_slug):
131     """
132     removes the ${zip_slug}.zip file from zip store.
133     """
134     zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip')
135     try:
136         unlink(zip_file)
137     except OSError as oe:
138         if oe.errno != ENOENT:
139             raise oe
140
141
142 class AttachmentHttpResponse(HttpResponse):
143     """Response serving a file to be downloaded.
144     """
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
150
151         with open(DefaultStorage().path(self.file_path)) as f:
152             for chunk in read_chunks(f):
153                 self.write(chunk)
154
155 @task
156 def async_build_pdf(book_id, customizations, file_name):
157     """
158     A celery task to generate pdf files.
159     Accepts the same args as Book.build_pdf, but with book id as first parameter
160     instead of Book instance
161     """
162     book = catalogue.models.Book.objects.get(id=book_id)
163     print "will gen %s" % DefaultStorage().path(file_name)
164     if not DefaultStorage().exists(file_name):
165         book.build_pdf(customizations=customizations, file_name=file_name)
166     print "done."