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.
5 from __future__ import with_statement
9 from base64 import urlsafe_b64encode
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
23 from librarian import DocProvider
24 from reporting.utils import read_chunks
25 from celery.task import task
26 import catalogue.models
28 # Use the system (hardware-based) random number generator if it exists.
29 if hasattr(random, 'SystemRandom'):
30 randrange = random.SystemRandom().randrange
32 randrange = random.randrange
33 MAX_SESSION_KEY = 18446744073709551616L # 2 << 63
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()
46 result.setdefault(tag.category, []).append(tag)
50 class ExistingFile(UploadedFile):
52 def __init__(self, path, *args, **kwargs):
54 return super(ExistingFile, self).__init__(*args, **kwargs)
56 def temporary_file_path(self):
63 class ORMDocProvider(DocProvider):
64 """Used for getting books' children."""
66 def __init__(self, book):
69 def by_slug(self, slug):
70 if slug == self.book.slug:
71 return self.book.xml_file
73 return type(self.book).objects.get(slug=slug).xml_file
76 class LockFile(object):
78 A file lock monitor class; createas an ${objname}.lock
79 file in directory dir, and locks it exclusively.
80 To be used in 'with' construct.
82 def __init__(self, dir, objname):
83 self.lockname = path.join(dir, objname + ".lock")
86 self.lock = open(self.lockname, 'w')
87 flock(self.lock, LOCK_EX)
89 def __exit__(self, *err):
93 if oe.errno != oe.EEXIST:
99 def create_zip(paths, zip_slug):
101 Creates a zip in MEDIA_ROOT/zip directory containing files from path.
102 Resulting archive filename is ${zip_slug}.zip
103 Returns it's path relative to MEDIA_ROOT (no initial slash)
105 # directory to store zip files
106 zip_path = path.join(settings.MEDIA_ROOT, 'zip')
110 except OSError as oe:
111 if oe.errno != EEXIST:
113 zip_filename = zip_slug + ".zip"
115 with LockFile(zip_path, zip_slug):
116 if not path.exists(path.join(zip_path, zip_filename)):
117 zipf = ZipFile(path.join(zip_path, zip_filename), 'w')
119 for arcname, p in paths:
121 arcname = path.basename(p)
122 zipf.write(p, arcname)
126 return 'zip/' + zip_filename
129 def remove_zip(zip_slug):
131 removes the ${zip_slug}.zip file from zip store.
133 zip_file = path.join(settings.MEDIA_ROOT, 'zip', zip_slug + '.zip')
136 except OSError as oe:
137 if oe.errno != ENOENT:
141 class AttachmentHttpResponse(HttpResponse):
142 """Response serving a file to be downloaded.
144 def __init__ (self, file_path, file_name, mimetype):
145 super(AttachmentHttpResponse, self).__init__(mimetype=mimetype)
146 self['Content-Disposition'] = 'attachment; filename=%s' % file_name
147 self.file_path = file_path
148 self.file_name = file_name
150 with open(DefaultStorage().path(self.file_path)) as f:
151 for chunk in read_chunks(f):
155 def async_build_pdf(book_id, customizations, file_name):
157 A celery task to generate pdf files.
158 Accepts the same args as Book.build_pdf, but with book id as first parameter
159 instead of Book instance
161 book = catalogue.models.Book.objects.get(id=book_id)
162 print "will gen %s" % DefaultStorage().path(file_name)
163 if not DefaultStorage().exists(file_name):
164 book.build_pdf(customizations=customizations, file_name=file_name)