Ticket #13956: patch_r15283.diff
File patch_r15283.diff, 10.7 KB (added by , 14 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): 796 796 return '' 797 797 return _render_value_in_context(output, context) 798 798 799 def generic_tag_compiler(params, defaults, name, node_class, parser, token ):799 def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None): 800 800 "Returns a template.Node subclass." 801 801 bits = token.split_contents()[1:] 802 802 bmax = len(params) 803 803 def_len = defaults and len(defaults) or 0 804 804 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) 811 816 return node_class(bits) 812 817 813 818 class Library(object): … … class Library(object): 865 870 866 871 def simple_tag(self, func=None, takes_context=None): 867 872 def dec(func): 868 params, xx, xxx, defaults = getargspec(func)873 params, varargs, xxx, defaults = getargspec(func) 869 874 if takes_context: 870 875 if params[0] == 'context': 871 876 params = params[1:] … … class Library(object): 884 889 func_args = resolved_vars 885 890 return func(*func_args) 886 891 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) 888 893 compile_func.__doc__ = func.__doc__ 889 894 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 890 895 return func … … class Library(object): 900 905 901 906 def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 902 907 def dec(func): 903 params, xx, xxx, defaults = getargspec(func)908 params, varargs, xxx, defaults = getargspec(func) 904 909 if takes_context: 905 910 if params[0] == 'context': 906 911 params = params[1:] … … class Library(object): 935 940 if csrf_token is not None: 936 941 new_context['csrf_token'] = csrf_token 937 942 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) 940 944 compile_func.__doc__ = func.__doc__ 941 945 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 942 946 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): 39 39 return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg) 40 40 params_and_context.anything = "Expected params_and_context __dict__" 41 41 42 43 def simple_tag_with_three_arguments(one, two, three): 44 return ', '.join((one, two, three)) 45 46 register.simple_tag(simple_tag_with_three_arguments) 47 48 49 def simple_tag_with_two_default_arguments(one, two='2', three='3'): 50 return ', '.join((one, two, three)) 51 52 register.simple_tag(simple_tag_with_two_default_arguments) 53 54 55 def simple_tag_with_unlimited_arguments(one, two='2', *args): 56 return ', '.join([one, two] + list(args)) 57 58 register.simple_tag(simple_tag_with_unlimited_arguments) 59 60 61 def simple_tag_with_only_unlimited_arguments(*args): 62 return ', '.join(args) 63 64 register.simple_tag(simple_tag_with_only_unlimited_arguments) 65 66 67 def inclusion_tag_with_two_arguments(one, two): 68 return { 69 'first': one, 70 'second': two 71 } 72 73 register.inclusion_tag('basic-syntax03')(inclusion_tag_with_two_arguments) 74 75 76 def inclusion_tag_with_one_default_argument(one, two='hi'): 77 return { 78 'first': one, 79 'second': two 80 } 81 82 register.inclusion_tag('basic-syntax03')(inclusion_tag_with_one_default_argument) 83 84 85 def inclusion_tag_with_unlimited_arguments(one, two='hi', *args): 86 return { 87 'values': [one, two] + list(args) 88 } 89 90 register.inclusion_tag('for-tag01')(inclusion_tag_with_unlimited_arguments) 91 92 93 def inclusion_tag_with_only_unlimited_arguments(*args): 94 return { 95 'values': args 96 } 97 98 register.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): 1526 1526 'static-prefixtag02': ('{% load static %}{% get_static_prefix as static_prefix %}{{ static_prefix }}', {}, settings.STATIC_URL), 1527 1527 'static-prefixtag03': ('{% load static %}{% get_media_prefix %}', {}, settings.MEDIA_URL), 1528 1528 '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), 1529 1579 } 1530 1580 1531 1581 class TemplateTagLoading(unittest.TestCase):