From 00a73c1c699e3df2790caf56635647bd72e3cd21 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 8 Oct 2013 21:16:58 +0200 Subject: [PATCH] Fixed #21173 -- Stopped fixing format for date-based widgets at init time Thanks Marc Tamlyn for the review. --- django/forms/widgets.py | 59 ++++++------------------- tests/forms_tests/tests/test_widgets.py | 14 +++--- 2 files changed, 22 insertions(+), 51 deletions(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index f8246d60bf..67befcfbe0 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -397,60 +397,27 @@ class Textarea(Widget): 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 - - 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 DateInput(DateTimeBaseInput): + format_key = 'DATE_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 +class DateTimeInput(DateTimeBaseInput): + format_key = 'DATETIME_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'): - 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) diff --git a/tests/forms_tests/tests/test_widgets.py b/tests/forms_tests/tests/test_widgets.py index 2d667791e8..9cb36d66a2 100644 --- a/tests/forms_tests/tests/test_widgets.py +++ b/tests/forms_tests/tests/test_widgets.py @@ -12,7 +12,7 @@ from django.forms.widgets import RadioFieldRenderer 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 @@ class FormsI18NWidgetsTestCase(TestCase): 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), '') def test_dateinput(self): w = DateInput() d = datetime.date(2007, 9, 17) - w.is_localized = True self.assertHTMLEqual(w.render('date', d), '') def test_timeinput(self): w = TimeInput() t = datetime.time(12, 51, 34, 482548) - w.is_localized = True self.assertHTMLEqual(w.render('time', t), '') + 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), '') + with override('es'): + self.assertHTMLEqual(w.render('date', d), '') + 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)), '') def test_nullbooleanselect(self):