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.
7 from base64 import urlsafe_b64encode
9 from django.core.files.uploadedfile import UploadedFile
10 from django.utils.hashcompat import sha_constructor
11 from django.conf import settings
13 from librarian import DocProvider
16 # Use the system (hardware-based) random number generator if it exists.
17 if hasattr(random, 'SystemRandom'):
18 randrange = random.SystemRandom().randrange
20 randrange = random.randrange
21 MAX_SESSION_KEY = 18446744073709551616L # 2 << 63
24 def get_random_hash(seed):
25 sha_digest = sha_constructor('%s%s%s%s' %
26 (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
27 settings.SECRET_KEY)).digest()
28 return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
34 result.setdefault(tag.category, []).append(tag)
38 class ExistingFile(UploadedFile):
40 def __init__(self, path, *args, **kwargs):
42 return super(ExistingFile, self).__init__(*args, **kwargs)
44 def temporary_file_path(self):
51 class BookImportDocProvider(DocProvider):
52 """Used for joined EPUB and PDF files."""
54 def __init__(self, book):
57 def by_slug(self, slug):
58 if slug == self.book.slug:
59 return self.book.xml_file
61 return type(self.book).objects.get(slug=slug).xml_file