Commit a28bd76b authored by Jannis Leidel's avatar Jannis Leidel
Browse files

Fixed #12777 - Localize decimal fields correctly. Thanks, Florian Apolloner

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12382 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent def34da8
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -80,11 +80,7 @@ def localize(value):
    formatted as a string using current locale format
    """
    if settings.USE_L10N:
        if isinstance(value, decimal.Decimal):
            return number_format(value)
        elif isinstance(value, float):
            return number_format(value)
        elif isinstance(value, int):
        if isinstance(value, (decimal.Decimal, float, int)):
            return number_format(value)
        elif isinstance(value, datetime.datetime):
            return date_format(value, 'DATETIME_FORMAT')
@@ -99,6 +95,8 @@ def localize_input(value, default=None):
    Checks if an input value is a localizable type and returns it
    formatted with the appropriate formatting string of the current locale.
    """
    if isinstance(value, (decimal.Decimal, float, int)):
        return number_format(value)
    if isinstance(value, datetime.datetime):
        value = datetime_safe.new_datetime(value)
        format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ class TestModel(models.Model):
class Company(models.Model):
    name = models.CharField(max_length=50)
    date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0))
    cents_payed = models.DecimalField(max_digits=4, decimal_places=2)

__test__ = {'API_TESTS': '''
>>> tm = TestModel()
+2 −1
Original line number Diff line number Diff line
@@ -362,12 +362,13 @@ class FormattingTests(TestCase):
            form6 = CompanyForm({
                'name': u'acme',
                'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
                'cents_payed': decimal.Decimal('59.47'),
            })
            form6.save()
            self.assertEqual(True, form6.is_valid())
            self.assertEqual(
                form6.as_ul(),
                u'<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" value="acme" maxlength="50" /></li>\n<li><label for="id_date_added">Date added:</label> <input type="text" name="date_added" value="31.12.2009 06:00:00" id="id_date_added" /></li>'
                u'<li><label for="id_name">Name:</label> <input id="id_name" type="text" name="name" value="acme" maxlength="50" /></li>\n<li><label for="id_date_added">Date added:</label> <input type="text" name="date_added" value="31.12.2009 06:00:00" id="id_date_added" /></li>\n<li><label for="id_cents_payed">Cents payed:</label> <input type="text" name="cents_payed" value="59,47" id="id_cents_payed" /></li>'
            )
            self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00')
            self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])