Opened 6 years ago

Closed 6 years ago

Last modified 4 years ago

#9520 closed (fixed)

date filter should not crash when dealing with non date objects

Reported by: hotani Owned by: ericholscher
Component: Template system Version: master
Severity: Keywords: date format filter template pycamp2009
Cc: nmiyasato 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 andrewbadr 6 years ago.
9520-with-tests.diff (1.7 KB) - added by ericholscher 6 years ago.

Download all attachments as: .zip

Change History (12)

comment:1 Changed 6 years ago by jacob

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

comment:2 Changed 6 years ago by nmiyasato

  • Cc nmiyasato added

comment:3 Changed 6 years ago by nmiyasato

  • Keywords pycamp2009 added

comment:4 Changed 6 years ago by andrewbadr

  • Owner changed from nobody to andrewbadr

Changed 6 years ago by andrewbadr

comment:5 Changed 6 years ago by andrewbadr

  • 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 6 years ago by ericholscher

comment:6 Changed 6 years ago by ericholscher

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 6 years ago by ericholscher

  • Owner changed from andrewbadr to ericholscher
  • Status changed from new to assigned

comment:8 Changed 6 years ago by jacob

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

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

comment:9 Changed 6 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 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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