Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#4317 closed (duplicate)

Fix compress() in SplitDateTimeField

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

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@… 8 years ago.

Download all attachments as: .zip

Change History (6)

Changed 8 years ago by ctdecci@…

comment:1 Changed 8 years ago by Gary Wilson <gary.wilson@…>

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 8 years ago by mboersma

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

comment:3 follow-up: Changed 8 years ago by mboersma

  • Owner mboersma deleted
  • Status changed from assigned to new

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

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

comment:4 in reply to: ↑ 3 Changed 8 years ago by anonymous

  • Resolution set to duplicate
  • Status changed from new to closed

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 Changed 8 years ago by ctdecci@…

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

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