Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10288 -- Corrected _has_changed handling of DateTimeIn…

…put when a custom date/time format is in use. Thanks to Koen Biermans for the report and patch.

Merge of r10641 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10757 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 17d214a9821542696b4b632b1fc70086ed2f7ecc 1 parent cc96ed9
Russell Keith-Magee authored May 13, 2009
23  django/forms/widgets.py
@@ -295,14 +295,21 @@ def __init__(self, attrs=None, format=None):
295 295
         if format:
296 296
             self.format = format
297 297
 
298  
-    def render(self, name, value, attrs=None):
  298
+    def _format_value(self, value):
299 299
         if value is None:
300  
-            value = ''
  300
+            return ''
301 301
         elif hasattr(value, 'strftime'):
302 302
             value = datetime_safe.new_datetime(value)
303  
-            value = value.strftime(self.format)
  303
+            return value.strftime(self.format)
  304
+        return value
  305
+
  306
+    def render(self, name, value, attrs=None):
  307
+        value = self._format_value(value)
304 308
         return super(DateTimeInput, self).render(name, value, attrs)
305 309
 
  310
+    def _has_changed(self, initial, data):
  311
+        return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
  312
+
306 313
 class TimeInput(Input):
307 314
     input_type = 'text'
308 315
 
@@ -400,11 +407,11 @@ def render(self, name, value, attrs=None, choices=()):
400 407
 
401 408
     def value_from_datadict(self, data, files, name):
402 409
         value = data.get(name, None)
403  
-        return {u'2': True, 
404  
-                True: True, 
405  
-                'True': True, 
406  
-                u'3': False, 
407  
-                'False': False, 
  410
+        return {u'2': True,
  411
+                True: True,
  412
+                'True': True,
  413
+                u'3': False,
  414
+                'False': False,
408 415
                 False: False}.get(value, None)
409 416
 
410 417
     def _has_changed(self, initial, data):
11  tests/regressiontests/forms/widgets.py
@@ -1071,9 +1071,9 @@
1071 1071
 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
1072 1072
 u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
1073 1073
 
1074  
->>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00'])
  1074
+>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'2008-05-06', u'12:40:00'])
1075 1075
 False
1076  
->>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00'])
  1076
+>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'2008-05-06', u'12:41:00'])
1077 1077
 True
1078 1078
 
1079 1079
 # DateTimeInput ###############################################################
@@ -1093,6 +1093,13 @@
1093 1093
 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
1094 1094
 u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
1095 1095
 
  1096
+Use 'format' to change the way a value is displayed.
  1097
+>>> w = DateTimeInput(format='%d/%m/%Y %H:%M')
  1098
+>>> w.render('date', d)
  1099
+u'<input type="text" name="date" value="17/09/2007 12:51" />'
  1100
+>>> w._has_changed(d, '17/09/2007 12:51')
  1101
+False
  1102
+
1096 1103
 # TimeInput ###################################################################
1097 1104
 
1098 1105
 >>> w = TimeInput()

0 notes on commit 17d214a

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