Opened 16 years ago

Closed 16 years ago

Last modified 12 years ago

#7499 closed (fixed)

TimeField may contain the sub-second times but cannot validate them

Reported by: honeyman Owned by: Kevin McConnell
Component: Forms Version: dev
Severity: Keywords: time aug22sprint
Cc: kevin.mcconnell@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If we create a form containing a TimeField which has the initial value set to time object which contains the sub-second data (for example, to time.max), the form is generated with the value which cannot be validated.
Example:

>>> from django import newforms as forms
>>> from datetime import time
>>>
>>> class MyForm(forms.Form):
...     mytime = forms.TimeField(initial = time.max)
...
>>> f1 = MyForm()
>>> # Imagine we've posted this form and are now parsing the POST request
... f1a = MyForm({'mytime': str(time.max)})
>>>
>>> print f1
<tr><th><label for="id_mytime">Mytime:</label></th><td><input type="text" name="mytime" value="23:59
:59.999999" id="id_mytime" /></td></tr>
>>>
>>> print f1a
<tr><th><label for="id_mytime">Mytime:</label></th><td><ul class="errorlist"><li>Enter a valid time.
</li></ul><input type="text" name="mytime" value="23:59:59.999999" id="id_mytime" /></td></tr>
>>>
>>> print f1a.is_bound, f1a.is_valid()
True False

As Django (Python) cannot parse the sub-second time in the text string, my understanding is that Django should never print it in the field, trimming the value to just '%H:%M:%S'.

This is likely applicable to DateTimeField too.

Attachments (3)

time_input_widget.diff (3.7 KB ) - added by Kevin McConnell 16 years ago.
Adds a TimeInput widget
time_input_widget.2.diff (3.5 KB ) - added by Kevin McConnell 16 years ago.
Updated patch
time_input_widget.3.diff (1.3 KB ) - added by Kevin McConnell 16 years ago.
Check TimeInput argument type (patched against r8507)

Download all attachments as: .zip

Change History (14)

comment:1 by Eric Holscher, 16 years ago

milestone: 1.0 maybe
Triage Stage: UnreviewedDesign decision needed

comment:2 by Jacob, 16 years ago

milestone: 1.0 maybe1.0
Triage Stage: Design decision neededAccepted

comment:3 by Kevin McConnell, 16 years ago

Owner: changed from nobody to Kevin McConnell
Status: newassigned

by Kevin McConnell, 16 years ago

Attachment: time_input_widget.diff added

Adds a TimeInput widget

comment:4 by Kevin McConnell, 16 years ago

Cc: kevin.mcconnell@… added
Has patch: set
Keywords: aug22sprint added

The patch adds a TimeInput widget, which functions similarly to DateTimeInput but with a suitable format string.

by Kevin McConnell, 16 years ago

Attachment: time_input_widget.2.diff added

Updated patch

comment:5 by Kevin McConnell, 16 years ago

After discussion with Malcolm, I've updated the patch to truncate the time down to seconds (using replace(microsecond=0)) rather than using the format string (which wasn't quite so explicit).

comment:6 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: assignedclosed

Fixed in [8491].

comment:7 by honeyman, 16 years ago

Resolution: fixed
Status: closedreopened

I almost forgot about this ticket, but it hit me again just now...
After an "svn update" of Django, I just got "Caught an exception while rendering: replace() takes no keyword arguments" in /forms/widgets.py, line 311, in render "value = value.replace(microsecond=0)". Seems that I was trying to initialize this widget using an unicode string (rather than a datetime.time), and it worked perfectly before; but now it fails (cause string object also does have a replace() method, with a different syntax though). Should it be considered a regression, or does such widget now have to be initialized with datetime.time since now?

comment:8 by Kevin McConnell, 16 years ago

That's a good point, I didn't think about directly instantiating the widgets like that.

Perhaps it would be safer to check the type of the argument (since replace could be a common method name)? I know some folks have strong feelings about doing that in Python, but it seems like it would be safer.

I'll attach a new patch that does that.

by Kevin McConnell, 16 years ago

Attachment: time_input_widget.3.diff added

Check TimeInput argument type (patched against r8507)

comment:9 by Malcolm Tredinnick, 16 years ago

Triage Stage: AcceptedReady for checkin

comment:10 by Jacob, 16 years ago

Resolution: fixed
Status: reopenedclosed

(In [8549]) Updated TimeInput changes from [8491] to allow time widgets to be used with unicode values. Fixes #7499.

comment:11 by Jacob, 12 years ago

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top