diff --git a/trac/ticket/model.py b/trac/ticket/model.py
|
a
|
b
|
|
| 52 | 52 | tkt_id = int(tkt_id) |
| 53 | 53 | self.resource = Resource('ticket', tkt_id, version) |
| 54 | 54 | self.fields = TicketSystem(self.env).get_ticket_fields() |
| | 55 | self.std_fields = [f['name'] for f in self.fields |
| | 56 | if not f.get('custom')] |
| | 57 | self.custom_fields = [f['name'] for f in self.fields |
| | 58 | if f.get('custom')] |
| 55 | 59 | self.time_fields = [f['name'] for f in self.fields |
| 56 | 60 | if f['type'] == 'time'] |
| 57 | 61 | self.values = {} |
| … |
… |
|
| 95 | 99 | db = self._get_db(db) |
| 96 | 100 | |
| 97 | 101 | # Fetch the standard ticket fields |
| 98 | | std_fields = [f['name'] for f in self.fields |
| 99 | | if not f.get('custom')] |
| 100 | 102 | cursor = db.cursor() |
| 101 | 103 | cursor.execute("SELECT %s FROM ticket WHERE id=%%s" |
| 102 | | % ','.join(std_fields), (tkt_id,)) |
| | 104 | % ','.join(self.std_fields), (tkt_id,)) |
| 103 | 105 | row = cursor.fetchone() |
| 104 | 106 | if not row: |
| 105 | 107 | raise ResourceNotFound(_('Ticket %(id)s does not exist.', |
| 106 | 108 | id=tkt_id), _('Invalid ticket number')) |
| 107 | 109 | |
| 108 | 110 | self.id = tkt_id |
| 109 | | for i, field in enumerate(std_fields): |
| | 111 | for i, field in enumerate(self.std_fields): |
| 110 | 112 | value = row[i] |
| 111 | 113 | if field in self.time_fields: |
| 112 | 114 | self.values[field] = from_utimestamp(value) |
| … |
… |
|
| 116 | 118 | self.values[field] = value |
| 117 | 119 | |
| 118 | 120 | # Fetch custom fields if available |
| 119 | | custom_fields = [f['name'] for f in self.fields if f.get('custom')] |
| 120 | 121 | cursor.execute("SELECT name,value FROM ticket_custom WHERE ticket=%s", |
| 121 | 122 | (tkt_id,)) |
| 122 | 123 | for name, value in cursor: |
| 123 | | if name in custom_fields: |
| | 124 | if name in self.custom_fields: |
| 124 | 125 | if value is None: |
| 125 | 126 | self.values[name] = empty |
| 126 | 127 | else: |
| … |
… |
|
| 200 | 201 | values[field] = to_utimestamp(values[field]) |
| 201 | 202 | |
| 202 | 203 | # Insert ticket record |
| 203 | | std_fields = [] |
| 204 | | custom_fields = [] |
| 205 | | for f in self.fields: |
| 206 | | fname = f['name'] |
| 207 | | if fname in self.values: |
| 208 | | if f.get('custom'): |
| 209 | | custom_fields.append(fname) |
| 210 | | else: |
| 211 | | std_fields.append(fname) |
| 212 | | |
| 213 | 204 | tkt_id = [None] |
| 214 | 205 | @self.env.with_transaction(db) |
| 215 | 206 | def do_insert(db): |
| 216 | 207 | cursor = db.cursor() |
| 217 | 208 | cursor.execute("INSERT INTO ticket (%s) VALUES (%s)" |
| 218 | | % (','.join(std_fields), |
| 219 | | ','.join(['%s'] * len(std_fields))), |
| 220 | | [values[name] for name in std_fields]) |
| | 209 | % (','.join(self.std_fields), |
| | 210 | ','.join(['%s'] * len(self.std_fields))), |
| | 211 | [values[name] for name in self.std_fields]) |
| 221 | 212 | tkt_id[0] = db.get_last_id(cursor, 'ticket') |
| 222 | 213 | |
| 223 | 214 | # Insert custom fields |
| 224 | | if custom_fields: |
| | 215 | if self.custom_fields: |
| 225 | 216 | cursor.executemany(""" |
| 226 | 217 | INSERT INTO ticket_custom (ticket,name,value) VALUES (%s,%s,%s) |
| 227 | | """, [(tkt_id[0], name, self[name]) for name in custom_fields]) |
| | 218 | """, [(tkt_id[0], name, self[name]) for name in self.custom_fields]) |
| 228 | 219 | |
| 229 | 220 | self.id = tkt_id[0] |
| 230 | 221 | self.resource = self.resource(id=tkt_id[0]) |
| … |
… |
|
| 306 | 297 | comment_num = str(num + 1) |
| 307 | 298 | |
| 308 | 299 | # store fields |
| 309 | | custom_fields = [f['name'] for f in self.fields if f.get('custom')] |
| 310 | | |
| 311 | 300 | for name in self._old.keys(): |
| 312 | | if name in custom_fields: |
| | 301 | if name in self.custom_fields: |
| 313 | 302 | cursor.execute(""" |
| 314 | 303 | SELECT * FROM ticket_custom |
| 315 | 304 | WHERE ticket=%s and name=%s |
| … |
… |
|
| 441 | 430 | return |
| 442 | 431 | ts = row[0] |
| 443 | 432 | |
| 444 | | custom_fields = set(f['name'] for f in self.fields |
| 445 | | if f.get('custom')) |
| 446 | | |
| 447 | 433 | # Find modified fields and their previous value |
| 448 | 434 | cursor.execute(""" |
| 449 | 435 | SELECT field, oldvalue, newvalue FROM ticket_change |
| … |
… |
|
| 469 | 455 | break |
| 470 | 456 | else: |
| 471 | 457 | # No next change, edit ticket field |
| 472 | | if field in custom_fields: |
| | 458 | if field in self.custom_fields: |
| 473 | 459 | cursor.execute(""" |
| 474 | 460 | UPDATE ticket_custom SET value=%s |
| 475 | 461 | WHERE ticket=%s AND name=%s |