1 from django.conf import settings
2 from django.http import QueryDict
3 from django.utils.encoding import smart_str
4 from django.utils.hashcompat import sha_constructor
5 from django.utils.http import urlquote_plus, urlquote
8 SEARCH_ARG_GET_KEY = 'q'
9 URL_REDIRECT_GET_KEY = 'url'
10 HASH_REDIRECT_GET_KEY = 's'
13 def make_redirect_hash(search_arg, url):
14 return sha_constructor(smart_str(search_arg + url + settings.SECRET_KEY)).hexdigest()[::2]
17 def check_redirect_hash(hash, search_arg, url):
18 return hash == make_redirect_hash(search_arg, url)
21 def make_tracking_querydict(search_arg, url):
23 Returns a QueryDict instance containing the information necessary
24 for tracking clicks of this url.
26 NOTE: will this kind of initialization handle quoting correctly?
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))