-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapi.py
240 lines (200 loc) · 9.29 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/python
#
# Copyright (c) 2010, Takashi Ito
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of the authors nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import re
import pkg_resources
from trac.core import *
from trac.env import IEnvironmentSetupParticipant
from trac.db import DatabaseManager
from trac.resource import ResourceNotFound
from trac.ticket.model import Ticket
from trac.ticket.api import ITicketChangeListener, ITicketManipulator
from trac.ticket.notification import TicketNotifyEmail
from trac.util.translation import domain_functions
import db_default
NUMBERS_RE = re.compile(r'\d+', re.U)
# i18n support for plugins, available since Trac r7705
# use _, tag_ and N_ as usual, e.g. _("this is a message text")
_, tag_, N_, add_domain = domain_functions('tracsubtickets',
'_', 'tag_', 'N_', 'add_domain')
class SubTicketsSystem(Component):
implements(IEnvironmentSetupParticipant,
ITicketChangeListener,
ITicketManipulator)
def __init__(self):
self._version = None
self.ui = None
# bind the 'traccsubtickets' catalog to the locale directory
locale_dir = pkg_resources.resource_filename(__name__, 'locale')
add_domain(self.env.path, locale_dir)
# IEnvironmentSetupParticipant methods
def environment_created(self):
self.found_db_version = 0
self.upgrade_environment(self.env.get_db_cnx())
def environment_needs_upgrade(self, db):
cursor = db.cursor()
cursor.execute("SELECT value FROM system WHERE name=%s",
(db_default.name, ))
value = cursor.fetchone()
try:
self.found_db_version = int(value[0])
if self.found_db_version < db_default.version:
return True
except:
self.found_db_version = 0
return True
# check the custom field
if 'parents' not in self.config['ticket-custom']:
return True
return False
def upgrade_environment(self, db):
db_manager, _ = DatabaseManager(self.env)._get_connector()
# update the version
old_data = {} # {table.name: (cols, rows)}
cursor = db.cursor()
if not self.found_db_version:
cursor.execute("INSERT INTO system (name, value) VALUES (%s, %s)",
(db_default.name, db_default.version))
else:
cursor.execute("UPDATE system SET value=%s WHERE name=%s",
(db_default.version, db_default.name))
for table in db_default.tables:
cursor.execute("SELECT * FROM " + table.name)
cols = [x[0] for x in cursor.description]
rows = cursor.fetchall()
old_data[table.name] = (cols, rows)
cursor.execute("DROP TABLE " + table.name)
# insert the default table
for table in db_default.tables:
for sql in db_manager.to_sql(table):
cursor.execute(sql)
# add old data
if table.name in old_data:
cols, rows = old_data[table.name]
sql = 'INSERT INTO %s (%s) VALUES (%s)' % \
(table.name, ','.join(cols), ','.join(['%s'] * len(cols)))
for row in rows:
cursor.execute(sql, row)
# add the custom field
cfield = self.config['ticket-custom']
if 'parents' not in cfield:
cfield.set('parents', 'text')
cfield.set('parents.label', 'Parent Tickets')
self.config.save()
# ITicketChangeListener methods
def ticket_created(self, ticket):
self.ticket_changed(ticket, '', ticket['reporter'], {'parents': ''})
def ticket_changed(self, ticket, comment, author, old_values):
if 'parents' not in old_values:
return
old_parents = old_values.get('parents', '') or ''
old_parents = set(NUMBERS_RE.findall(old_parents))
new_parents = set(NUMBERS_RE.findall(ticket['parents'] or ''))
if new_parents == old_parents:
return
db = self.env.get_db_cnx()
cursor = db.cursor()
# remove old parents
for parent in old_parents - new_parents:
cursor.execute("DELETE FROM subtickets WHERE parent=%s AND child=%s",
(parent, ticket.id))
# add a comment to old parent
xticket = Ticket(self.env, parent)
xticket.save_changes(author, _('Remove a subticket #%s.') % ticket.id)
tn = TicketNotifyEmail(self.env)
tn.notify(xticket, newticket=False, modtime=xticket['changetime'])
# add new parents
for parent in new_parents - old_parents:
cursor.execute("INSERT INTO subtickets VALUES(%s, %s)",
(parent, ticket.id))
# add a comment to new parent
xticket = Ticket(self.env, parent)
xticket.save_changes(author, _('Add a subticket #%s.') % ticket.id)
tn = TicketNotifyEmail(self.env)
tn.notify(xticket, newticket=False, modtime=xticket['changetime'])
db.commit()
def ticket_deleted(self, ticket):
db = self.env.get_db_cnx()
cursor = db.cursor()
# TODO: check if there's any child ticket
cursor.execute("DELETE FROM subtickets WHERE child=%s", (ticket.id, ))
db.commit()
# ITicketManipulator methods
def prepare_ticket(self, req, ticket, fields, actions):
pass
def validate_ticket(self, req, ticket):
db = self.env.get_db_cnx()
cursor = db.cursor()
try:
invalid_ids = set()
_ids = set(NUMBERS_RE.findall(ticket['parents'] or ''))
myid = str(ticket.id)
for id in _ids:
if id == myid:
invalid_ids.add(id)
yield 'parents', _('A ticket cannot be a parent to itself')
else:
# check if the id exists
cursor.execute("SELECT id FROM ticket WHERE id=%s", (id, ))
row = cursor.fetchone()
if row is None:
invalid_ids.add(id)
yield 'parents', _('Ticket #%s does not exist') % id
# circularity check function
def _check_parents(id, all_parents):
all_parents = all_parents + [id]
errors = []
cursor.execute("SELECT parent FROM subtickets WHERE child=%s", (id, ))
for x in [int(x[0]) for x in cursor]:
if x in all_parents:
invalid_ids.add(x)
error = ' > '.join(['#%s' % n for n in all_parents + [x]])
errors.append(('parents', _('Circularity error: %s') % error))
else:
errors += _check_parents(x, all_parents)
return errors
for x in [i for i in _ids if i not in invalid_ids]:
# check parent ticket state
try:
parent = Ticket(self.env, x)
if parent and parent['status'] == 'closed' and ticket['status'] != 'closed':
invalid_ids.add(x)
yield 'parents', _('Parent ticket #%s is closed.') % x
else:
# check circularity
all_parents = ticket.id and [ticket.id] or []
for error in _check_parents(int(x), all_parents):
yield error
except ResourceNotFound, e:
invalid_ids.add(x)
valid_ids = _ids.difference(invalid_ids)
ticket['parents'] = valid_ids and ', '.join(sorted(valid_ids, key=lambda x: int(x))) or ''
except Exception, e:
import traceback
self.log.error(traceback.format_exc())
yield 'parents', _('Not a valid list of ticket IDs.')