Ticket #6201: cache_tag2.diff

File cache_tag2.diff, 4.8 KB (added by Fredrik Sjöblom, 16 years ago)

Updated the patch and added a line to the docs

  • 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)
  • docs/cache.txt

     
    334334    {% endcache %}
    335335
    336336It's perfectly fine to specify more than one argument to identify the fragment.
    337 Simply pass as many arguments to ``{% cache %}`` as you need.
     337Simply pass as many arguments to ``{% cache %}`` as you need. The cache timeout
     338can also be a template variable.
    338339
    339340The low-level cache API
    340341=======================
  • tests/regressiontests/templates/tests.py

     
    882882            'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'),
    883883            'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'),
    884884            'cache06' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'),
    885             'cache07' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 1}, 'cache05'),
     885            'cache07' : ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'),
     886            'cache08' : ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'),
     887            'cache09' : ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'),
     888            'cache10' : ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'),
    886889
    887             # Raise exception if we dont have at least 2 args, first one integer.
    888             'cache08' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError),
    889             'cache09' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError),
    890             'cache10' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError),
     890            # Raise exception if we dont have at least 2 args, first one resolved to integer.
     891            'cache11' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError),
     892            'cache12' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError),
     893            'cache13' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError),
     894            'cache14' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError),
     895            'cache15' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError),
    891896
    892897            ### AUTOESCAPE TAG ##############################################
    893898            'autoescape-tag01': ("{% autoescape off %}hello{% endautoescape %}", {}, "hello"),
Back to Top