Edgewall Software
Modify

Opened 2 years ago

Closed 2 years ago

#12857 closed defect (fixed)

Normalize default value of checkbox custom field

Reported by: Jun Omae Owned by: Ryan J Ollos
Priority: normal Milestone: 1.2.3
Component: ticket system Version: 1.0.15
Severity: normal Keywords:
Cc: Branch:
Release Notes:
API Changes:

Normalized value of custom field checkbox to 1/0.

Description (last modified by Ryan J Ollos)

Creating new ticket with checkbox field which has 42 for default value via web ui, the field would have 1 or 0.

[ticket-custom]
check = checkbox
check.value = 42
sqlite> SELECT t.id, t.summary, c.value FROM ticket AS t LEFT JOIN ticket_custom AS c ON c.ticket=t.id AND c.name='check';
1|Checked|1
2|Unchecked|0

However, the default value would be stored for the checkbox field via application code instead of 1|0.

$ ~/venv/trac/1.0.15/bin/python
Python 2.5.6 (r256:88840, Oct 21 2014, 22:49:55)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from trac.env import Environment
>>> env = Environment('/dev/shm/tracenv')
>>> from trac.ticket.model import Ticket
>>> ticket = Ticket(env)
>>> ticket['summary'] = 'via plugin'
>>> ticket.insert()
3
>>> for name in sorted(ticket.values): name, ticket[name]
...
('changetime', datetime.datetime(2017, 7, 6, 12, 42, 59, 479729, tzinfo=<FixedOffset "UTC" 0:00:00>))
(u'check', u'42')   # <== neither 1 nor 0
('owner', '')
('priority', u'major')
('summary', 'via plugin')
('time', datetime.datetime(2017, 7, 6, 12, 42, 59, 479729, tzinfo=<FixedOffset "UTC" 0:00:00>))
('type', u'defect')

I consider we should normalize default value for checkbox custom fields to have 1|0.

Attachments (0)

Change History (2)

comment:1 by Ryan J Ollos, 2 years ago

API Changes: modified (diff)
Description: modified (diff)
Owner: set to Ryan J Ollos
Status: newassigned

Any value other than 1 results in the checkbox defaulting to 0: tags/trac-1.2.2/trac/ticket/templates/ticket.html@:267#L244.

I considered using as_int, but maybe better to use as_bool:

  • trac/ticket/api.py

    diff --git a/trac/ticket/api.py b/trac/ticket/api.py
    index ea15d77a0..05f757a8d 100644
    a b from trac.config import (  
    2727from trac.core import *
    2828from trac.perm import IPermissionRequestor, PermissionCache, PermissionSystem
    2929from trac.resource import IResourceManager
    30 from trac.util import Ranges, as_int
     30from trac.util import Ranges, as_bool, as_int
    3131from trac.util.text import shorten_line
    3232from trac.util.translation import _, N_, deactivate, gettext, reactivate
    3333from trac.wiki import IWikiSyntaxProvider, WikiParser
    class TicketSystem(Component):  
    422422                    field['optional'] = True
    423423                    if '' in field['options']:
    424424                        field['options'].remove('')
     425            elif field['type'] == 'checkbox':
     426                field['value'] = '1' if as_bool(field['value']) else '0'
    425427            elif field['type'] == 'text':
    426428                field['format'] = config.get(name + '.format', 'plain')
    427429            elif field['type'] == 'textarea':

That would allow checkbox.value = true to be accepted. We don't need to document that as allowed, but it might be better fallback behavior.

>>> from trac.env import Environment
>>> from trac.ticket.model import Ticket
>>> env = Environment('/Users/rjollos/Documents/Workspace/trac-dev/tracenvs/proj-1
.2')
>>> env.config.get('ticket-custom', 'checkbox1.value')
u'42'
>>> ticket = Ticket(env)
>>> ticket['summary'] = 'via plugin'
>>> ticket.insert()
21
>>> for name in sorted(ticket.values): name, ticket[name]
...
('changetime', datetime.datetime(2017, 10, 2, 23, 28, 12, 648549, tzinfo=<FixedOff
set "UTC" 0:00:00>))
(u'checkbox1', 1)
('owner', u'< default >')
('priority', u'blocker')
('summary', 'via plugin')
('time', datetime.datetime(2017, 10, 2, 23, 28, 12, 648549, tzinfo=<FixedOffset "U
TC" 0:00:00>))

comment:2 by Ryan J Ollos, 2 years ago

Resolution: fixed
Status: assignedclosed

Fixed in r16348, r16349.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Ryan J Ollos.
The resolution will be deleted. Next status will be 'reopened'.
to as closed The owner will be changed from Ryan J Ollos to the specified user.

Add Comment


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