Fixed #20122 -- Made pluralize template filter return '' on invalid input.

This commit is contained in:
Tobias Kunze 2019-04-27 18:09:22 +02:00 committed by Mariusz Felisiak
parent e3968df527
commit e683613649
2 changed files with 6 additions and 7 deletions

View File

@ -879,17 +879,15 @@ def pluralize(value, arg='s'):
singular_suffix, plural_suffix = bits[:2]
try:
if float(value) != 1:
return plural_suffix
return singular_suffix if float(value) == 1 else plural_suffix
except ValueError: # Invalid string that's not a number.
pass
except TypeError: # Value isn't a string or a number; maybe it's a list?
try:
if len(value) != 1:
return plural_suffix
return singular_suffix if len(value) == 1 else plural_suffix
except TypeError: # len() of unsized object.
pass
return singular_suffix
return ''
@register.filter("phone2numeric", is_safe=True)

View File

@ -58,8 +58,9 @@ class FunctionTests(SimpleTestCase):
self.assertEqual(pluralize(0, 'y,ies,error'), '')
def test_no_len_type(self):
self.assertEqual(pluralize(object(), 'y,es'), 'y')
self.assertEqual(pluralize(object(), 'y,es'), '')
self.assertEqual(pluralize(object(), 'es'), '')
def test_value_error(self):
self.assertEqual(pluralize('', 'y,es'), 'y')
self.assertEqual(pluralize('', 'y,es'), '')
self.assertEqual(pluralize('', 'es'), '')