Ticket #13956: indefinite_args_patch_with_complete_tests.diff

File indefinite_args_patch_with_complete_tests.diff, 10.2 KB (added by Stephen Burrows, 14 years ago)
  • django/template/__init__.py

     
    848848            return ''
    849849        return _render_value_in_context(output, context)
    850850
    851 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     851def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None):
    852852    "Returns a template.Node subclass."
    853853    bits = token.split_contents()[1:]
    854854    bmax = len(params)
    855855    def_len = defaults and len(defaults) or 0
    856856    bmin = bmax - def_len
    857     if(len(bits) < bmin or len(bits) > bmax):
    858         if bmin == bmax:
    859             message = "%s takes %s arguments" % (name, bmin)
    860         else:
    861             message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
    862         raise TemplateSyntaxError(message)
     857    if varargs is None:
     858        if(len(bits) < bmin or len(bits) > bmax):
     859            if bmin == bmax:
     860                message = "%s takes %s arguments" % (name, bmin)
     861            else:
     862                message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
     863            raise TemplateSyntaxError(message)
     864    else:
     865        if(len(bits) < bmin):
     866            message = "%s takes at least %s arguments" % (name, bmin)
     867            raise TemplateSyntaxError(message)
    863868    return node_class(bits)
    864869
    865870class Library(object):
     
    916921        return func
    917922
    918923    def simple_tag(self,func):
    919         params, xx, xxx, defaults = getargspec(func)
     924        params, varargs, xx, defaults = getargspec(func)
    920925
    921926        class SimpleNode(Node):
    922927            def __init__(self, vars_to_resolve):
     
    926931                resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
    927932                return func(*resolved_vars)
    928933
    929         compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     934        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs)
    930935        compile_func.__doc__ = func.__doc__
    931936        self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    932937        return func
    933938
    934939    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
    935940        def dec(func):
    936             params, xx, xxx, defaults = getargspec(func)
     941            params, varargs, xx, defaults = getargspec(func)
    937942            if takes_context:
    938943                if params[0] == 'context':
    939944                    params = params[1:]
     
    968973                    if csrf_token is not None:
    969974                        new_context['csrf_token'] = csrf_token
    970975                    return self.nodelist.render(new_context)
    971 
    972             compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
     976            compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs)
    973977            compile_func.__doc__ = func.__doc__
    974978            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    975979            return func
  • tests/regressiontests/templates/tests.py

     
    13501350            # Escape requirement survives lookup.
    13511351            'autoescape-lookup01': ('{{ var.key }}', { "var": {"key": "this & that" }}, "this &amp; that" ),
    13521352
     1353
     1354            ### SIMPLE TAGS ######################################################
     1355            'simpletags01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1356            'simpletags02': ('{% load custom %}{% simple_tag_with_three_arguments one two three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 2, 3'),
     1357
     1358            # Default arguments
     1359            'simpletags-default01': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1360            'simpletags-default02': ('{% load custom %}{% simple_tag_with_two_default_arguments one three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 3, 3'),
     1361            'simpletags-default03': ('{% load custom %}{% simple_tag_with_two_default_arguments one %}', {'one': 'first'}, 'first, 2, 3'),
     1362
     1363            # Unlimited arguments
     1364            'simpletags-unlimited01': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}', {}, 'one, two, three'),
     1365            'simpletags-unlimited02': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}', {}, 'one, 2'),
     1366            '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'),
     1367            'simpletags-unlimited04': ('{% load custom %}{% simple_tag_with_unlimited_arguments one %}', {'one': 'first'}, 'first, 2'),
     1368            'simpletags-unlimited05': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments one %}', {'one': 'first'}, 'first'),
     1369            'simpletags-unlimited06': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments %}', {}, ''),
     1370            'simpletags-unlimited07': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments "one" "2" "3" %}', {}, 'one, 2, 3'),
     1371
     1372            # Failures
     1373            'simpletags-fail01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}', {}, template.TemplateSyntaxError),
     1374            'simpletags-fail02': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError),
     1375            'simpletags-fail03': ('{% load custom %}{% simple_tag_with_two_default_arguments %}', {}, template.TemplateSyntaxError),
     1376            'simpletags-fail04': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError),
     1377            'simpletags-fail05': ('{% load custom %}{% simple_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError),
     1378
     1379
     1380
     1381            ### INCLUSION TAGS ######################################################
     1382            'inclusiontags01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" "2" %}', {}, "1 --- 2"),
     1383            'inclusiontags02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two %}', {'two': '2'}, "1 --- 2"),
     1384
     1385            # Default arguments
     1386            'inclusiontags-default01': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" "2" %}', {}, "1 --- 2"),
     1387            'inclusiontags-default02': ('{% load custom %}{% inclusion_tag_with_one_default_argument one %}', {'one': "1"}, "1 --- hi"),
     1388
     1389            # Unlimited arguments
     1390            'inclusiontags-unlimited01': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" %}', {}, "12"),
     1391            'inclusiontags-unlimited02': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments one %}', {'one': 'first'}, "firsthi"),
     1392            'inclusiontags-unlimited03': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" "3" "5" "7" %}', {}, "12357"),
     1393            'inclusiontags-unlimited04': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments %}', {}, ""),
     1394            'inclusiontags-unlimited05': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments "1" "2" "3" "5" hello %}', {'hello': 'hello'}, "1235hello"),
     1395
     1396            # Failures
     1397            'inclusiontags-fail01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" %}', {}, template.TemplateSyntaxError),
     1398            'inclusiontags-fail02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError),
     1399            'inclusiontags-fail03': ('{% load custom %}{% inclusion_tag_with_one_default_argument %}', {}, template.TemplateSyntaxError),
     1400            'inclusiontags-fail04': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError),
     1401            'inclusiontags-fail05': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError),
     1402
    13531403        }
    13541404
    13551405
  • tests/regressiontests/templates/templatetags/custom.py

     
    99
    1010register.filter(trim)
    1111
     12
     13def simple_tag_with_three_arguments(one, two, three):
     14    return ', '.join((one, two, three))
     15
     16register.simple_tag(simple_tag_with_three_arguments)
     17
     18
     19def simple_tag_with_two_default_arguments(one, two='2', three='3'):
     20    return ', '.join((one, two, three))
     21
     22register.simple_tag(simple_tag_with_two_default_arguments)
     23
     24
     25def simple_tag_with_unlimited_arguments(one, two='2', *args):
     26    return ', '.join([one, two] + list(args))
     27
     28register.simple_tag(simple_tag_with_unlimited_arguments)
     29
     30
     31def simple_tag_with_only_unlimited_arguments(*args):
     32        return ', '.join(args)
     33
     34register.simple_tag(simple_tag_with_only_unlimited_arguments)
     35
     36
     37def inclusion_tag_with_two_arguments(one, two):
     38    return {
     39        'first': one,
     40        'second': two
     41    }
     42
     43register.inclusion_tag('basic-syntax03')(inclusion_tag_with_two_arguments)
     44
     45
     46def inclusion_tag_with_one_default_argument(one, two='hi'):
     47    return {
     48        'first': one,
     49        'second': two
     50    }
     51
     52register.inclusion_tag('basic-syntax03')(inclusion_tag_with_one_default_argument)
     53
     54
     55def inclusion_tag_with_unlimited_arguments(one, two='hi', *args):
     56    return {
     57        'values': [one, two] + list(args)
     58    }
     59
     60register.inclusion_tag('for-tag01')(inclusion_tag_with_unlimited_arguments)
     61
     62
     63def inclusion_tag_with_only_unlimited_arguments(*args):
     64    return {
     65        'values': args
     66    }
     67
     68register.inclusion_tag('for-tag01')(inclusion_tag_with_only_unlimited_arguments)
     69 No newline at end of file
Back to Top