trac-1942 / datefmt-err-to-warn.patch

# HG changeset patch
# Parent e75d9104b1af854ddeeb08070adbcd39fdc4ec53
Handle invalid time input more gracefully.

diff --git a/trac/ticket/web_ui.py b/trac/ticket/web_ui.py
--- a/trac/ticket/web_ui.py
+++ b/trac/ticket/web_ui.py
@@ -706,8 +706,16 @@
             fields.pop('checkbox_' + each, None)    # See Ticket.populate()
         for field, value in fields.iteritems():
             if field in ticket.time_fields:
-                fields[field] = user_time(req, parse_date, value) \
-                                if value else None
+                try:
+                    fields[field] = user_time(req, parse_date, value) \
+                                    if value else None
+                except TracError, e:
+                    # Handle bad user input for custom time fields gracefully.
+                    if field in ticket.custom_fields:
+                        # Leave it to _validate_ticket() to complain.
+                        fields[field] = value
+                    else:
+                        raise TracError(e)
         ticket.populate(fields)
         # special case for updating the Cc: field
         if 'cc_update' in req.args:
@@ -1202,7 +1210,19 @@
             # Shouldn't happen in "normal" circumstances, hence not a warning
             raise InvalidTicket(_("Invalid comment threading identifier"))
 
-        # FIXME: Validate time field content
+        # Validate time field content
+        for field in ticket.time_fields:
+            value = ticket[field]
+            if not (field in ticket.std_fields or \
+                    isinstance(value, datetime)):
+                try:
+                    ticket.values[field] = user_time(req, parse_date, value) \
+                                          if value else None
+                except TracError, e:
+                    # Degrade TracError to warning.
+                    add_warning(req, e)
+                    ticket.values[field] = value
+                    valid = False
 
         # Custom validation rules
         for manipulator in self.ticket_manipulators:
@@ -1681,6 +1701,9 @@
         return rendered
 
     def _render_time_field(self, req, value, format, relative=False):
+        if value and not isinstance(value, datetime):
+            # Return invalid timestamps unchanged.
+            return u'' if 'None' else unicode(value)
         format = format or 'datetime'
         if format == 'age' and relative:
             return pretty_timedelta(value) if value else ''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.