Unicode support for request_cached decorator and commentable_id
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
An implementation of a RequestCache. This cache is reset at the beginning
|
||||
and end of every request.
|
||||
"""
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
import crum
|
||||
import threading
|
||||
@@ -108,7 +109,7 @@ def func_call_cache_key(func, *args, **kwargs):
|
||||
the function's name, and a stringified list of arguments
|
||||
and a query string-style stringified list of keyword arguments.
|
||||
"""
|
||||
converted_args = map(str, args)
|
||||
converted_kwargs = map(str, reduce(list.__add__, map(list, sorted(kwargs.iteritems())), []))
|
||||
converted_args = map(force_text, args)
|
||||
converted_kwargs = map(force_text, reduce(list.__add__, map(list, sorted(kwargs.iteritems())), []))
|
||||
cache_keys = [func.__module__, func.func_name] + converted_args + converted_kwargs
|
||||
return '.'.join(cache_keys)
|
||||
return u'.'.join(cache_keys)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Tests for the request cache.
|
||||
"""
|
||||
@@ -183,6 +184,28 @@ class TestRequestCache(TestCase):
|
||||
self.assertEqual(result, 4)
|
||||
self.assertEqual(to_be_wrapped.call_count, 4)
|
||||
|
||||
def test_request_cached_mixed_unicode_str_args(self):
|
||||
"""
|
||||
Ensure that request_cached can work with mixed str and Unicode parameters.
|
||||
"""
|
||||
RequestCache.clear_request_cache()
|
||||
|
||||
def dummy_function(arg1, arg2):
|
||||
"""
|
||||
A dummy function that expects an str and unicode arguments.
|
||||
"""
|
||||
assert isinstance(arg1, str), 'First parameter has to be of type `str`'
|
||||
assert isinstance(arg2, unicode), 'Second parameter has to be of type `unicode`'
|
||||
return True
|
||||
|
||||
self.assertTrue(dummy_function('Hello', u'World'), 'Should be callable with ASCII chars')
|
||||
self.assertTrue(dummy_function('H∂llå', u'Wørld'), 'Should be callable with non-ASCII chars')
|
||||
|
||||
wrapped = request_cached(dummy_function)
|
||||
|
||||
self.assertTrue(wrapped('Hello', u'World'), 'Wrapper should handle ASCII only chars')
|
||||
self.assertTrue(wrapped('H∂llå', u'Wørld'), 'Wrapper should handle non-ASCII chars')
|
||||
|
||||
def test_request_cached_with_none_result(self):
|
||||
"""
|
||||
Ensure that calling a decorated function that returns None
|
||||
|
||||
Reference in New Issue
Block a user