Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#7499 closed (fixed)

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

Reported by: honeyman Owned by: kevin
Component: Forms Version: master
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: UI/UX:

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 7 years ago.
Adds a TimeInput widget
time_input_widget.2.diff (3.5 KB) - added by kevin 7 years ago.
Updated patch
time_input_widget.3.diff (1.3 KB) - added by kevin 7 years ago.
Check TimeInput argument type (patched against r8507)

Download all attachments as: .zip

Change History (14)

comment:1 Changed 7 years ago by ericholscher

  • milestone set to 1.0 maybe
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by jacob

  • milestone changed from 1.0 maybe to 1.0
  • Triage Stage changed from Design decision needed to Accepted

comment:3 Changed 7 years ago by kevin

  • Owner changed from nobody to kevin
  • Status changed from new to assigned

Changed 7 years ago by kevin

Adds a TimeInput widget

comment:4 Changed 7 years ago by kevin

  • 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.

Changed 7 years ago by kevin

Updated patch

comment:5 Changed 7 years ago by kevin

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 Changed 7 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

Fixed in [8491].

comment:7 Changed 7 years ago by honeyman

  • Resolution fixed deleted
  • Status changed from closed to reopened

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 Changed 7 years ago by kevin

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.

Changed 7 years ago by kevin

Check TimeInput argument type (patched against r8507)

comment:9 Changed 7 years ago by mtredinnick

  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 7 years ago by jacob

  • Resolution set to fixed
  • Status changed from reopened to closed

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

comment:11 Changed 4 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

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