Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Claude Paroz authored October 08, 2013
57  django/forms/widgets.py
@@ -397,60 +397,27 @@ def render(self, name, value, attrs=None):
397 397
                            force_text(value))
398 398
 
399 399
 
400  
-class DateInput(TextInput):
  400
+class DateTimeBaseInput(TextInput):
  401
+    format_key = ''
401 402
     def __init__(self, attrs=None, format=None):
402  
-        super(DateInput, self).__init__(attrs)
403  
-        if format:
404  
-            self.format = format
405  
-            self.manual_format = True
406  
-        else:
407  
-            self.format = formats.get_format('DATE_INPUT_FORMATS')[0]
408  
-            self.manual_format = False
  403
+        super(DateTimeBaseInput, self).__init__(attrs)
  404
+        self.format = format if format else None
409 405
 
410 406
     def _format_value(self, value):
411  
-        if self.is_localized and not self.manual_format:
412  
-            return formats.localize_input(value)
413  
-        elif hasattr(value, 'strftime'):
414  
-            value = datetime_safe.new_date(value)
415  
-            return value.strftime(self.format)
416  
-        return value
  407
+        return formats.localize_input(value,
  408
+            self.format or formats.get_format(self.format_key)[0])
417 409
 
418 410
 
419  
-class DateTimeInput(TextInput):
420  
-    def __init__(self, attrs=None, format=None):
421  
-        super(DateTimeInput, self).__init__(attrs)
422  
-        if format:
423  
-            self.format = format
424  
-            self.manual_format = True
425  
-        else:
426  
-            self.format = formats.get_format('DATETIME_INPUT_FORMATS')[0]
427  
-            self.manual_format = False
  411
+class DateInput(DateTimeBaseInput):
  412
+    format_key = 'DATE_INPUT_FORMATS'
428 413
 
429  
-    def _format_value(self, value):
430  
-        if self.is_localized and not self.manual_format:
431  
-            return formats.localize_input(value)
432  
-        elif hasattr(value, 'strftime'):
433  
-            value = datetime_safe.new_datetime(value)
434  
-            return value.strftime(self.format)
435  
-        return value
436 414
 
  415
+class DateTimeInput(DateTimeBaseInput):
  416
+    format_key = 'DATETIME_INPUT_FORMATS'
437 417
 
438  
-class TimeInput(TextInput):
439  
-    def __init__(self, attrs=None, format=None):
440  
-        super(TimeInput, self).__init__(attrs)
441  
-        if format:
442  
-            self.format = format
443  
-            self.manual_format = True
444  
-        else:
445  
-            self.format = formats.get_format('TIME_INPUT_FORMATS')[0]
446  
-            self.manual_format = False
447 418
 
448  
-    def _format_value(self, value):
449  
-        if self.is_localized and not self.manual_format:
450  
-            return formats.localize_input(value)
451  
-        elif hasattr(value, 'strftime'):
452  
-            return value.strftime(self.format)
453  
-        return value
  419
+class TimeInput(DateTimeBaseInput):
  420
+    format_key = 'TIME_INPUT_FORMATS'
454 421
 
455 422
 
456 423
 # Defined at module level so that CheckboxInput is picklable (#17976)
14  tests/forms_tests/tests/test_widgets.py
@@ -12,7 +12,7 @@
12 12
 from django.utils import formats
13 13
 from django.utils.safestring import mark_safe
14 14
 from django.utils import six
15  
-from django.utils.translation import activate, deactivate
  15
+from django.utils.translation import activate, deactivate, override
16 16
 from django.test import TestCase
17 17
 from django.test.utils import override_settings
18 18
 from django.utils.encoding import python_2_unicode_compatible, force_text
@@ -997,26 +997,30 @@ def tearDown(self):
997 997
     def test_datetimeinput(self):
998 998
         w = DateTimeInput()
999 999
         d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
1000  
-        w.is_localized = True
1001 1000
         self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007 12:51:34" />')
1002 1001
 
1003 1002
     def test_dateinput(self):
1004 1003
         w = DateInput()
1005 1004
         d = datetime.date(2007, 9, 17)
1006  
-        w.is_localized = True
1007 1005
         self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17.09.2007" />')
1008 1006
 
1009 1007
     def test_timeinput(self):
1010 1008
         w = TimeInput()
1011 1009
         t = datetime.time(12, 51, 34, 482548)
1012  
-        w.is_localized = True
1013 1010
         self.assertHTMLEqual(w.render('time', t), '<input type="text" name="time" value="12:51:34" />')
1014 1011
 
  1012
+    def test_datetime_locale_aware(self):
  1013
+        w = DateTimeInput()
  1014
+        d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
  1015
+        with self.settings(USE_L10N=False):
  1016
+            self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="2007-09-17 12:51:34" />')
  1017
+        with override('es'):
  1018
+            self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17/09/2007 12:51:34" />')
  1019
+
1015 1020
     def test_splithiddendatetime(self):
1016 1021
         from django.forms.widgets import SplitHiddenDateTimeWidget
1017 1022
 
1018 1023
         w = SplitHiddenDateTimeWidget()
1019  
-        w.is_localized = True
1020 1024
         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" />')
1021 1025
 
1022 1026
     def test_nullbooleanselect(self):

0 notes on commit 00a73c1

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