Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#9520 closed (fixed)

date filter should not crash when dealing with non date objects

Reported by: hotani Owned by: Eric Holscher
Component: Template system Version: master
Severity: Keywords: date format filter template pycamp2009
Cc: Nicolás Miyasato Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm using date filter on a form field like so: {{ form.field.data|date:"m-d-Y" }} When loading the form for editing, all is well. However, if validation fails for the form, date fields are sent back as unicode and not date objects. The date filter throws an error.

As a workaround, I wrote a wrapper for the date filter which puts it in a try/except block, returning the value if 'date' fails. This works but feels hacky.

Attachments (2)

datefilter_t9520_r10177.diff (784 bytes) - added by Andrew Badr 8 years ago.
9520-with-tests.diff (1.7 KB) - added by Eric Holscher 8 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:2 Changed 8 years ago by Nicolás Miyasato

Cc: Nicolás Miyasato added

comment:3 Changed 8 years ago by Nicolás Miyasato

Keywords: pycamp2009 added

comment:4 Changed 8 years ago by Andrew Badr

Owner: changed from nobody to Andrew Badr

Changed 8 years ago by Andrew Badr

comment:5 Changed 8 years ago by Andrew Badr

Has patch: set

The attachment implements the behavior described in the ticket, returning value if there is an exception. It patches the time filter in addition to date since the two cases are directly analogous. Only catches AttributeError since that's what you get when you pass a string to format or time_format. I'd prefer to raise an exception if value can't be formatted and is not a basestring.

Changed 8 years ago by Eric Holscher

Attachment: 9520-with-tests.diff added

comment:6 Changed 8 years ago by Eric Holscher

Added simple tests of the date filter. Also made it so that the filter returns the empty string on error, instead of the original value, which is a Django convention.

comment:7 Changed 8 years ago by Eric Holscher

Owner: changed from Andrew Badr to Eric Holscher
Status: newassigned

comment:8 Changed 8 years ago by Jacob

Resolution: fixed
Status: assignedclosed

(In [10365]) Fixed #9520: make the date filter fail silently for non-date values. Thanks, Andrew Badr and Eric Holscher.

comment:9 Changed 8 years ago by Jacob

(In [10366]) [1.0.X] Fixed #9520: make the date filter fail silently for non-date values. Thanks, Andrew Badr and Eric Holscher. Backport of r10365 from trunk.

comment:10 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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