Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11687: the `add` filter is now less failsome when face…

…d 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
  • Loading branch information...
commit bd79677e2999b545c27be8b2696c7f176ad78001 1 parent 8c4f166
Jacob Kaplan-Moss authored February 22, 2010
5  django/template/defaultfilters.py
@@ -650,7 +650,10 @@ def _helper(list_, tabs=1):
650 650
 
651 651
 def add(value, arg):
652 652
     """Adds the arg to the value."""
653  
-    return int(value) + int(arg)
  653
+    try:
  654
+        return int(value) + int(arg)
  655
+    except (ValueError, TypeError):
  656
+        return value
654 657
 add.is_safe = False
655 658
 
656 659
 def get_digit(value, arg):
6  tests/regressiontests/templates/filters.py
@@ -333,4 +333,10 @@ def get_filter_tests():
333 333
         'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'),
334 334
         #Ticket 9520: Make sure |date doesn't blow up on non-dates
335 335
         'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''),
  336
+
  337
+         # Ticket #11687: make sure that add works on int-able things.
  338
+         'add01': (r'{{ i|add:"5" }}', {'i': 2000}, '2005'),
  339
+         'add02': (r'{{ i|add:"napis" }}', {'i': 2000}, '2000'),
  340
+         'add03': (r'{{ i|add:16 }}', {'i': 'not_an_int'}, 'not_an_int'),
  341
+         'add04': (r'{{ i|add:"16" }}', {'i': 'not_an_int'}, 'not_an_int'),
336 342
     }

0 notes on commit bd79677

Please sign in to comment.
Something went wrong with that request. Please try again.