Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7499 -- Trim microseconds off rendering of form.TimeFields by …

…default so

that they validate. Previous code didn't work with microseconds anyway, so this
is backwards compatible. Thanks to kevin for the patch.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8491 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8a3ef1f8bce338b2ca255177b4ba6d020fd05bca 1 parent 646f2f6
Malcolm Tredinnick authored
5  django/forms/fields.py
@@ -27,14 +27,14 @@
27 27
 from django.utils.encoding import smart_unicode, smart_str
28 28
 
29 29
 from util import ErrorList, ValidationError
30  
-from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
  30
+from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput
31 31
 from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
32 32
 
33 33
 __all__ = (
34 34
     'Field', 'CharField', 'IntegerField',
35 35
     'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
36 36
     'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
37  
-    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
  37
+    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', 'TimeField',
38 38
     'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
39 39
     'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
40 40
     'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
@@ -311,6 +311,7 @@ def clean(self, value):
311 311
 )
312 312
 
313 313
 class TimeField(Field):
  314
+    widget = TimeInput()
314 315
     default_error_messages = {
315 316
         'invalid': _(u'Enter a valid time.')
316 317
     }
12  django/forms/widgets.py
@@ -22,7 +22,7 @@
22 22
 __all__ = (
23 23
     'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
24 24
     'HiddenInput', 'MultipleHiddenInput',
25  
-    'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
  25
+    'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
26 26
     'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
27 27
     'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
28 28
 )
@@ -301,6 +301,16 @@ def render(self, name, value, attrs=None):
301 301
             value = value.strftime(self.format)
302 302
         return super(DateTimeInput, self).render(name, value, attrs)
303 303
 
  304
+class TimeInput(Input):
  305
+    input_type = 'text'
  306
+
  307
+    def render(self, name, value, attrs=None):
  308
+        if value is None:
  309
+            value = ''
  310
+        elif hasattr(value, 'replace'):
  311
+            value = value.replace(microsecond=0)
  312
+        return super(TimeInput, self).render(name, value, attrs)
  313
+
304 314
 class CheckboxInput(Widget):
305 315
     def __init__(self, attrs=None, check_test=bool):
306 316
         super(CheckboxInput, self).__init__(attrs)
18  tests/regressiontests/forms/widgets.py
@@ -1087,4 +1087,22 @@
1087 1087
 u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
1088 1088
 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
1089 1089
 u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
  1090
+
  1091
+# TimeInput ###############################################################
  1092
+
  1093
+>>> w = TimeInput()
  1094
+>>> w.render('time', None)
  1095
+u'<input type="text" name="time" />'
  1096
+>>> t = datetime.time(12, 51, 34, 482548)
  1097
+>>> print t
  1098
+12:51:34.482548
  1099
+
  1100
+The microseconds are trimmed on display, by default.
  1101
+>>> w.render('time', t)
  1102
+u'<input type="text" name="time" value="12:51:34" />'
  1103
+>>> w.render('time', datetime.time(12, 51, 34))
  1104
+u'<input type="text" name="time" value="12:51:34" />'
  1105
+>>> w.render('time', datetime.time(12, 51))
  1106
+u'<input type="text" name="time" value="12:51:00" />'
1090 1107
 """
  1108
+

0 notes on commit 8a3ef1f

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