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
14 # Use the system (hardware-based) random number generator if it exists.
15 if hasattr(random, 'SystemRandom'):
16 randrange = random.SystemRandom().randrange
18 randrange = random.randrange
19 MAX_SESSION_KEY = 18446744073709551616L # 2 << 63
22 def get_random_hash(seed):
23 sha_digest = sha_constructor('%s%s%s%s' %
24 (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
25 settings.SECRET_KEY)).digest()
26 return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
32 result.setdefault(tag.category, []).append(tag)
36 class ExistingFile(UploadedFile):
38 def __init__(self, path, *args, **kwargs):
40 return super(ExistingFile, self).__init__(*args, **kwargs)
42 def temporary_file_path(self):