-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathweb_ui.py
189 lines (159 loc) · 7.35 KB
/
web_ui.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
#!/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.
from trac.core import *
from trac.web.api import IRequestFilter, ITemplateStreamFilter
from trac.web.chrome import ITemplateProvider, add_stylesheet
from trac.ticket.api import ITicketManipulator
from trac.ticket.model import Ticket
from trac.resource import ResourceNotFound
from genshi.builder import tag
from genshi.filters import Transformer
from api import NUMBERS_RE, _
class SubTicketsModule(Component):
implements(ITemplateProvider,
IRequestFilter,
ITicketManipulator,
ITemplateStreamFilter)
# ITemplateProvider methods
def get_htdocs_dirs(self):
from pkg_resources import resource_filename
return [('subtickets', resource_filename(__name__, 'htdocs'))]
def get_templates_dirs(self):
return []
# IRequestFilter methods
def pre_process_request(self, req, handler):
return handler
def post_process_request(self, req, template, data, content_type):
path = req.path_info
if path.startswith('/ticket/') or path.startswith('/newticket'):
# get parent ticket's data
if data and 'ticket' in data:
ticket = data['ticket']
parents = ticket['parents'] or ''
ids = set(NUMBERS_RE.findall(parents))
if len(parents) > 0:
self._append_parent_links(req, data, ids)
children = self.get_children(ticket.id)
if children:
data['subtickets'] = children
return template, data, content_type
def _append_parent_links(self, req, data, ids):
links = []
for id in sorted(ids, key=lambda x: int(x)):
try:
ticket = Ticket(self.env, id)
elem = tag.a('#%s' % id,
href=req.href.ticket(id),
class_='%s ticket' % ticket['status'],
title=ticket['summary'])
if len(links) > 0:
links.append(', ')
links.append(elem)
except ResourceNotFound, e:
pass
for field in data.get('fields', ''):
if field.get('name') == 'parents':
field['rendered'] = tag.span(*links)
# ITicketManipulator methods
def prepare_ticket(self, req, ticket, fields, actions):
pass
def get_children(self, parent_id, db=None):
children = {}
if not db:
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s",
(parent_id, ))
for parent, child in cursor:
children[child] = None
for id in children:
children[id] = self.get_children(id, db)
return children
def validate_ticket(self, req, ticket):
action = req.args.get('action')
if action == 'resolve':
db = self.env.get_db_cnx()
cursor = db.cursor()
cursor.execute("SELECT parent, child FROM subtickets WHERE parent=%s",
(ticket.id, ))
for parent, child in cursor:
if Ticket(self.env, child)['status'] != 'closed':
yield None, _('Child ticket #%s has not been closed yet') % child
elif action == 'reopen':
ids = set(NUMBERS_RE.findall(ticket['parents'] or ''))
for id in ids:
if Ticket(self.env, id)['status'] == 'closed':
yield None, _('Parent ticket #%s is closed') % id
# ITemplateStreamFilter method
def filter_stream(self, req, method, filename, stream, data):
if req.path_info.startswith('/ticket/'):
div = None
if 'ticket' in data:
# get parents data
ticket = data['ticket']
# title
div = tag.div(class_='description')
if ticket['status'] != 'closed':
link = tag.a(_('add'),
href=req.href.newticket(parents=ticket.id),
title=_('Create new child ticket'))
link = tag.span('(', link, ')', class_='addsubticket')
else:
link = None
div.append(tag.h3(_('Subtickets '), link))
if 'subtickets' in data:
# table
tbody = tag.tbody()
div.append(tag.table(tbody, class_='subtickets'))
# tickets
def _func(children, depth=0):
for id in sorted(children, key=lambda x: int(x)):
ticket = Ticket(self.env, id)
# 1st column
attrs = {'href': req.href.ticket(id)}
if ticket['status'] == 'closed':
attrs['class_'] = 'closed'
link = tag.a('#%s' % id, **attrs)
summary = tag.td(link, ': %s' % ticket['summary'],
style='padding-left: %dpx;' % (depth * 15))
# 2nd column
type = tag.td(ticket['type'])
# 3rd column
status = tag.td(ticket['status'])
# 4th column
href = req.href.query(status='!closed',
owner=ticket['owner'])
owner = tag.td(tag.a(ticket['owner'], href=href))
tbody.append(tag.tr(summary, type, status, owner))
_func(children[id], depth + 1)
_func(data['subtickets'])
if div:
add_stylesheet(req, 'subtickets/css/subtickets.css')
stream |= Transformer('.//div[@id="ticket"]').append(div)
return stream