Ticket #13014: 13014.diff
File 13014.diff, 5.8 KB (added by , 15 years ago) |
---|
-
django/templatetags/cache.py
diff --git a/django/templatetags/cache.py b/django/templatetags/cache.py index 387dd87..1b1b2be 100644
a b 1 1 from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist 2 2 from django.template import resolve_variable 3 3 from django.core.cache import cache 4 from django.utils.encoding import force_unicode 5 from django.utils.http import urlquote 6 from django.utils.hashcompat import md5_constructor 4 from django.utils.cache import get_template_cache_key 7 5 8 6 register = Library() 9 7 … … class CacheNode(Node): 24 22 except (ValueError, TypeError): 25 23 raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) 26 24 # Build a unicode key for this fragment and all vary-on's. 27 args = md5_constructor(u':'.join([urlquote(resolve_variable(var, context)) for var in self.vary_on]))28 cache_key = 'template.cache.%s.%s' % (self.fragment_name, args.hexdigest())25 args = [resolve_variable(var, context) for var in self.vary_on] 26 cache_key = get_template_cache_key(self.fragment_name, args) 29 27 value = cache.get(cache_key) 30 28 if value is None: 31 29 value = self.nodelist.render(context) -
django/utils/cache.py
diff --git a/django/utils/cache.py b/django/utils/cache.py index 6cfd893..4c360f6 100644
a b import time 23 23 from django.conf import settings 24 24 from django.core.cache import cache 25 25 from django.utils.encoding import smart_str, iri_to_uri 26 from django.utils.http import http_date 26 from django.utils.http import http_date, urlquote 27 27 from django.utils.hashcompat import md5_constructor 28 28 from django.utils.translation import get_language 29 from django.http import HttpRequest30 29 31 30 cc_delim_re = re.compile(r'\s*,\s*') 32 31 … … def patch_vary_headers(response, newheaders): 135 134 response['Vary'] = ', '.join(vary_headers + additional_headers) 136 135 137 136 def _i18n_cache_key_suffix(request, cache_key): 138 """If enabled, returns the cache key ending with a locale.""" 137 """ 138 If I18N is enabled, returns the cache key appended with a locale (otherwise 139 returns the cache key unmodified). 140 141 While ``request`` is a required argument, if it does not make sense in the 142 context of the key generation, use ``None`` for its value. 143 144 """ 139 145 if settings.USE_I18N: 140 146 # first check if LocaleMiddleware or another middleware added 141 147 # LANGUAGE_CODE to request, then fall back to the active language … … def learn_cache_key(request, response, cache_timeout=None, key_prefix=None): 210 216 cache.set(cache_key, [], cache_timeout) 211 217 return _generate_cache_key(request, [], key_prefix) 212 218 219 def get_template_cache_key(name, args=None): 220 """Returns a cache key for a template tag related cache.""" 221 args = args and [urlquote(arg) for arg in args] or [] 222 md5_args = md5_constructor(u':'.join(args)) 223 cache_key = 'template.cache.%s.%s' % (name, md5_args.hexdigest()) 224 return _i18n_cache_key_suffix(request=None, cache_key=cache_key) 213 225 214 226 def _to_tuple(s): 215 227 t = s.split('=',1) -
tests/regressiontests/cache/tests.py
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 109374c..09ce616 100644
a b from django.core.cache.backends.base import InvalidCacheBackendError 16 16 from django.http import HttpResponse, HttpRequest 17 17 from django.middleware.cache import FetchFromCacheMiddleware, UpdateCacheMiddleware 18 18 from django.utils import translation 19 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key 19 from django.utils.cache import patch_vary_headers, get_cache_key, learn_cache_key,\ 20 get_template_cache_key 20 21 from django.utils.hashcompat import md5_constructor 22 from django.template import Context 23 from django.template.loader import get_template_from_string 21 24 from regressiontests.cache.models import Poll, expensive_calculation 22 25 23 26 # functions/classes for complex data type tests … … class CacheI18nTest(unittest.TestCase): 527 530 key2 = get_cache_key(request) 528 531 self.assertEqual(key, key2) 529 532 530 def test_cache_key_no_i18n 533 def test_cache_key_no_i18n(self): 531 534 settings.USE_I18N = False 532 535 request = self._get_request() 533 536 lang = translation.get_language() … … class CacheI18nTest(unittest.TestCase): 568 571 get_cache_data = FetchFromCacheMiddleware().process_request(request) 569 572 self.assertEqual(get_cache_data.content, es_message) 570 573 574 def test_template_cache_key_i18n(self): 575 settings.USE_I18N = True 576 lang = 'en' 577 translation.activate(lang) 578 key = get_template_cache_key('somekey') 579 self.assertTrue(key.endswith(lang), "Cache keys should include the " 580 "language name when i18n is active") 581 582 def test_template_cache_key_no_i18n(self): 583 settings.USE_I18N = False 584 lang = 'en' 585 translation.activate(lang) 586 key = get_template_cache_key('somekey') 587 self.assertFalse(key.endswith(lang), "Cache keys shouldn't include " 588 "the language name when i18n is inactive") 589 590 def test_template_tag(self): 591 settings.USE_I18N = True 592 template = get_template_from_string('{% load cache %}' 593 '{% cache 60 greeting %}{{ welcome }}!{% endcache %}') 594 translation.activate('en') 595 template.render(Context({'welcome': 'Hello'})) 596 translation.activate('es') 597 template.render(Context({'welcome': 'Hola'})) 598 599 translation.activate('en') 600 self.assertEqual(template.render(Context()), 'Hello!') 601 translation.activate('es') 602 self.assertEqual(template.render(Context()), 'Hola!') 603 571 604 if __name__ == '__main__': 572 605 unittest.main()