Fixed #21173 -- Stopped fixing format for date-based widgets at init time

Thanks Marc Tamlyn for the review.
This commit is contained in:
Claude Paroz 2013-10-08 21:16:58 +02:00
parent 18ea1c473b
commit 00a73c1c69
2 changed files with 22 additions and 51 deletions

View File

@ -397,60 +397,27 @@ class Textarea(Widget):
force_text(value)) force_text(value))
class DateInput(TextInput): class DateTimeBaseInput(TextInput):
format_key = ''
def __init__(self, attrs=None, format=None): def __init__(self, attrs=None, format=None):
super(DateInput, self).__init__(attrs) super(DateTimeBaseInput, self).__init__(attrs)
if format: self.format = format if format else None
self.format = format
self.manual_format = True
else:
self.format = formats.get_format('DATE_INPUT_FORMATS')[0]
self.manual_format = False
def _format_value(self, value): def _format_value(self, value):
if self.is_localized and not self.manual_format: return formats.localize_input(value,
return formats.localize_input(value) self.format or formats.get_format(self.format_key)[0])
elif hasattr(value, 'strftime'):
value = datetime_safe.new_date(value)
return value.strftime(self.format)
return value
class DateTimeInput(TextInput): class DateInput(DateTimeBaseInput):
def __init__(self, attrs=None, format=None): format_key = 'DATE_INPUT_FORMATS'
super(DateTimeInput, self).__init__(attrs)
if format:
self.format = format
self.manual_format = True
else:
self.format = formats.get_format('DATETIME_INPUT_FORMATS')[0]
self.manual_format = False
def _format_value(self, value):
if self.is_localized and not self.manual_format:
return formats.localize_input(value)
elif hasattr(value, 'strftime'):
value = datetime_safe.new_datetime(value)
return value.strftime(self.format)
return value
class TimeInput(TextInput): class DateTimeInput(DateTimeBaseInput):
def __init__(self, attrs=None, format=None): format_key = 'DATETIME_INPUT_FORMATS'
super(TimeInput, self).__init__(attrs)
if format:
self.format = format
self.manual_format = True
else:
self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
self.manual_format = False
def _format_value(self, value):
if self.is_localized and not self.manual_format: class TimeInput(DateTimeBaseInput):
return formats.localize_input(value) format_key = 'TIME_INPUT_FORMATS'
elif hasattr(value, 'strftime'):
return value.strftime(self.format)
return value
# Defined at module level so that CheckboxInput is picklable (#17976) # Defined at module level so that CheckboxInput is picklable (#17976)

View File

@ -12,7 +12,7 @@ from django.forms.widgets import RadioFieldRenderer
from django.utils import formats from django.utils import formats
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
from django.utils.translation import activate, deactivate from django.utils.translation import activate, deactivate, override
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.encoding import python_2_unicode_compatible, force_text from django.utils.encoding import python_2_unicode_compatible, force_text
@ -997,26 +997,30 @@ class FormsI18NWidgetsTestCase(TestCase):
def test_datetimeinput(self): def test_datetimeinput(self):
w = DateTimeInput() w = DateTimeInput()
d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548) d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
w.is_localized = True
self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007 12:51:34" />') self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007 12:51:34" />')
def test_dateinput(self): def test_dateinput(self):
w = DateInput() w = DateInput()
d = datetime.date(2007, 9, 17) d = datetime.date(2007, 9, 17)
w.is_localized = True
self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007" />') self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007" />')
def test_timeinput(self): def test_timeinput(self):
w = TimeInput() w = TimeInput()
t = datetime.time(12, 51, 34, 482548) t = datetime.time(12, 51, 34, 482548)
w.is_localized = True
self.assertHTMLEqual(w.render('time', t), '<input type="text" name="time" value="12:51:34" />') self.assertHTMLEqual(w.render('time', t), '<input type="text" name="time" value="12:51:34" />')
def test_datetime_locale_aware(self):
w = DateTimeInput()
d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
with self.settings(USE_L10N=False):
self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="2007-09-17 12:51:34" />')
with override('es'):
self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17/09/2007 12:51:34" />')
def test_splithiddendatetime(self): def test_splithiddendatetime(self):
from django.forms.widgets import SplitHiddenDateTimeWidget from django.forms.widgets import SplitHiddenDateTimeWidget
w = SplitHiddenDateTimeWidget() w = SplitHiddenDateTimeWidget()
w.is_localized = True
self.assertHTMLEqual(w.render('date', datetime.datetime(2007, 9, 17, 12, 51)), '<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />') self.assertHTMLEqual(w.render('date', datetime.datetime(2007, 9, 17, 12, 51)), '<input type="hidden" name="date_0" value="17.09.2007" /><input type="hidden" name="date_1" value="12:51:00" />')
def test_nullbooleanselect(self): def test_nullbooleanselect(self):