Edgewall Software
Modify

Opened 15 months ago

Closed 7 weeks ago

Last modified 6 weeks ago

#13040 closed defect (fixed)

TypeError: expecting datetime, int, long, float, or None; got <type 'unicode'> - Clone button bug

Reported by: pcelba Owned by: Jun Omae
Priority: normal Milestone: 1.3.4
Component: ticket system Version: 1.3.2
Severity: normal Keywords: ticketclone time field
Cc: Branch:
Release Notes:

Fixed TypeError cloning a ticket that has a time custom field. Fixed Clone button not present when Reply button not present.

API Changes:

Description (last modified by Jun Omae)

How to Reproduce

While doing a POST operation on /newticket, Trac issued an internal error.

When creating a new ticket by the Clone button click and the original ticket contains Time field defined as

test_eight = time
test_eight.format = date
test_eight.label = An absolute date
test_eight.order = 5
test_eight.value = 

the Type error appears and also the warning on top saying:

Warning: The ticket field An absolute date is invalid: "1528603200000000" is an invalid date, or the date format is not known. Try "MMM d, y" or "YYYY-MM-DD" instead.

Request parameters:

{u'__FORM_TOKEN': u'af4e62a78a566df7aed49e9e',
 u'clone': u'+ Clone',
 u'field_blockedby': u'',
 u'field_blocking': u'',
 u'field_cc': u'',
 u'field_changetime': u'1528658560945708',
 u'field_client': u'',
 u'field_component': u'Infrastructure',
 u'field_description': u'Cloned from #1:\r\n> Some description here. Images will come later...\r\n> \r\n',
 u'field_estimatedhours': u'0',
 u'field_hours': u'0',
 u'field_keywords': u'',
 u'field_milestone': u'',
 u'field_owner': u'pcelba',
 u'field_parents': u'',
 u'field_priority': u'critical',
 u'field_summary': u'The first ticket with a large number of comments (cloned)',
 u'field_test_eight': u'1528603200000000',
 u'field_time': u'1528374973950465',
 u'field_totalhours': u'1640.5',
 u'field_type': u'task (no coding)',
 u'field_version': u'',
 u'preview': u''}

User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36

System Information

Trac 1.3.2
Babel 2.6.0
Genshi 0.7 (without speedups)
Jinja2 2.10
pysqlite 2.6.0
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)]
pytz 2018.4
setuptools 39.2.0
SQLite 3.14.2
jQuery 1.12.4
jQuery UI 1.12.1
jQuery Timepicker 1.6.3

Enabled Plugins

timingandestimationplugin 1.5.9
traccustomfieldadmin 0.2.13
tracmastertickets 4.0.2
tracsubticketsplugin 0.5.3

Interface Customization

shared-htdocs
shared-templates
site-htdocs malotraccloud3.jpg
site-templates

Python Traceback

