Opened 18 years ago

Closed 17 years ago

Last modified 17 years ago

#4317 closed (duplicate)

Fix compress() in SplitDateTimeField

Reported by: ctdecci@… Owned by:
Component: Forms Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a SplitDateTimeField is not required, and one or both of the date/time fields is None, then datetime.datetime.combine() raises an exception within the compress() method. The current test "if data_list:" (shown below) will only fail if passed an empty list/tuple. If data_list is a non-empty list containing a None value (because one or both of the date/time fields are blank), then the "if data_list:" test still passes, and datetime.datetime.combine() raises an exception.

Snippet from compress() method of SplitDateTimeField class:

if data_list:
    return datetime.datetime.combine(*data_list)

Given this model:

from django.db import models

class Foo(models.Model):
    date = models.DateTimeField(blank=True, null=True)

Here is an example:

>>> from scinet.test.models import Foo
>>> from django import newforms as forms
>>> Form = forms.form_for_model(Foo)
>>> Form.base_fields['date'] = forms.fields.SplitDateTimeField(required=False, widget=forms.widgets.SplitDateTimeWidget)
>>> f = Form({'date_0': None, 'date_1': None})
>>> f.errors
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line 93, in _errors
    self.full_clean()
  File "/usr/lib/python2.4/site-packages/django/newforms/forms.py", line 187, in full_clean
    value = field.clean(value)
  File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line 473, in clean
    return self.compress(clean_data)
  File "/usr/lib/python2.4/site-packages/django/newforms/fields.py", line 493, in compress
    return datetime.datetime.combine(*data_list)
TypeError: combine() argument 1 must be datetime.date, not None

I've attached a patch that checks data_list for empty values, verifies that (in the case of non-required fields) either both are blank or both are non-blank, and raises an exception otherwise.

Attachments (1)

fields.diff (832 bytes ) - added by ctdecci@… 18 years ago.

Download all attachments as: .zip

Change History (6)

by ctdecci@…, 18 years ago

Attachment: fields.diff added

comment:1 by Gary Wilson <gary.wilson@…>, 18 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by Matt Boersma, 17 years ago

Owner: changed from nobody to Matt Boersma
Status: newassigned

comment:3 by Matt Boersma, 17 years ago

Owner: Matt Boersma removed
Status: assignednew

I wasn't able to reproduce the error given the steps described above:

In [7]: f.errors
Out[7]: {}

in reply to:  3 comment:4 by anonymous, 17 years ago

Resolution: duplicate
Status: newclosed

Replying to mboersma:

I wasn't able to reproduce the error given the steps described above:

In [7]: f.errors
Out[7]: {}

It appears that a duplicate ticket (#4630) was entered, and this was fixed (more elegantly, IMHO) in revision 5515:


r5515 | mtredinnick | 2007-06-22 20:32:59 -0700 (Fri, 22 Jun 2007) | 3 lines

Fixed #4630 -- Fixed some validation problems with SplitDateTimeField. Thanks
glin@… and SmileyChris.

comment:5 by ctdecci@…, 17 years ago

Just forgot to enter my email on last comment...

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