3c5e537ad630be3a0cee91392648e2c0bb48fd9e
[philo.git] / contrib / sobol / utils.py
1 from django.conf import settings
2 from django.http import QueryDict
3 from django.utils.encoding import smart_str
4 from django.utils.http import urlquote_plus, urlquote
5 from hashlib import sha1
6
7
8 SEARCH_ARG_GET_KEY = 'q'
9 URL_REDIRECT_GET_KEY = 'url'
10 HASH_REDIRECT_GET_KEY = 's'
11
12
13 def make_redirect_hash(search_arg, url):
14         return sha1(smart_str(search_arg + url + settings.SECRET_KEY)).hexdigest()[::2]
15
16
17 def check_redirect_hash(hash, search_arg, url):
18         return hash == make_redirect_hash(search_arg, url)
19
20
21 def make_tracking_querydict(search_arg, url):
22         """
23         Returns a QueryDict instance containing the information necessary
24         for tracking clicks of this url.
25         
26         NOTE: will this kind of initialization handle quoting correctly?
27         """
28         return QueryDict("%s=%s&%s=%s&%s=%s" % (
29                 SEARCH_ARG_GET_KEY, urlquote_plus(search_arg),
30                 URL_REDIRECT_GET_KEY, urlquote(url),
31                 HASH_REDIRECT_GET_KEY, make_redirect_hash(search_arg, url))
32         )