Author: mtredinnick
Date: 2008-08-23 12:33:09 -0500 (Sat, 23 Aug 2008)
New Revision: 8491

Modified:
   django/trunk/django/forms/fields.py
   django/trunk/django/forms/widgets.py
   django/trunk/tests/regressiontests/forms/widgets.py
Log:
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.


Modified: django/trunk/django/forms/fields.py
===================================================================
--- django/trunk/django/forms/fields.py 2008-08-23 17:28:12 UTC (rev 8490)
+++ django/trunk/django/forms/fields.py 2008-08-23 17:33:09 UTC (rev 8491)
@@ -27,14 +27,14 @@
 from django.utils.encoding import smart_unicode, smart_str
 
 from util import ErrorList, ValidationError
-from widgets import TextInput, PasswordInput, HiddenInput, 
MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, 
SelectMultiple, DateTimeInput
+from widgets import TextInput, PasswordInput, HiddenInput, 
MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, 
SelectMultiple, DateTimeInput, TimeInput
 from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
 
 __all__ = (
     'Field', 'CharField', 'IntegerField',
     'DEFAULT_DATE_INPUT_FORMATS', 'DateField',
     'DEFAULT_TIME_INPUT_FORMATS', 'TimeField',
-    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField',
+    'DEFAULT_DATETIME_INPUT_FORMATS', 'DateTimeField', 'TimeField',
     'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
     'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
     'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
@@ -311,6 +311,7 @@
 )
 
 class TimeField(Field):
+    widget = TimeInput()
     default_error_messages = {
         'invalid': _(u'Enter a valid time.')
     }

Modified: django/trunk/django/forms/widgets.py
===================================================================
--- django/trunk/django/forms/widgets.py        2008-08-23 17:28:12 UTC (rev 
8490)
+++ django/trunk/django/forms/widgets.py        2008-08-23 17:33:09 UTC (rev 
8491)
@@ -22,7 +22,7 @@
 __all__ = (
     'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
     'HiddenInput', 'MultipleHiddenInput',
-    'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
+    'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
     'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
     'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
 )
@@ -301,6 +301,16 @@
             value = value.strftime(self.format)
         return super(DateTimeInput, self).render(name, value, attrs)
 
+class TimeInput(Input):
+    input_type = 'text'
+
+    def render(self, name, value, attrs=None):
+        if value is None:
+            value = ''
+        elif hasattr(value, 'replace'):
+            value = value.replace(microsecond=0)
+        return super(TimeInput, self).render(name, value, attrs)
+
 class CheckboxInput(Widget):
     def __init__(self, attrs=None, check_test=bool):
         super(CheckboxInput, self).__init__(attrs)

Modified: django/trunk/tests/regressiontests/forms/widgets.py
===================================================================
--- django/trunk/tests/regressiontests/forms/widgets.py 2008-08-23 17:28:12 UTC 
(rev 8490)
+++ django/trunk/tests/regressiontests/forms/widgets.py 2008-08-23 17:33:09 UTC 
(rev 8491)
@@ -1087,4 +1087,22 @@
 u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
 >>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
 u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
+
+# TimeInput ###############################################################
+
+>>> w = TimeInput()
+>>> w.render('time', None)
+u'<input type="text" name="time" />'
+>>> t = datetime.time(12, 51, 34, 482548)
+>>> print t
+12:51:34.482548
+
+The microseconds are trimmed on display, by default.
+>>> w.render('time', t)
+u'<input type="text" name="time" value="12:51:34" />'
+>>> w.render('time', datetime.time(12, 51, 34))
+u'<input type="text" name="time" value="12:51:34" />'
+>>> w.render('time', datetime.time(12, 51))
+u'<input type="text" name="time" value="12:51:00" />'
 """
+


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to