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