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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Bug tracker views."""
__metaclass__ = type
__all__ = [
'BugTrackerAddView',
'BugTrackerBreadcrumb',
'BugTrackerComponentGroupNavigation',
'BugTrackerEditView',
'BugTrackerEditComponentView',
'BugTrackerNavigation',
'BugTrackerNavigationMenu',
'BugTrackerSetBreadcrumb',
'BugTrackerSetContextMenu',
'BugTrackerSetNavigation',
'BugTrackerSetView',
'BugTrackerView',
'RemoteBug',
]
from itertools import chain
from zope.app.form.browser import TextAreaWidget
from zope.component import getUtility
from zope.formlib import form
from zope.interface import implements
from zope.schema import Choice
from zope.schema.vocabulary import SimpleVocabulary
from canonical.database.sqlbase import flush_database_updates
from canonical.launchpad import _
from canonical.launchpad.helpers import (
english_list,
shortlist,
)
from canonical.launchpad.interfaces.launchpad import ILaunchBag
from canonical.launchpad.webapp import (
canonical_url,
ContextMenu,
GetitemNavigation,
LaunchpadView,
Link,
Navigation,
redirection,
stepthrough,
structured,
)
from canonical.launchpad.webapp.authorization import check_permission
from canonical.launchpad.webapp.batching import (
ActiveBatchNavigator,
BatchNavigator,
InactiveBatchNavigator,
)
from canonical.launchpad.webapp.breadcrumb import Breadcrumb
from canonical.launchpad.webapp.menu import NavigationMenu
from canonical.lazr.utils import smartquote
from lp.app.browser.launchpadform import (
action,
custom_widget,
LaunchpadEditFormView,
LaunchpadFormView,
)
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.app.widgets.itemswidgets import LaunchpadRadioWidget
from lp.app.widgets.textwidgets import DelimitedListWidget
from lp.bugs.browser.widgets.bugtask import (
UbuntuSourcePackageNameWidget,
)
from lp.bugs.interfaces.bugtracker import (
BugTrackerType,
IBugTracker,
IBugTrackerComponent,
IBugTrackerComponentGroup,
IBugTrackerSet,
IRemoteBug,
)
from lp.services.propertycache import cachedproperty
# A set of bug tracker types for which there can only ever be one bug
# tracker.
SINGLE_INSTANCE_TRACKERS = (
BugTrackerType.DEBBUGS,
)
# A set of bug tracker types that we should not allow direct creation
# of.
NO_DIRECT_CREATION_TRACKERS = (
SINGLE_INSTANCE_TRACKERS + (
BugTrackerType.EMAILADDRESS,
)
)
class BugTrackerSetNavigation(GetitemNavigation):
usedfor = IBugTrackerSet
class BugTrackerSetContextMenu(ContextMenu):
usedfor = IBugTrackerSet
links = ['newbugtracker']
def newbugtracker(self):
text = 'Register another bug tracker'
return Link('+newbugtracker', text, icon='add')
class BugTrackerAddView(LaunchpadFormView):
page_title = u"Register an external bug tracker"
schema = IBugTracker
label = page_title
field_names = ['bugtrackertype', 'name', 'title', 'baseurl', 'summary',
'contactdetails']
def setUpWidgets(self, context=None):
# We only show those bug tracker types for which there can be
# multiple instances in the bugtrackertype Choice widget.
vocab_items = [
item for item in BugTrackerType.items.items
if item not in NO_DIRECT_CREATION_TRACKERS]
fields = []
for field_name in self.field_names:
if field_name == 'bugtrackertype':
fields.append(form.FormField(
Choice(__name__='bugtrackertype',
title=_('Bug Tracker Type'),
values=vocab_items,
default=BugTrackerType.BUGZILLA)))
else:
fields.append(self.form_fields[field_name])
self.form_fields = form.Fields(*fields)
super(BugTrackerAddView, self).setUpWidgets(context=context)
@action(_('Add'), name='add')
def add(self, action, data):
"""Create the IBugTracker."""
btset = getUtility(IBugTrackerSet)
bugtracker = btset.ensureBugTracker(
name=data['name'],
bugtrackertype=data['bugtrackertype'],
title=data['title'],
summary=data['summary'],
baseurl=data['baseurl'],
contactdetails=data['contactdetails'],
owner=getUtility(ILaunchBag).user)
self.next_url = canonical_url(bugtracker)
@property
def cancel_url(self):
return canonical_url(self.context)
class BugTrackerSetView(LaunchpadView):
"""View for actions on the bugtracker index pages."""
page_title = u"Bug trackers registered in Launchpad"
pillar_limit = 3
def initialize(self):
# eager load related pillars. In future we should do this for
# just the rendered trackers, and also use group by to get
# bug watch counts per tracker. However the batching makes
# the inefficiency tolerable for now. Robert Collins 20100919.
self._pillar_cache = self.context.getPillarsForBugtrackers(
list(self.context.trackers()))
@property
def inactive_tracker_count(self):
return self.inactive_trackers.currentBatch().listlength
@cachedproperty
def active_trackers(self):
results = self.context.trackers(active=True)
navigator = ActiveBatchNavigator(results, self.request)
navigator.setHeadings('tracker', 'trackers')
return navigator
@cachedproperty
def inactive_trackers(self):
results = self.context.trackers(active=False)
navigator = InactiveBatchNavigator(results, self.request)
navigator.setHeadings('tracker', 'trackers')
return navigator
def getPillarData(self, bugtracker):
"""Return dict of pillars and booleans indicating ellipsis.
In more detail, the dictionary holds a list of products/projects
and a boolean determining whether or not there we omitted
pillars by truncating to pillar_limit.
If no pillars are mapped to this bugtracker, returns {}.
"""
if bugtracker not in self._pillar_cache:
return {}
pillars = self._pillar_cache[bugtracker]
if len(pillars) > self.pillar_limit:
has_more_pillars = True
else:
has_more_pillars = False
return {
'pillars': pillars[:self.pillar_limit],
'has_more_pillars': has_more_pillars,
}
class BugTrackerView(LaunchpadView):
usedfor = IBugTracker
@property
def page_title(self):
return smartquote(
u'The "%s" bug tracker in Launchpad' % self.context.title)
def initialize(self):
self.batchnav = BatchNavigator(self.context.watches, self.request)
@property
def related_projects(self):
"""Return all project groups and projects.
This property was created for the Related projects portlet in
the bug tracker's page.
"""
return shortlist(chain(self.context.projects,
self.context.products), 100)
@property
def related_component_groups(self):
"""All component groups and components."""
return self.context.getAllRemoteComponentGroups()
BUG_TRACKER_ACTIVE_VOCABULARY = SimpleVocabulary.fromItems(
[('On', True), ('Off', False)])
class BugTrackerEditView(LaunchpadEditFormView):
schema = IBugTracker
custom_widget('summary', TextAreaWidget, width=30, height=5)
custom_widget('aliases', DelimitedListWidget, height=3)
custom_widget('active', LaunchpadRadioWidget, orientation='vertical')
@property
def page_title(self):
return smartquote(
u'Change details for the "%s" bug tracker' % self.context.title)
@cachedproperty
def field_names(self):
field_names = [
'name',
'title',
'bugtrackertype',
'summary',
'baseurl',
'aliases',
'contactdetails',
]
# Members of the admin team can set the bug tracker's active
# state.
if check_permission("launchpad.Admin", self.user):
field_names.append('active')
return field_names
def setUpFields(self):
"""Set up the fields for the bug tracker edit form.
If the `active` field is to be displayed, remove the default
Field and replace it with a Choice field for the sake of
usability.
See `LaunchpadFormView`.
"""
super(BugTrackerEditView, self).setUpFields()
# If we're displaying the 'active' field we need to swap it out
# and replace it with a field that uses our custom vocabulary.
if 'active' in self.field_names:
active_field = Choice(
__name__='active',
title=_('Updates for this bug tracker are'),
vocabulary=BUG_TRACKER_ACTIVE_VOCABULARY,
required=True, default=self.context.active)
self.form_fields = self.form_fields.omit('active')
self.form_fields += form.Fields(active_field)
def validate(self, data):
# Normalise aliases to an empty list if it's None.
if data.get('aliases') is None:
data['aliases'] = []
# If aliases has an error, unwrap the Dantean exception from
# Zope so that we can tell the user something useful.
if self.getFieldError('aliases'):
# XXX: GavinPanella 2008-04-02 bug=210901: The error
# messages may already be escaped (with `cgi.escape`), but
# the water is muddy, so we won't attempt to unescape them
# or otherwise munge them, in case we introduce a
# different problem. For now, escaping twice is okay as we
# won't see any artifacts of that during normal use.
aliases_errors = self.widgets['aliases']._error.errors.args[0]
self.setFieldError('aliases', structured(
'<br />'.join(['%s'] * len(aliases_errors)),
*aliases_errors))
@action('Change', name='change')
def change_action(self, action, data):
# If the baseurl is going to change, save the current baseurl
# as an alias. Users attempting to use this URL, which is
# presumably incorrect or out-of-date, will be captured.
current_baseurl = self.context.baseurl
requested_baseurl = data['baseurl']
if requested_baseurl != current_baseurl:
data['aliases'].append(current_baseurl)
self.updateContextFromData(data)
self.next_url = canonical_url(self.context)
@cachedproperty
def delete_not_possible_reasons(self):
"""A list of reasons why the context cannot be deleted.
An empty list means that there are no reasons, so the delete
can go ahead.
"""
reasons = []
celebrities = getUtility(ILaunchpadCelebrities)
# We go through all of the conditions why the bug tracker
# can't be deleted, and record reasons for all of them. We do
# this so that users can discover the logic behind the
# decision, and try something else, seek help, or give up as
# appropriate. Just showing the first problem would stop users
# from being able to help themselves.
# Check that no products or projects use this bugtracker.
pillars = (
getUtility(IBugTrackerSet).getPillarsForBugtrackers(
[self.context]).get(self.context, []))
if len(pillars) > 0:
reasons.append(
'This is the bug tracker for %s.' % english_list(
sorted(pillar.title for pillar in pillars)))
# Only admins and registry experts can delete bug watches en
# masse.
if self.context.watches.count() > 0:
admin_teams = [celebrities.admin, celebrities.registry_experts]
for team in admin_teams:
if self.user.inTeam(team):
break
else:
reasons.append(
'There are linked bug watches and only members of %s '
'can delete them en masse.' % english_list(
sorted(team.title for team in admin_teams)))
# Bugtrackers with imported messages cannot be deleted.
if self.context.imported_bug_messages.count() > 0:
reasons.append(
'Bug comments have been imported via this bug tracker.')
# If the bugtracker is a celebrity then we protect it from
# deletion.
celebrities_set = set(
getattr(celebrities, name)
for name in ILaunchpadCelebrities.names())
if self.context in celebrities_set:
reasons.append(
'This bug tracker is protected from deletion.')
return reasons
def delete_condition(self, action):
return len(self.delete_not_possible_reasons) == 0
@action('Delete', name='delete', condition=delete_condition)
def delete_action(self, action, data):
# First unlink bug watches from all bugtasks, flush updates,
# then delete the watches themselves.
for watch in self.context.watches:
for bugtask in watch.bugtasks:
if len(bugtask.bug.bugtasks) < 2:
raise AssertionError(
'There should be more than one bugtask for a bug '
'when one of them is linked to the original bug via '
'a bug watch.')
bugtask.bugwatch = None
flush_database_updates()
for watch in self.context.watches:
watch.destroySelf()
# Now delete the aliases and the bug tracker itself.
self.context.aliases = []
self.context.destroySelf()
# Hey, it worked! Tell the user.
self.request.response.addInfoNotification(
'%s has been deleted.' % (self.context.title,))
# Go back to the bug tracker listing.
self.next_url = canonical_url(getUtility(IBugTrackerSet))
@property
def cancel_url(self):
return canonical_url(self.context)
def reschedule_action_condition(self, action):
"""Return True if the user can see the reschedule action."""
user_can_reset_watches = check_permission(
"launchpad.Admin", self.context)
return (
user_can_reset_watches and
self.context.watches.count() > 0)
@action(
'Reschedule all watches', name='reschedule',
condition=reschedule_action_condition)
def rescheduleAction(self, action, data):
"""Reschedule all the watches for the bugtracker."""
self.context.resetWatches()
self.request.response.addInfoNotification(
"All bug watches on %s have been rescheduled." %
self.context.title)
self.next_url = canonical_url(self.context)
class BugTrackerNavigation(Navigation):
usedfor = IBugTracker
def traverse(self, remotebug):
bugs = self.context.getBugsWatching(remotebug)
if len(bugs) == 0:
# no bugs watching => not found
return None
elif len(bugs) == 1:
# one bug watching => redirect to that bug
return redirection(canonical_url(bugs[0]))
else:
# else list the watching bugs
return RemoteBug(self.context, remotebug, bugs)
@stepthrough("+components")
def component_groups(self, id):
# Navigate by id (component group name should work too)
return self.context.getRemoteComponentGroup(id)
class BugTrackerEditComponentView(LaunchpadEditFormView):
"""Provides editing form for setting source packages for components.
In this class we assume that bug tracker components are always
linked to source packages in the Ubuntu distribution.
"""
schema = IBugTrackerComponent
custom_widget('sourcepackagename', UbuntuSourcePackageNameWidget)
field_names = ['sourcepackagename']
label = 'Link a distribution source package to the Example component'
page_title = 'Link component'
@property
def initial_values(self):
"""See `LaunchpadFormView.`"""
field_values = dict(sourcepackagename='')
dsp = self.context.distro_source_package
if dsp is not None:
field_values['sourcepackagename'] = dsp.name
return field_values
def updateContextFromData(self, data, context=None):
"""Link component to specified distro source package.
Get the user-provided source package name from the form widget,
look it up in Ubuntu to retrieve the distro_source_package
object, and link it to this component.
"""
sourcepackagename = data['sourcepackagename']
distribution = self.widgets['sourcepackagename'].getDistribution()
dsp = distribution.getSourcePackage(sourcepackagename)
bug_tracker = self.context.component_group.bug_tracker
# Has this source package already been assigned to a component?
component = bug_tracker.getRemoteComponentForDistroSourcePackage(
distribution.name, sourcepackagename.name)
if component is not None:
self.request.response.addNotification(
"The %s source package is already linked to %s:%s in %s" % (
sourcepackagename.name,
component.component_group.name,
component.name, distribution.name))
return
# The submitted component can be linked to the distro source package.
component = context or self.context
component.distro_source_package = dsp
self.request.response.addNotification(
"%s:%s is now linked to the %s source package in %s" % (
component.component_group.name, component.name,
sourcepackagename.name, distribution.name))
@action('Save Changes', name='save')
def save_action(self, action, data):
"""Update the component with the form data."""
self.updateContextFromData(data)
self.next_url = canonical_url(
self.context.component_group.bug_tracker)
class BugTrackerComponentGroupNavigation(Navigation):
usedfor = IBugTrackerComponentGroup
def traverse(self, id):
return self.context.getComponent(id)
class BugTrackerSetBreadcrumb(Breadcrumb):
"""Builds a breadcrumb for the `IBugTrackerSet`."""
rootsite = None
@property
def text(self):
return u"Bug trackers"
class BugTrackerBreadcrumb(Breadcrumb):
"""Builds a breadcrumb for an `IBugTracker`."""
rootsite = None
@property
def text(self):
return self.context.title
class RemoteBug:
"""Represents a bug in a remote bug tracker."""
implements(IRemoteBug)
def __init__(self, bugtracker, remotebug, bugs):
self.bugtracker = bugtracker
self.remotebug = remotebug
self.bugs = bugs
@property
def title(self):
return 'Remote Bug #%s in %s' % (self.remotebug,
self.bugtracker.title)
class BugTrackerNavigationMenu(NavigationMenu):
usedfor = BugTrackerView
facet = 'bugs'
links = ['edit']
def edit(self):
text = 'Change details'
return Link('+edit', text, icon='edit')
|