[1.6.x] Fixed #18777 -- Localized form fields with as_text/as_hidden

Thanks croldan for the report.

Backport of 893d8de6f5 from master
This commit is contained in:
Aleksandra Sendecka 2013-02-24 14:57:29 +01:00 committed by Tim Graham
parent 196cc875b2
commit 6e4fd816c4
2 changed files with 36 additions and 0 deletions

View File

@ -118,6 +118,8 @@ class Field(object):
super(Field, self).__init__()
def prepare_value(self, value):
if self.widget.is_localized:
value = formats.localize_input(value)
return value
def to_python(self, value):
@ -460,6 +462,7 @@ class DateTimeField(BaseTemporalField):
}
def prepare_value(self, value):
value = super(DateTimeField, self).prepare_value(value)
if isinstance(value, datetime.datetime):
value = to_current_timezone(value)
return value

View File

@ -774,6 +774,39 @@ class FormattingTests(TransRealMixin, TestCase):
self.assertEqual(template2.render(context), output2)
self.assertEqual(template3.render(context), output3)
def test_localized_as_text_as_hidden_input(self):
"""
Tests if form input with 'as_hidden' or 'as_text' is correctly localized. Ticket #18777
"""
self.maxDiff = 1200
with translation.override('de-at', deactivate=True):
template = Template('{% load l10n %}{{ form.date_added }}; {{ form.cents_paid }}')
template_as_text = Template('{% load l10n %}{{ form.date_added.as_text }}; {{ form.cents_paid.as_text }}')
template_as_hidden = Template('{% load l10n %}{{ form.date_added.as_hidden }}; {{ form.cents_paid.as_hidden }}')
form = CompanyForm({
'name': 'acme',
'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
'cents_paid': decimal.Decimal('59.47'),
'products_delivered': 12000,
})
context = Context({'form': form })
self.assertTrue(form.is_valid())
self.assertHTMLEqual(
template.render(context),
'<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="text" value="59,47" />'
)
self.assertHTMLEqual(
template_as_text.render(context),
'<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="text" value="59,47" />'
)
self.assertHTMLEqual(
template_as_hidden.render(context),
'<input id="id_date_added" name="date_added" type="hidden" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="hidden" value="59,47" />'
)
class MiscTests(TransRealMixin, TestCase):
def setUp(self):