Ticket #6201: cache_tag.diff

File cache_tag.diff, 4.3 KB (added by zz <zodizz@…>, 17 years ago)

Improved tag to accept time as variable

  • django/templatetags/cache.py

     
    1 from django.template import Library, Node, TemplateSyntaxError
    2 from django.template import resolve_variable
     1from django.template import Library, Node, Variable, TemplateSyntaxError, VariableDoesNotExist
    32from django.core.cache import cache
    43from django.utils.encoding import force_unicode
    54
     
    1312        self.vary_on = vary_on
    1413
    1514    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)
    1622        # Build a unicode key for this fragment and all vary-on's.
    1723        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])
    1925        value = cache.get(cache_key)
    2026        if value is None:
    2127            value = self.nodelist.render(context)
     
    4854    tokens = token.contents.split()
    4955    if len(tokens) < 3:
    5056        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:])
    5658
    5759register.tag('cache', do_cache)
  • tests/regressiontests/templates/tests.py

     
    877877            'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'),
    878878            'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'),
    879879            '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'),
    881884
    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),
    886891
    887892            ### AUTOESCAPE TAG ##############################################
    888893            'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),
Back to Top