Traceback (most recent call last):
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\web\main.py", line 640, in _dispatch_request
    dispatcher.dispatch(req)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\web\main.py", line 263, in dispatch
    method=method)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\web\chrome.py", line 1418, in render_template
    fragment, iterable, method)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\web\chrome.py", line 1477, in _render_jinja_template
    page = self.render_template_string(template, data, text)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\web\chrome.py", line 1647, in render_template_string
    string = template.render(data)
  File "e:\pythonvirtual\trac13\lib\site-packages\jinja2\environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "e:\pythonvirtual\trac13\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\ticket\templates\ticket.html", line 132, in top-level template code
    <a href="#comment:${cnum}" class="${cls}">${prefix}${cnum}</a>
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\templates\layout.html", line 12, in top-level template code
    # import "macros.html" as jmacros with context
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\templates\theme.html", line 22, in top-level template code
    # block body
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\templates\theme.html", line 128, in block "body"
    # block content
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\ticket\templates\ticket.html", line 621, in block "content"
    #     include 'ticket_box.html'
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\ticket\templates\ticket_box.html", line 137, in top-level template code
    ${pretty_dateinfo(value, field.format,
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\timeline\web_ui.py", line 280, in pretty_dateinfo
    absolute = user_time(req, format_date, date)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\util\datefmt.py", line 915, in user_time
    return func(*args, **kwargs)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\util\datefmt.py", line 324, in format_date
    return _format_datetime(t, format, tzinfo, locale, 'date')
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\util\datefmt.py", line 274, in _format_datetime
    t = to_datetime(t, tzinfo or localtz)
  File "e:\pythonvirtual\trac13\lib\site-packages\trac\util\datefmt.py", line 179, in to_datetime
    type(t))
TypeError: expecting datetime, int, long, float, or None; got <type 'unicode'>

Attachments (0)

Change History (14)

comment:1 by Jun Omae, 15 months ago

Description: modified (diff)
Priority: highnormal

Maybe a related issue #12714.

comment:2 by Jun Omae, 15 months ago

Keywords: ticketclone added

I'll post unit tests for the regression.

  • trac/ticket/web_ui.py

    diff --git a/trac/ticket/web_ui.py b/trac/ticket/web_ui.py
    index 351855e3a..cf1f0a35b 100644
    a b class TicketModule(Component):  
    767767                    break
    768768
    769769        # Data need for Javascript-specific logic
    770         old_values = {name: ticket[name]
     770        old_values = {name: unicode(ticket[name] or '')
    771771                      for name in [field['name'] for field in ticket.fields]}
    772772        old_values['id'] = ticket.id
    773773        add_script_data(req, {'comments_prefs': self._get_prefs(req),

comment:3 by Ryan J Ollos, 15 months ago

Yeah, I think that makes sense. TracJSONEncoder currently converts the datetime value to a timestamp, but it looks like the old_values should be strings because they are used as form values in the clone POST request: tags/trac-1.3.2/tracopt/ticket/htdocs/ticketclone.js@:26#L20.

Do the old_values have another use outside of the clone action? If not, I might expect them to be added by clone.py. I suppose even if they don't have another use in Trac it is good for the values to always be available for use by plugins.

Last edited 15 months ago by Ryan J Ollos (previous) (diff)

in reply to:  3 comment:4 by Jun Omae, 15 months ago

Replying to Ryan J Ollos:

Yeah, I think that makes sense. TracJSONEncoder currently converts the datetime value to a timestamp, but it looks like the old_values should be strings because they are used as form values in the clone POST request: tags/trac-1.3.2/tracopt/ticket/htdocs/ticketclone.js@:26#L20.

I consider we should convert to an iso8601 string rather than a timestamp integer. It is not easy to use the timestamp integer from javascript.

  • trac/util/presentation.py

    diff --git a/trac/util/presentation.py b/trac/util/presentation.py
    index 357d519ff..46165e94f 100644
    a b from jinja2.utils import soft_unicode  
    2828from jinja2._compat import iteritems
    2929
    3030from trac.core import TracError
    31 from .datefmt import to_utimestamp
     31from .datefmt import format_datetime, to_utimestamp, utc
    3232from .html import Fragment, classes, html_attribute, styles, tag
    3333from .text import javascript_quote
    3434
    class TracJSONEncoder(JSONEncoder):  
    469469        if isinstance(o, Undefined):
    470470            return ''
    471471        elif isinstance(o, datetime):
    472             return to_utimestamp(o)
     472            return format_datetime(o, 'iso8601', o.tzinfo or utc)
    473473        elif isinstance(o, Fragment):
    474474            return '"%s"' % javascript_quote(unicode(o))
    475475        return JSONEncoder.default(self, o)

It seems missing unit tests for to_json with datetime instance. We should add the unit tests.

Do the old_values have another use outside of the clone action? If not, I might expect them to be added by clone.py. I suppose even if they don't have another use in Trac it is good for the values to always be available for use by plugins.

The old_values is introduced in [15435,15436] (#12639) to integrate Jinja2. As far as I know, we have no other uses.

comment:5 by Ryan J Ollos, 13 months ago

Milestone: next-dev-1.3.x1.3.4

comment:6 by Ryan J Ollos, 11 months ago

Milestone: 1.3.41.3.5

comment:7 by Ryan J Ollos, 7 weeks ago

Milestone: 1.3.51.3.4
Owner: set to Ryan J Ollos
Status: newassigned

Added a test for comment:4 patch in log:rjollos.git:t13040_ticket_clone_time_field.

Also fixed issue in which Clone button was not added if ticket description was empty, because Reply button not present.

comment:8 by Ryan J Ollos, 7 weeks ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Committed in r16944, r16945.

comment:9 by Ryan J Ollos, 7 weeks ago

Owner: changed from Ryan J Ollos to Jun Omae

comment:10 by Jun Omae, 7 weeks ago

Ah, I noticed javascript errors are lead by [16944].

22:48:37.282 Error: Syntax error, unrecognized expression: unsupported pseudo: 26 jquery.js:2:12733
    jQuery 9
    addCloneFromComments http://192.168.11.27:3000/1.3-sqlite/chrome/ticketopt/ticketclone.js:38
    <anonymous> http://192.168.11.27:3000/1.3-sqlite/chrome/ticketopt/ticketclone.js:69
    jQuery 4

ticketclone.js expects c.date is an utimestamp integer. The patch in comment:2 only should be applied…

 38       btns = $("#trac-change-" + c.cnum + "-" + c.date + " .trac-ticket-buttons");
Last edited 7 weeks ago by Ryan J Ollos (previous) (diff)

comment:11 by Jun Omae, 7 weeks ago

Proposed changes in [d1843a1b1/jomae.git].

Last edited 7 weeks ago by Jun Omae (previous) (diff)

comment:12 by Ryan J Ollos, 7 weeks ago

Thanks for spotting. I forgot to test the clone-from-comment feature.

comment:13 by Ryan J Ollos, 6 weeks ago

Sorry I forgot to commit these changes. Will commit and create 1.3.5.

Last edited 6 weeks ago by Ryan J Ollos (previous) (diff)

comment:14 by Ryan J Ollos, 6 weeks ago

Committed in r16966. I'll create the 1.3.5 release later in the day.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.