Fixed importing books from Django admin.
[wolnelektury.git] / apps / catalogue / utils.py
1 # -*- coding: utf-8 -*-
2 import random
3 import time
4 from base64 import urlsafe_b64encode
5
6 from django.utils.hashcompat import sha_constructor
7 from django.conf import settings
8
9
10 # Use the system (hardware-based) random number generator if it exists.
11 if hasattr(random, 'SystemRandom'):
12     randrange = random.SystemRandom().randrange
13 else:
14     randrange = random.randrange
15 MAX_SESSION_KEY = 18446744073709551616L     # 2 << 63
16
17
18 def get_random_hash(seed):
19     sha_digest = sha_constructor('%s%s%s%s' % 
20         (randrange(0, MAX_SESSION_KEY), time.time(), unicode(seed).encode('utf-8', 'replace'),
21         settings.SECRET_KEY)).digest()
22     return urlsafe_b64encode(sha_digest).replace('=', '').replace('_', '-').lower()
23
24
25 def split_tags(tags):
26     result = {}
27     for tag in tags:
28         result.setdefault(tag.category, []).append(tag)
29     return result
30