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
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Bug domain vocabularies"""
__metaclass__ = type
__all__ = [
'UsesBugsDistributionVocabulary',
'BugNominatableDistroSeriesVocabulary',
'BugNominatableProductSeriesVocabulary',
'BugNominatableSeriesVocabulary',
'BugTrackerVocabulary',
'BugVocabulary',
'BugWatchVocabulary',
'DistributionUsingMaloneVocabulary',
'project_products_using_malone_vocabulary_factory',
'UsesBugsDistributionVocabulary',
'WebBugTrackerVocabulary',
]
import cgi
from operator import attrgetter
from sqlobject import (
CONTAINSSTRING,
OR,
)
from storm.expr import (
And,
Or,
)
from zope.component import getUtility
from zope.interface import implements
from zope.schema.interfaces import (
IVocabulary,
IVocabularyTokenized,
)
from zope.schema.vocabulary import (
SimpleTerm,
SimpleVocabulary,
)
from lp.services.helpers import (
ensure_unicode,
shortlist,
)
from lp.services.database.lpstorm import IStore
from canonical.launchpad.webapp.interfaces import ILaunchBag
from canonical.launchpad.webapp.vocabulary import (
CountableIterator,
IHugeVocabulary,
NamedSQLObjectVocabulary,
SQLObjectVocabularyBase,
)
from lp.app.browser.stringformatter import FormattersAPI
from lp.app.enums import ServiceUsage
from lp.bugs.interfaces.bugtask import IBugTask
from lp.bugs.interfaces.bugtracker import BugTrackerType
from lp.bugs.model.bug import Bug
from lp.bugs.model.bugtracker import BugTracker
from lp.bugs.model.bugwatch import BugWatch
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.projectgroup import IProjectGroup
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.model.distribution import Distribution
from lp.registry.model.distroseries import DistroSeries
from lp.registry.model.productseries import ProductSeries
from lp.registry.vocabularies import DistributionVocabulary
class UsesBugsDistributionVocabulary(DistributionVocabulary):
"""Distributions that use Launchpad to track bugs.
If the context is a distribution, it is always included in the
vocabulary. Historic data is not invalidated if a distro stops
using Launchpad to track bugs. This vocabulary offers the correct
choices of distributions at this moment.
"""
def __init__(self, context=None):
super(UsesBugsDistributionVocabulary, self).__init__(context=context)
self.distribution = IDistribution(self.context, None)
@property
def _filter(self):
if self.distribution is None:
distro_id = 0
else:
distro_id = self.distribution.id
return OR(
self._table.q.official_malone == True,
self._table.id == distro_id)
class BugVocabulary(SQLObjectVocabularyBase):
_table = Bug
_orderBy = 'id'
class BugTrackerVocabulary(SQLObjectVocabularyBase):
"""All web and email based external bug trackers."""
displayname = 'Select a bug tracker'
step_title = 'Search'
implements(IHugeVocabulary)
_table = BugTracker
_filter = True
_orderBy = 'title'
_order_by = [BugTracker.title]
def toTerm(self, obj):
"""See `IVocabulary`."""
return SimpleTerm(obj, obj.name, obj.title)
def getTermByToken(self, token):
"""See `IVocabularyTokenized`."""
result = IStore(self._table).find(
self._table,
self._filter,
BugTracker.name == token).one()
if result is None:
raise LookupError(token)
return self.toTerm(result)
def search(self, query, vocab_filter=None):
"""Search for web bug trackers."""
query = ensure_unicode(query).lower()
results = IStore(self._table).find(
self._table, And(
self._filter,
BugTracker.active == True,
Or(
CONTAINSSTRING(BugTracker.name, query),
CONTAINSSTRING(BugTracker.title, query),
CONTAINSSTRING(BugTracker.summary, query),
CONTAINSSTRING(BugTracker.baseurl, query))))
results = results.order_by(self._order_by)
return results
def searchForTerms(self, query=None, vocab_filter=None):
"""See `IHugeVocabulary`."""
results = self.search(query, vocab_filter)
return CountableIterator(results.count(), results, self.toTerm)
class WebBugTrackerVocabulary(BugTrackerVocabulary):
"""All web-based bug tracker types."""
_filter = BugTracker.bugtrackertype != BugTrackerType.EMAILADDRESS
def project_products_using_malone_vocabulary_factory(context):
"""Return a vocabulary containing a project's products using Malone."""
project = IProjectGroup(context)
return SimpleVocabulary([
SimpleTerm(product, product.name, title=product.displayname)
for product in project.products
if product.bug_tracking_usage == ServiceUsage.LAUNCHPAD])
class BugWatchVocabulary(SQLObjectVocabularyBase):
_table = BugWatch
def __iter__(self):
assert IBugTask.providedBy(self.context), (
"BugWatchVocabulary expects its context to be an IBugTask.")
bug = self.context.bug
for watch in bug.watches:
yield self.toTerm(watch)
def toTerm(self, watch):
def escape(string):
return cgi.escape(string, quote=True)
if watch.url.startswith('mailto:'):
user = getUtility(ILaunchBag).user
if user is None:
title = FormattersAPI(
watch.bugtracker.title).obfuscate_email()
return SimpleTerm(
watch, watch.id, escape(title))
else:
url = watch.url
title = escape(watch.bugtracker.title)
if url in title:
title = title.replace(
url, '<a href="%s">%s</a>' % (
escape(url), escape(url)))
else:
title = '%s <<a href="%s">%s</a>>' % (
title, escape(url), escape(url[7:]))
return SimpleTerm(watch, watch.id, title)
else:
return SimpleTerm(
watch, watch.id, '%s <a href="%s">#%s</a>' % (
escape(watch.bugtracker.title),
escape(watch.url),
escape(watch.remotebug)))
class DistributionUsingMaloneVocabulary:
"""All the distributions that uses Malone officially."""
implements(IVocabulary, IVocabularyTokenized)
_orderBy = 'displayname'
def __init__(self, context=None):
self.context = context
def __iter__(self):
"""Return an iterator which provides the terms from the vocabulary."""
distributions_using_malone = Distribution.selectBy(
official_malone=True, orderBy=self._orderBy)
for distribution in distributions_using_malone:
yield self.getTerm(distribution)
def __len__(self):
return Distribution.selectBy(official_malone=True).count()
def __contains__(self, obj):
return (IDistribution.providedBy(obj)
and obj.bug_tracking_usage == ServiceUsage.LAUNCHPAD)
def getTerm(self, obj):
if obj not in self:
raise LookupError(obj)
return SimpleTerm(obj, obj.name, obj.displayname)
def getTermByToken(self, token):
found_dist = Distribution.selectOneBy(
name=token, official_malone=True)
if found_dist is None:
raise LookupError(token)
return self.getTerm(found_dist)
def BugNominatableSeriesVocabulary(context=None):
"""Return a nominatable series vocabulary."""
if getUtility(ILaunchBag).distribution:
return BugNominatableDistroSeriesVocabulary(
context, getUtility(ILaunchBag).distribution)
else:
assert getUtility(ILaunchBag).product
return BugNominatableProductSeriesVocabulary(
context, getUtility(ILaunchBag).product)
class BugNominatableSeriesVocabularyBase(NamedSQLObjectVocabulary):
"""Base vocabulary class for series for which a bug can be nominated."""
def __iter__(self):
bug = self.context.bug
all_series = self._getNominatableObjects()
for series in sorted(all_series, key=attrgetter("displayname")):
if bug.canBeNominatedFor(series):
yield self.toTerm(series)
def toTerm(self, obj):
return SimpleTerm(obj, obj.name, obj.name.capitalize())
def getTermByToken(self, token):
obj = self._queryNominatableObjectByName(token)
if obj is None:
raise LookupError(token)
return self.toTerm(obj)
def _getNominatableObjects(self):
"""Return the series objects that the bug can be nominated for."""
raise NotImplementedError
def _queryNominatableObjectByName(self, name):
"""Return the series object with the given name."""
raise NotImplementedError
class BugNominatableProductSeriesVocabulary(
BugNominatableSeriesVocabularyBase):
"""The product series for which a bug can be nominated."""
_table = ProductSeries
def __init__(self, context, product):
BugNominatableSeriesVocabularyBase.__init__(self, context)
self.product = product
def _getNominatableObjects(self):
"""See BugNominatableSeriesVocabularyBase."""
return shortlist(self.product.series)
def _queryNominatableObjectByName(self, name):
"""See BugNominatableSeriesVocabularyBase."""
return self.product.getSeries(name)
class BugNominatableDistroSeriesVocabulary(
BugNominatableSeriesVocabularyBase):
"""The distribution series for which a bug can be nominated."""
_table = DistroSeries
def __init__(self, context, distribution):
BugNominatableSeriesVocabularyBase.__init__(self, context)
self.distribution = distribution
def _getNominatableObjects(self):
"""Return all non-obsolete distribution series"""
return [
series for series in shortlist(self.distribution.series)
if series.status != SeriesStatus.OBSOLETE]
def _queryNominatableObjectByName(self, name):
"""See BugNominatableSeriesVocabularyBase."""
return self.distribution.getSeries(name)
|