diff options
author | Dan McGee <dan@archlinux.org> | 2012-05-04 10:57:59 -0500 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2012-05-04 10:59:04 -0500 |
commit | 86f8efaeb1f67138c194d0c373f9d91e2999c5dd (patch) | |
tree | 0a1bd29d137e6a0916f1745a06fb8e495c16a829 /packages | |
parent | 12bf4c1b1e7df2d934b9dfde8629137dedeea99f (diff) |
Fix search suggestions for invalid cache keys
Unfortunately, "invalid" in this case includes spaces, which is a bit
crazy. MD5 the provided search term before using it as a cache key to be
safe.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'packages')
-rw-r--r-- | packages/views/__init__.py | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/packages/views/__init__.py b/packages/views/__init__.py index 0f1dc799..559368b9 100644 --- a/packages/views/__init__.py +++ b/packages/views/__init__.py @@ -1,3 +1,4 @@ +import hashlib from string import Template from urllib import urlencode @@ -46,7 +47,7 @@ def opensearch_suggest(request): if search_term == '': return HttpResponse('', mimetype='application/x-suggestions+json') - cache_key = 'opensearch:packages:' + search_term + cache_key = 'opensearch:packages:' + hashlib.md5(search_term).hexdigest() to_json = cache.get(cache_key, None) if to_json is None: names = Package.objects.filter( @@ -54,7 +55,7 @@ def opensearch_suggest(request): 'pkgname', flat=True).order_by('pkgname').distinct()[:10] results = [search_term, list(names)] to_json = simplejson.dumps(results, ensure_ascii=False) - cache.set('opensearch:packages:%s' % search_term, to_json, 300) + cache.set(cache_key, to_json, 300) return HttpResponse(to_json, mimetype='application/x-suggestions+json') |