mirror of https://github.com/django/django.git
Fixed #20122 -- Made pluralize template filter return '' on invalid input.
This commit is contained in:
parent
e3968df527
commit
e683613649
|
@ -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)
|
||||
|
|
|
@ -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'), '')
|
||||
|
|
Loading…
Reference in New Issue