Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17165 -- Fixed `SelectDateWidget._has_changed()` to work corre…

…ctly with a localized date format.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17071 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b2948d81798e78e0b3bcb016bd4efa4d66bc5d8e 1 parent af7688d
Julien Phalip jphalip authored
5 django/forms/extras/widgets.py
View
@@ -134,3 +134,8 @@ def create_select(self, name, field, value, val, choices):
s = Select(choices=choices)
select_html = s.render(field % name, val, local_attrs)
return select_html
+
+ def _has_changed(self, initial, data):
+ input_format = get_format('DATE_INPUT_FORMATS')[0]
+ data = datetime_safe.datetime.strptime(data, input_format).date()
+ return super(SelectDateWidget, self)._has_changed(initial, data)
57 tests/regressiontests/forms/tests/extra.py
View
@@ -17,6 +17,9 @@
class GetDate(Form):
mydate = DateField(widget=SelectDateWidget)
+class GetDateShowHiddenInitial(Form):
+ mydate = DateField(widget=SelectDateWidget, show_hidden_initial=True)
+
class FormsExtraTestCase(unittest.TestCase, AssertFormErrorsMixin):
###############
# Extra stuff #
@@ -697,6 +700,60 @@ def test_l10n(self):
w = SelectDateWidget(years=('1899',))
self.assertEqual(w.value_from_datadict({'date_year': '1899', 'date_month': '8', 'date_day': '13'}, {}, 'date'), '13-08-1899')
+ def test_l10n_date_changed(self):
+ """
+ Ensure that SelectDateWidget._has_changed() works correctly with a
+ localized date format.
+ Refs #17165.
+ """
+ # With Field.show_hidden_initial=False -----------------------
+ b = GetDate({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '1',
+ }, initial={'mydate': datetime.date(2008, 4, 1)})
+ self.assertFalse(b.has_changed())
+
+ b = GetDate({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '2',
+ }, initial={'mydate': datetime.date(2008, 4, 1)})
+ self.assertTrue(b.has_changed())
+
+ # With Field.show_hidden_initial=True ------------------------
+ b = GetDateShowHiddenInitial({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '1',
+ 'initial-mydate': HiddenInput()._format_value(datetime.date(2008, 4, 1))
+ }, initial={'mydate': datetime.date(2008, 4, 1)})
+ self.assertFalse(b.has_changed())
+
+ b = GetDateShowHiddenInitial({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '22',
+ 'initial-mydate': HiddenInput()._format_value(datetime.date(2008, 4, 1))
+ }, initial={'mydate': datetime.date(2008, 4, 1)})
+ self.assertTrue(b.has_changed())
+
+ b = GetDateShowHiddenInitial({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '22',
+ 'initial-mydate': HiddenInput()._format_value(datetime.date(2008, 4, 1))
+ }, initial={'mydate': datetime.date(2008, 4, 22)})
+ self.assertTrue(b.has_changed())
+
+ b = GetDateShowHiddenInitial({
+ 'mydate_year': '2008',
+ 'mydate_month': '4',
+ 'mydate_day': '22',
+ 'initial-mydate': HiddenInput()._format_value(datetime.date(2008, 4, 22))
+ }, initial={'mydate': datetime.date(2008, 4, 1)})
+ self.assertFalse(b.has_changed())
+
def test_l10n_invalid_date_in(self):
# Invalid dates shouldn't be allowed
a = GetDate({'mydate_month':'2', 'mydate_day':'31', 'mydate_year':'2010'})
Please sign in to comment.
Something went wrong with that request. Please try again.