Django

Code

Changeset 7754

Show
Ignore:
Timestamp:
06/25/08 23:54:10 (3 months ago)
Author:
adrian
Message:

Fixed #6201 -- Improved the {% cache %} template tag to allow the timeout to be a template variable. Inspired by the patch by zz and edrik

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/templatetags/cache.py

    r7294 r7754  
    1 from django.template import Library, Node, TemplateSyntaxError 
     1from django.template import Library, Node, TemplateSyntaxError, Variable, VariableDoesNotExist 
    22from django.template import resolve_variable 
    33from django.core.cache import cache 
     
    77 
    88class CacheNode(Node): 
    9     def __init__(self, nodelist, expire_time, fragment_name, vary_on): 
     9    def __init__(self, nodelist, expire_time_var, fragment_name, vary_on): 
    1010        self.nodelist = nodelist 
    11         self.expire_time = expire_time 
     11        self.expire_time_var = Variable(expire_time_var) 
    1212        self.fragment_name = fragment_name 
    1313        self.vary_on = vary_on 
    1414 
    1515    def render(self, context): 
     16        try: 
     17            expire_time = self.expire_time_var.resolve(context) 
     18        except VariableDoesNotExist: 
     19            raise TemplateSyntaxError('"cache" tag got an unknkown variable: %r' % self.expire_time_var.var) 
     20        try: 
     21            expire_time = int(expire_time) 
     22        except (ValueError, TypeError): 
     23            raise TemplateSyntaxError('"cache" tag got a non-integer timeout value: %r' % expire_time) 
    1624        # Build a unicode key for this fragment and all vary-on's. 
    17         cache_key = u':'.join([self.fragment_name] + \ 
    18             [force_unicode(resolve_variable(var, context)) for var in self.vary_on]) 
     25        cache_key = u':'.join([self.fragment_name] + [force_unicode(resolve_variable(var, context)) for var in self.vary_on]) 
    1926        value = cache.get(cache_key) 
    2027        if value is None: 
    2128            value = self.nodelist.render(context) 
    22             cache.set(cache_key, value, self.expire_time) 
     29            cache.set(cache_key, value, expire_time) 
    2330        return value 
    2431 
     
    4956    if len(tokens) < 3: 
    5057        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:]) 
     58    return CacheNode(nodelist, tokens[1], tokens[2], tokens[3:]) 
    5659 
    5760register.tag('cache', do_cache) 
  • django/trunk/docs/cache.txt

    r7294 r7754  
    337337Simply pass as many arguments to ``{% cache %}`` as you need. 
    338338 
     339The cache timeout can be a template variable, as long as the template variable 
     340resolves to an integer value. For example, if the template variable 
     341``my_timeout`` is set to the value ``600``, then the following two examples are 
     342equivalent:: 
     343 
     344    {% cache 600 sidebar %} ... {% endcache %} 
     345    {% cache my_timeout sidebar %} ... {% endcache %} 
     346 
     347This feature is useful in avoiding repetition in templates. You can set the 
     348timeout in a variable, in one place, and just reuse that value. 
     349 
    339350The low-level cache API 
    340351======================= 
  • django/trunk/tests/regressiontests/templates/tests.py

    r7753 r7754  
    864864            ### NOW TAG ######################################################## 
    865865            # Simple case 
    866             'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)), 
     866            'now01': ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)), 
    867867 
    868868            # Check parsing of escaped and special characters 
    869             'now02' : ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError), 
    870         #    'now03' : ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), 
    871         #    'now04' : ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year)) 
     869            'now02': ('{% now "j "n" Y"%}', {}, template.TemplateSyntaxError), 
     870        #    'now03': ('{% now "j \"n\" Y"%}', {}, str(datetime.now().day) + '"' + str(datetime.now().month) + '"' + str(datetime.now().year)), 
     871        #    'now04': ('{% now "j \nn\n Y"%}', {}, str(datetime.now().day) + '\n' + str(datetime.now().month) + '\n' + str(datetime.now().year)) 
    872872 
    873873            ### URL TAG ######################################################## 
    874874            # Successes 
    875             'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'), 
    876             'url02' : ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), 
    877             'url03' : ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'), 
    878             'url04' : ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'), 
    879             'url05' : (u'{% url метка_оператора v %}', {'v': u'Ω'}, 
    880                     '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), 
     875            'url01': ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'), 
     876            'url02': ('{% url regressiontests.templates.views.client_action client.id, action="update" %}', {'client': {'id': 1}}, '/url_tag/client/1/update/'), 
     877            'url03': ('{% url regressiontests.templates.views.index %}', {}, '/url_tag/'), 
     878            'url04': ('{% url named.client client.id %}', {'client': {'id': 1}}, '/url_tag/named-client/1/'), 
     879            'url05': (u'{% url метка_оператора v %}', {'v': u'Ω'}, '/url_tag/%D0%AE%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4/%CE%A9/'), 
    881880 
    882881            # Failures 
    883             'url-fail01' : ('{% url %}', {}, template.TemplateSyntaxError), 
    884             'url-fail02' : ('{% url no_such_view %}', {}, ''), 
    885             'url-fail03' : ('{% url regressiontests.templates.views.client no_such_param="value" %}', {}, ''), 
     882            'url-fail01': ('{% url %}', {}, template.TemplateSyntaxError), 
     883            'url-fail02': ('{% url no_such_view %}', {}, ''), 
     884            'url-fail03': ('{% url regressiontests.templates.views.client no_such_param="value" %}', {}, ''), 
    886885 
    887886            ### CACHE TAG ###################################################### 
    888             'cache01' : ('{% load cache %}{% cache -1 test %}cache01{% endcache %}', {}, 'cache01'), 
    889             'cache02' : ('{% load cache %}{% cache -1 test %}cache02{% endcache %}', {}, 'cache02'), 
    890             'cache03' : ('{% load cache %}{% cache 2 test %}cache03{% endcache %}', {}, 'cache03'), 
    891             'cache04' : ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), 
    892             'cache05' : ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), 
    893             'cache06' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), 
    894             'cache07' : ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 1}, 'cache05'), 
     887            'cache01': ('{% load cache %}{% cache -1 test %}cache01{% endcache %}', {}, 'cache01'), 
     888            'cache02': ('{% load cache %}{% cache -1 test %}cache02{% endcache %}', {}, 'cache02'), 
     889            'cache03': ('{% load cache %}{% cache 2 test %}cache03{% endcache %}', {}, 'cache03'), 
     890            'cache04': ('{% load cache %}{% cache 2 test %}cache04{% endcache %}', {}, 'cache03'), 
     891            'cache05': ('{% load cache %}{% cache 2 test foo %}cache05{% endcache %}', {'foo': 1}, 'cache05'), 
     892            'cache06': ('{% load cache %}{% cache 2 test foo %}cache06{% endcache %}', {'foo': 2}, 'cache06'), 
     893            'cache07': ('{% load cache %}{% cache 2 test foo %}cache07{% endcache %}', {'foo': 1}, 'cache05'), 
     894 
     895            # Allow first argument to be a variable. 
     896            'cache08': ('{% load cache %}{% cache time test foo %}cache08{% endcache %}', {'foo': 2, 'time': 2}, 'cache06'),  
     897            'cache09': ('{% load cache %}{% cache time test foo %}cache09{% endcache %}', {'foo': 3, 'time': -1}, 'cache09'),  
     898            'cache10': ('{% load cache %}{% cache time test foo %}cache10{% endcache %}', {'foo': 3, 'time': -1}, 'cache10'),  
    895899 
    896900            # Raise exception if we don't have at least 2 args, first one integer. 
    897             'cache08' : ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 
    898             'cache09' : ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 
    899             'cache10' : ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 
     901            'cache11': ('{% load cache %}{% cache %}{% endcache %}', {}, template.TemplateSyntaxError), 
     902            'cache12': ('{% load cache %}{% cache 1 %}{% endcache %}', {}, template.TemplateSyntaxError), 
     903            'cache13': ('{% load cache %}{% cache foo bar %}{% endcache %}', {}, template.TemplateSyntaxError), 
     904            'cache14': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': 'fail'}, template.TemplateSyntaxError),  
     905            'cache15': ('{% load cache %}{% cache foo bar %}{% endcache %}', {'foo': []}, template.TemplateSyntaxError),  
    900906 
    901907            ### AUTOESCAPE TAG ##############################################