1 from hashlib import sha1
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
9 SEARCH_ARG_GET_KEY = 'q'
10 URL_REDIRECT_GET_KEY = 'url'
11 HASH_REDIRECT_GET_KEY = 's'
14 def make_redirect_hash(search_arg, url):
15 return sha1(smart_str(search_arg + url + settings.SECRET_KEY)).hexdigest()[::2]
18 def check_redirect_hash(hash, search_arg, url):
19 return hash == make_redirect_hash(search_arg, url)
22 def make_tracking_querydict(search_arg, url):
24 Returns a QueryDict instance containing the information necessary
25 for tracking clicks of this url.
27 NOTE: will this kind of initialization handle quoting correctly?
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))