Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…time

Thanks Marc Tamlyn for the review.
  • Loading branch information...
commit 00a73c1c699e3df2790caf56635647bd72e3cd21 1 parent 18ea1c4
@claudep claudep authored
Showing with 21 additions and 50 deletions.
  1. +12 −45 django/forms/widgets.py
  2. +9 −5 tests/forms_tests/tests/test_widgets.py
View
57 django/forms/widgets.py
@@ -397,60 +397,27 @@ def render(self, name, value, attrs=None):
force_text(value))
-class DateInput(TextInput):
+class DateTimeBaseInput(TextInput):
+ format_key = ''
def __init__(self, attrs=None, format=None):
- super(DateInput, self).__init__(attrs)
- if format:
- self.format = format
- self.manual_format = True
- else:
- self.format = formats.get_format('DATE_INPUT_FORMATS')[0]
- self.manual_format = False
+ super(DateTimeBaseInput, self).__init__(attrs)
+ self.format = format if format else None
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_date(value)
- return value.strftime(self.format)
- return value
+ return formats.localize_input(value,
+ self.format or formats.get_format(self.format_key)[0])
-class DateTimeInput(TextInput):
- def __init__(self, attrs=None, format=None):
- 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
+class DateInput(DateTimeBaseInput):
+ format_key = 'DATE_INPUT_FORMATS'
- 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 DateTimeInput(DateTimeBaseInput):
+ format_key = 'DATETIME_INPUT_FORMATS'
-class TimeInput(TextInput):
- def __init__(self, attrs=None, format=None):
- 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:
- return formats.localize_input(value)
- elif hasattr(value, 'strftime'):
- return value.strftime(self.format)
- return value
+class TimeInput(DateTimeBaseInput):
+ format_key = 'TIME_INPUT_FORMATS'
# Defined at module level so that CheckboxInput is picklable (#17976)
View
14 tests/forms_tests/tests/test_widgets.py
@@ -12,7 +12,7 @@
from django.utils import formats
from django.utils.safestring import mark_safe
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.utils import override_settings
from django.utils.encoding import python_2_unicode_compatible, force_text
@@ -997,26 +997,30 @@ def tearDown(self):
def test_datetimeinput(self):
w = DateTimeInput()
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" />')
def test_dateinput(self):
w = DateInput()
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" />')
def test_timeinput(self):
w = TimeInput()
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" />')
+ 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):
from django.forms.widgets import 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" />')
def test_nullbooleanselect(self):

0 comments on commit 00a73c1

Please sign in to comment.
Something went wrong with that request. Please try again.