Ticket #13956: patch_r15283.diff

File patch_r15283.diff, 10.7 KB (added by Stephen Burrows, 13 years ago)
  • django/template/base.py

    diff --git a/django/template/base.py b/django/template/base.py
    index 1440869..d865f1d 100644
    a b class VariableNode(Node):  
    796796            return ''
    797797        return _render_value_in_context(output, context)
    798798
    799 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     799def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None):
    800800    "Returns a template.Node subclass."
    801801    bits = token.split_contents()[1:]
    802802    bmax = len(params)
    803803    def_len = defaults and len(defaults) or 0
    804804    bmin = bmax - def_len
    805     if(len(bits) < bmin or len(bits) > bmax):
    806         if bmin == bmax:
    807             message = "%s takes %s arguments" % (name, bmin)
    808         else:
    809             message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    810         raise TemplateSyntaxError(message)
     805    if varargs is None:
     806        if(len(bits) < bmin or len(bits) > bmax):
     807            if bmin == bmax:
     808                message = "%s takes %s arguments" % (name, bmin)
     809            else:
     810                message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
     811            raise TemplateSyntaxError(message)
     812    else:
     813        if(len(bits) < bmin):
     814            message = "%s takes at least %s arguments" % (name, bmin)
     815            raise TemplateSyntaxError(message)
    811816    return node_class(bits)
    812817
    813818class Library(object):
    class Library(object):  
    865870
    866871    def simple_tag(self, func=None, takes_context=None):
    867872        def dec(func):
    868             params, xx, xxx, defaults = getargspec(func)
     873            params, varargs, xxx, defaults = getargspec(func)
    869874            if takes_context:
    870875                if params[0] == 'context':
    871876                    params = params[1:]
    class Library(object):  
    884889                        func_args = resolved_vars
    885890                    return func(*func_args)
    886891
    887             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     892            compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs)
    888893            compile_func.__doc__ = func.__doc__
    889894            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    890895            return func
    class Library(object):  
    900905
    901906    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
    902907        def dec(func):
    903             params, xx, xxx, defaults = getargspec(func)
     908            params, varargs, xxx, defaults = getargspec(func)
    904909            if takes_context:
    905910                if params[0] == 'context':
    906911                    params = params[1:]
    class Library(object):  
    935940                    if csrf_token is not None:
    936941                        new_context['csrf_token'] = csrf_token
    937942                    return self.nodelist.render(new_context)
    938 
    939             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
     943            compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs)
    940944            compile_func.__doc__ = func.__doc__
    941945            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    942946            return func
  • tests/regressiontests/templates/templatetags/custom.py

    diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py
    index 7011316..87aeb83 100644
    a b def params_and_context(context, arg):  
    3939    return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
    4040params_and_context.anything = "Expected params_and_context __dict__"
    4141
     42
     43def simple_tag_with_three_arguments(one, two, three):
     44    return ', '.join((one, two, three))
     45
     46register.simple_tag(simple_tag_with_three_arguments)
     47
     48
     49def simple_tag_with_two_default_arguments(one, two='2', three='3'):
     50    return ', '.join((one, two, three))
     51
     52register.simple_tag(simple_tag_with_two_default_arguments)
     53
     54
     55def simple_tag_with_unlimited_arguments(one, two='2', *args):
     56    return ', '.join([one, two] + list(args))
     57
     58register.simple_tag(simple_tag_with_unlimited_arguments)
     59
     60
     61def simple_tag_with_only_unlimited_arguments(*args):
     62        return ', '.join(args)
     63
     64register.simple_tag(simple_tag_with_only_unlimited_arguments)
     65
     66
     67def inclusion_tag_with_two_arguments(one, two):
     68    return {
     69        'first': one,
     70        'second': two
     71    }
     72
     73register.inclusion_tag('basic-syntax03')(inclusion_tag_with_two_arguments)
     74
     75
     76def inclusion_tag_with_one_default_argument(one, two='hi'):
     77    return {
     78        'first': one,
     79        'second': two
     80    }
     81
     82register.inclusion_tag('basic-syntax03')(inclusion_tag_with_one_default_argument)
     83
     84
     85def inclusion_tag_with_unlimited_arguments(one, two='hi', *args):
     86    return {
     87        'values': [one, two] + list(args)
     88    }
     89
     90register.inclusion_tag('for-tag01')(inclusion_tag_with_unlimited_arguments)
     91
     92
     93def inclusion_tag_with_only_unlimited_arguments(*args):
     94    return {
     95        'values': args
     96    }
     97
     98register.inclusion_tag('for-tag01')(inclusion_tag_with_only_unlimited_arguments)
     99 No newline at end of file
  • tests/regressiontests/templates/tests.py

    diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
    index 99b4367..0e6e2b6 100644
    a b class Templates(unittest.TestCase):  
    15261526            'static-prefixtag02': ('{% load static %}{% get_static_prefix as static_prefix %}{{ static_prefix }}', {}, settings.STATIC_URL),
    15271527            'static-prefixtag03': ('{% load static %}{% get_media_prefix %}', {}, settings.MEDIA_URL),
    15281528            'static-prefixtag04': ('{% load static %}{% get_media_prefix as media_prefix %}{{ media_prefix }}', {}, settings.MEDIA_URL),
     1529
     1530
     1531            ### SIMPLE TAGS ######################################################
     1532            'simpletags01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1533            'simpletags02': ('{% load custom %}{% simple_tag_with_three_arguments one two three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 2, 3'),
     1534
     1535            # Default arguments
     1536            'simpletags-default01': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1537            'simpletags-default02': ('{% load custom %}{% simple_tag_with_two_default_arguments one three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 3, 3'),
     1538            'simpletags-default03': ('{% load custom %}{% simple_tag_with_two_default_arguments one %}', {'one': 'first'}, 'first, 2, 3'),
     1539
     1540            # Unlimited arguments
     1541            'simpletags-unlimited01': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1542            'simpletags-unlimited02': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}', {}, 'one, 2'),
     1543            'simpletags-unlimited03': ('{% load custom %}{% simple_tag_with_unlimited_arguments one two three four five %}', {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'}, '1, 2, 3, 4, 5'),
     1544            'simpletags-unlimited04': ('{% load custom %}{% simple_tag_with_unlimited_arguments one %}', {'one': 'first'}, 'first, 2'),
     1545            'simpletags-unlimited05': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments one %}', {'one': 'first'}, 'first'),
     1546            'simpletags-unlimited06': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments %}', {}, ''),
     1547            'simpletags-unlimited07': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments "one" "2" "3" %}', {}, 'one, 2, 3'),
     1548
     1549            # Failures
     1550            'simpletags-fail01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}', {}, template.TemplateSyntaxError),
     1551            'simpletags-fail02': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError),
     1552            'simpletags-fail03': ('{% load custom %}{% simple_tag_with_two_default_arguments %}', {}, template.TemplateSyntaxError),
     1553            'simpletags-fail04': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError),
     1554            'simpletags-fail05': ('{% load custom %}{% simple_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError),
     1555
     1556
     1557
     1558            ### INCLUSION TAGS ######################################################
     1559            'inclusiontags01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" "2" %}', {}, "1 --- 2"),
     1560            'inclusiontags02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two %}', {'two': '2'}, "1 --- 2"),
     1561
     1562            # Default arguments
     1563            'inclusiontags-default01': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" "2" %}', {}, "1 --- 2"),
     1564            'inclusiontags-default02': ('{% load custom %}{% inclusion_tag_with_one_default_argument one %}', {'one': "1"}, "1 --- hi"),
     1565
     1566            # Unlimited arguments
     1567            'inclusiontags-unlimited01': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" %}', {}, "12"),
     1568            'inclusiontags-unlimited02': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments one %}', {'one': 'first'}, "firsthi"),
     1569            'inclusiontags-unlimited03': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" "3" "5" "7" %}', {}, "12357"),
     1570            'inclusiontags-unlimited04': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments %}', {}, ""),
     1571            'inclusiontags-unlimited05': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments "1" "2" "3" "5" hello %}', {'hello': 'hello'}, "1235hello"),
     1572
     1573            # Failures
     1574            'inclusiontags-fail01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" %}', {}, template.TemplateSyntaxError),
     1575            'inclusiontags-fail02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError),
     1576            'inclusiontags-fail03': ('{% load custom %}{% inclusion_tag_with_one_default_argument %}', {}, template.TemplateSyntaxError),
     1577            'inclusiontags-fail04': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError),
     1578            'inclusiontags-fail05': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError),
    15291579        }
    15301580
    15311581class TemplateTagLoading(unittest.TestCase):
Back to Top