Ticket #6201: cache_tag.diff
File cache_tag.diff, 4.3 KB (added by , 17 years ago) |
---|
-
django/templatetags/cache.py
1 from django.template import Library, Node, TemplateSyntaxError 2 from django.template import resolve_variable 1 from django.template import Library, Node, Variable, TemplateSyntaxError, VariableDoesNotExist 3 2 from django.core.cache import cache 4 3 from django.utils.encoding import force_unicode 5 4 … … 13 12 self.vary_on = vary_on 14 13 15 14 def render(self, context): 15 try: 16 self.expire_time = int(self.expire_time) 17 except ValueError: 18 try: 19 self.expire_time = int(Variable(self.expire_time).resolve(context)) 20 except (ValueError, TypeError, VariableDoesNotExist): 21 raise TemplateSyntaxError(u"Cannot resolve cache timeout! '%s' must be an integer." % self.expire_time) 16 22 # Build a unicode key for this fragment and all vary-on's. 17 23 cache_key = u':'.join([self.fragment_name] + \ 18 [force_unicode( resolve_variable(var,context)) for var in self.vary_on])24 [force_unicode(Variable(var).resolve(context)) for var in self.vary_on]) 19 25 value = cache.get(cache_key) 20 26 if value is None: 21 27 value = self.nodelist.render(context) … … 48 54 tokens = token.contents.split() 49 55 if len(tokens) < 3: 50 56 raise TemplateSyntaxError(u"'%r' tag requires at least 2 arguments." % tokens[0]) 51 try: 52 expire_time = int(tokens[1]) 53 except ValueError: 54 raise TemplateSyntaxError(u"First argument to '%r' must be an integer (got '%s')." % (tokens[0], tokens[1])) 55 return CacheNode(nodelist, expire_time, tokens[2], tokens[3:]) 57 return CacheNode(nodelist, tokens[1], tokens[2], tokens[3:]) 56 58 57 59 register.tag('cache', do_cache) -
tests/regressiontests/templates/tests.py
877 877 'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), 878 878 'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), 879 879 'cache06' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), 880 'cache07' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 1}, 'cache05'), 880 'cache07' : ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'), 881 'cache08' : ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'), 882 'cache09' : ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'), 883 'cache10' : ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'), 881 884 882 # Raise exception if we dont have at least 2 args, first one integer. 883 'cache08' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 884 'cache09' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 885 'cache10' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 885 # Raise exception if we dont have at least 2 args, first one resolved to integer. 886 'cache11' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 887 'cache12' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 888 'cache13' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 889 'cache14' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError), 890 'cache15' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError), 886 891 887 892 ### AUTOESCAPE TAG ############################################## 888 893 'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),