Fixed #19282 -- Restored ability to pass Decimals to intcomma filter

This commit is contained in:
Hernan Lozano 2013-02-23 13:19:21 -03:00 committed by Claude Paroz
parent 10026c2ad0
commit 7e6ad76b24
2 changed files with 7 additions and 5 deletions

View File

@ -1,6 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import re import re
from datetime import date, datetime from datetime import date, datetime
from decimal import Decimal
from django import template from django import template
from django.conf import settings from django.conf import settings
@ -35,7 +36,7 @@ def intcomma(value, use_l10n=True):
""" """
if settings.USE_L10N and use_l10n: if settings.USE_L10N and use_l10n:
try: try:
if not isinstance(value, float): if not isinstance(value, (float, Decimal)):
value = int(value) value = int(value)
except (TypeError, ValueError): except (TypeError, ValueError):
return intcomma(value, False) return intcomma(value, False)

View File

@ -1,5 +1,6 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
from decimal import Decimal
try: try:
import pytz import pytz
@ -58,20 +59,20 @@ class HumanizeTests(TestCase):
def test_intcomma(self): def test_intcomma(self):
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
'100', '1000', '10123', '10311', '1000000', '1234567.1234567', '100', '1000', '10123', '10311', '1000000', '1234567.1234567', Decimal('1234567.1234567'),
None) None)
result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25', result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567',
None) None)
self.humanize_tester(test_list, result_list, 'intcomma') self.humanize_tester(test_list, result_list, 'intcomma')
def test_l10n_intcomma(self): def test_l10n_intcomma(self):
test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25, test_list = (100, 1000, 10123, 10311, 1000000, 1234567.25,
'100', '1000', '10123', '10311', '1000000', '1234567.1234567', '100', '1000', '10123', '10311', '1000000', '1234567.1234567', Decimal('1234567.1234567'),
None) None)
result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25', result_list = ('100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.25',
'100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '100', '1,000', '10,123', '10,311', '1,000,000', '1,234,567.1234567', '1,234,567.1234567',
None) None)
with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False): with self.settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=False):