Ticket #12777: localize_input.diff

File localize_input.diff, 3.2 KB (added by Florian Apolloner, 15 years ago)
  • django/utils/formats.py

    diff --git a/django/utils/formats.py b/django/utils/formats.py
    index 77479ae..ad6127e 100644
    a b def localize(value):  
    8080    formatted as a string using current locale format
    8181    """
    8282    if settings.USE_L10N:
    83         if isinstance(value, decimal.Decimal):
    84             return number_format(value)
    85         elif isinstance(value, float):
    86             return number_format(value)
    87         elif isinstance(value, int):
     83        if isinstance(value, (decimal.Decimal, float, int)):
    8884            return number_format(value)
    8985        elif isinstance(value, datetime.datetime):
    9086            return date_format(value, 'DATETIME_FORMAT')
    def localize_input(value, default=None):  
    9995    Checks if an input value is a localizable type and returns it
    10096    formatted with the appropriate formatting string of the current locale.
    10197    """
     98
     99    if isinstance(value, (decimal.Decimal, float, int)):
     100        return number_format(value)
    102101    if isinstance(value, datetime.datetime):
    103102        value = datetime_safe.new_datetime(value)
    104103        format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
  • tests/regressiontests/i18n/models.py

    diff --git a/tests/regressiontests/i18n/models.py b/tests/regressiontests/i18n/models.py
    index 56f1585..2ff5fc4 100644
    a b class TestModel(models.Model):  
    88class Company(models.Model):
    99    name = models.CharField(max_length=50)
    1010    date_added = models.DateTimeField(default=datetime(1799,1,31,23,59,59,0))
     11    cents_payed = models.DecimalField(max_digits=4, decimal_places=2)
    1112
    1213__test__ = {'API_TESTS': '''
    1314>>> tm = TestModel()
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index c2a39cd..e9ba9a7 100644
    a b class FormattingTests(TestCase):  
    362362            form6 = CompanyForm({
    363363                'name': u'acme',
    364364                'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
     365                'cents_payed': decimal.Decimal("59.47"),
    365366            })
    366367            form6.save()
    367368            self.assertEqual(True, form6.is_valid())
    368369            self.assertEqual(
    369370                form6.as_ul(),
    370                 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>'
     371                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>'
    371372            )
    372373            self.assertEqual(localize_input(datetime.datetime(2009, 12, 31, 6, 0, 0)), '31.12.2009 06:00:00')
    373374            self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])
Back to Top