Kaydet (Commit) e6836136 authored tarafından Tobias Kunze's avatar Tobias Kunze Kaydeden (comit) Mariusz Felisiak

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

üst e3968df5
...@@ -879,17 +879,15 @@ def pluralize(value, arg='s'): ...@@ -879,17 +879,15 @@ def pluralize(value, arg='s'):
singular_suffix, plural_suffix = bits[:2] singular_suffix, plural_suffix = bits[:2]
try: try:
if float(value) != 1: return singular_suffix if float(value) == 1 else plural_suffix
return plural_suffix
except ValueError: # Invalid string that's not a number. except ValueError: # Invalid string that's not a number.
pass pass
except TypeError: # Value isn't a string or a number; maybe it's a list? except TypeError: # Value isn't a string or a number; maybe it's a list?
try: try:
if len(value) != 1: return singular_suffix if len(value) == 1 else plural_suffix
return plural_suffix
except TypeError: # len() of unsized object. except TypeError: # len() of unsized object.
pass pass
return singular_suffix return ''
@register.filter("phone2numeric", is_safe=True) @register.filter("phone2numeric", is_safe=True)
......
...@@ -58,8 +58,9 @@ class FunctionTests(SimpleTestCase): ...@@ -58,8 +58,9 @@ class FunctionTests(SimpleTestCase):
self.assertEqual(pluralize(0, 'y,ies,error'), '') self.assertEqual(pluralize(0, 'y,ies,error'), '')
def test_no_len_type(self): def test_no_len_type(self):
self.assertEqual(pluralize(object(), 'y,es'), 'y') self.assertEqual(pluralize(object(), 'y,es'), '')
self.assertEqual(pluralize(object(), 'es'), '') self.assertEqual(pluralize(object(), 'es'), '')
def test_value_error(self): def test_value_error(self):
self.assertEqual(pluralize('', 'y,es'), 'y') self.assertEqual(pluralize('', 'y,es'), '')
self.assertEqual(pluralize('', 'es'), '')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment