From bd79677e2999b545c27be8b2696c7f176ad78001 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 22 Feb 2010 23:41:29 +0000 Subject: [PATCH] [1.1.X] Fixed #11687: the `add` filter is now less failsome when faced with things that can't be coerced to integers. Backport of [12497] from trunk, although the fix here is slightly different to avoid adding new behavior to a bugfix branch. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12499 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaultfilters.py | 5 ++++- tests/regressiontests/templates/filters.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 03a294bc28..2af20aca42 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -650,7 +650,10 @@ unordered_list.needs_autoescape = True def add(value, arg): """Adds the arg to the value.""" - return int(value) + int(arg) + try: + return int(value) + int(arg) + except (ValueError, TypeError): + return value add.is_safe = False def get_digit(value, arg): diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py index d2e8e7473b..c02a161014 100644 --- a/tests/regressiontests/templates/filters.py +++ b/tests/regressiontests/templates/filters.py @@ -333,4 +333,10 @@ def get_filter_tests(): 'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'), #Ticket 9520: Make sure |date doesn't blow up on non-dates 'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''), + + # Ticket #11687: make sure that add works on int-able things. + 'add01': (r'{{ i|add:"5" }}', {'i': 2000}, '2005'), + 'add02': (r'{{ i|add:"napis" }}', {'i': 2000}, '2000'), + 'add03': (r'{{ i|add:16 }}', {'i': 'not_an_int'}, 'not_an_int'), + 'add04': (r'{{ i|add:"16" }}', {'i': 'not_an_int'}, 'not_an_int'), }