Skip to content

Commit

Permalink
Fixed #11687: the add filter is now less failsome when faced with t…
Browse files Browse the repository at this point in the history
…hings that can't be coerced to integers.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12497 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Feb 22, 2010
1 parent eb0751a commit 49d6a82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion django/template/defaultfilters.py
Expand Up @@ -651,7 +651,13 @@ def _helper(list_, tabs=1):


def add(value, arg): def add(value, arg):
"""Adds the arg to the value.""" """Adds the arg to the value."""
return int(value) + int(arg) try:
return int(value) + int(arg)
except (ValueError, TypeError):
try:
return value + arg
except:
return value
add.is_safe = False add.is_safe = False


def get_digit(value, arg): def get_digit(value, arg):
Expand Down
21 changes: 21 additions & 0 deletions docs/ref/templates/builtins.txt
Expand Up @@ -1010,6 +1010,27 @@ For example::


If ``value`` is ``4``, then the output will be ``6``. If ``value`` is ``4``, then the output will be ``6``.


.. versionchanged:: 1.2
The following behavior didn't exist in previous Django versions.

This filter will first try to coerce both values to integers. If this fails,
it'll attempt to add the values together anyway. This will work on some data
types (strings, list, etc.) and fail on others. If it fails, the result will
be an empty string.

For example, if we have::

{{ first|add:second }}

and ``first`` is ``[1, 2, 3]`` and ``second`` is ``[4, 5, 6]``, then the
output will be ``[1, 2, 3, 4, 5, 6]``.

.. warning::

Keep in mind that strings that can both be coerced to integers will be,
and thus will be will be *summed*, not concatenated, as in the first
example above.

.. templatefilter:: addslashes .. templatefilter:: addslashes


addslashes addslashes
Expand Down
11 changes: 11 additions & 0 deletions tests/regressiontests/templates/filters.py
Expand Up @@ -333,4 +333,15 @@ def get_filter_tests():
'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'), 'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'),
#Ticket 9520: Make sure |date doesn't blow up on non-dates #Ticket 9520: Make sure |date doesn't blow up on non-dates
'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''), 'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''),

# base tests for add that assert, that old behaviour, which means
# trying to add two values as ints, is kept
'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_int16'),
# further additions using dynamic typing
'add05': (r'{{ l1|add:l2 }}', {'l1': [1, 2], 'l2': [3, 4]}, '[1, 2, 3, 4]'),
'add06': (r'{{ t1|add:t2 }}', {'t1': (3, 4), 't2': (1, 2)}, '(3, 4, 1, 2)'),
'add07': (r'{{ d|add:t }}', {'d': date(2000, 1, 1), 't': timedelta(10)}, '2000-01-11'),
} }

0 comments on commit 49d6a82

Please sign in to comment.