X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/15c68a7a08349d807c767cce58fdbeec0dcc192d..a9bcac16553e058df0d6da55692c8935388e5b5c:/contrib/sobol/utils.py diff --git a/contrib/sobol/utils.py b/contrib/sobol/utils.py new file mode 100644 index 0000000..723a463 --- /dev/null +++ b/contrib/sobol/utils.py @@ -0,0 +1,32 @@ +from django.conf import settings +from django.http import QueryDict +from django.utils.encoding import smart_str +from django.utils.hashcompat import sha_constructor +from django.utils.http import urlquote_plus, urlquote + + +SEARCH_ARG_GET_KEY = 'q' +URL_REDIRECT_GET_KEY = 'url' +HASH_REDIRECT_GET_KEY = 's' + + +def make_redirect_hash(search_arg, url): + return sha_constructor(smart_str(search_arg + url + settings.SECRET_KEY)).hexdigest()[::2] + + +def check_redirect_hash(hash, search_arg, url): + return hash == make_redirect_hash(search_arg, url) + + +def make_tracking_querydict(search_arg, url): + """ + Returns a QueryDict instance containing the information necessary + for tracking clicks of this url. + + NOTE: will this kind of initialization handle quoting correctly? + """ + return QueryDict("%s=%s&%s=%s&%s=%s" % ( + SEARCH_ARG_GET_KEY, urlquote_plus(search_arg), + URL_REDIRECT_GET_KEY, urlquote(url), + HASH_REDIRECT_GET_KEY, make_redirect_hash(search_arg, url)) + ) \ No newline at end of file