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
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
# pylint: disable-msg=E0611,W0212
__metaclass__ = type
__all__ = [
'Sprint',
'SprintSet',
'HasSprintsMixin',
]
from sqlobject import (
ForeignKey,
StringCol,
)
from storm.locals import Store
from zope.component import getUtility
from zope.interface import implements
from lp.app.interfaces.launchpad import (
IHasIcon,
IHasLogo,
IHasMugshot,
ILaunchpadCelebrities,
)
from lp.blueprints.enums import (
SpecificationFilter,
SpecificationImplementationStatus,
SpecificationSort,
SprintSpecificationStatus,
)
from lp.blueprints.interfaces.sprint import (
ISprint,
ISprintSet,
)
from lp.blueprints.model.specification import HasSpecificationsMixin
from lp.blueprints.model.sprintattendance import SprintAttendance
from lp.blueprints.model.sprintspecification import SprintSpecification
from lp.registry.interfaces.person import (
IPersonSet,
validate_public_person,
)
from lp.registry.model.hasdrivers import HasDriversMixin
from lp.services.database.constants import DEFAULT
from lp.services.database.datetimecol import UtcDateTimeCol
from lp.services.database.sqlbase import (
flush_database_updates,
quote,
SQLBase,
)
class Sprint(SQLBase, HasDriversMixin, HasSpecificationsMixin):
"""See `ISprint`."""
implements(ISprint, IHasLogo, IHasMugshot, IHasIcon)
_defaultOrder = ['name']
# db field names
owner = ForeignKey(
dbName='owner', foreignKey='Person',
storm_validator=validate_public_person, notNull=True)
name = StringCol(notNull=True, alternateID=True)
title = StringCol(notNull=True)
summary = StringCol(notNull=True)
driver = ForeignKey(
dbName='driver', foreignKey='Person',
storm_validator=validate_public_person)
home_page = StringCol(notNull=False, default=None)
homepage_content = StringCol(default=None)
icon = ForeignKey(
dbName='icon', foreignKey='LibraryFileAlias', default=None)
logo = ForeignKey(
dbName='logo', foreignKey='LibraryFileAlias', default=None)
mugshot = ForeignKey(
dbName='mugshot', foreignKey='LibraryFileAlias', default=None)
address = StringCol(notNull=False, default=None)
datecreated = UtcDateTimeCol(notNull=True, default=DEFAULT)
time_zone = StringCol(notNull=True)
time_starts = UtcDateTimeCol(notNull=True)
time_ends = UtcDateTimeCol(notNull=True)
# attributes
# we want to use this with templates that can assume a displayname,
# because in many ways a sprint behaves just like a project or a
# product - it has specs
@property
def displayname(self):
return self.title
@property
def drivers(self):
"""See IHasDrivers."""
if self.driver is not None:
return [self.driver, self.owner]
return [self.owner]
@property
def attendees(self):
# Only really used in tests.
return [a.attendee for a in self.attendances]
def spec_filter_clause(self, filter=None):
"""Figure out the appropriate query for specifications on a sprint.
We separate out the query generation from the normal
specifications() method because we want to reuse this query in the
specificationLinks() method.
"""
# Make a new list of the filter, so that we do not mutate what we
# were passed as a filter
if not filter:
# filter could be None or [] then we decide the default
# which for a sprint is to show everything approved
filter = [SpecificationFilter.ACCEPTED]
# figure out what set of specifications we are interested in. for
# sprint, we need to be able to filter on the basis of:
#
# - completeness.
# - acceptance for sprint agenda.
# - informational.
#
base = """SprintSpecification.sprint = %s AND
SprintSpecification.specification = Specification.id AND
(Specification.product IS NULL OR
Specification.product NOT IN
(SELECT Product.id FROM Product
WHERE Product.active IS FALSE))
""" % quote(self)
query = base
# look for informational specs
if SpecificationFilter.INFORMATIONAL in filter:
query += (' AND Specification.implementation_status = %s' %
quote(SpecificationImplementationStatus.INFORMATIONAL))
# import here to avoid circular deps
from lp.blueprints.model.specification import Specification
# filter based on completion. see the implementation of
# Specification.is_complete() for more details
completeness = Specification.completeness_clause
if SpecificationFilter.COMPLETE in filter:
query += ' AND ( %s ) ' % completeness
elif SpecificationFilter.INCOMPLETE in filter:
query += ' AND NOT ( %s ) ' % completeness
# look for specs that have a particular SprintSpecification
# status (proposed, accepted or declined)
if SpecificationFilter.ACCEPTED in filter:
query += ' AND SprintSpecification.status = %s' % (
quote(SprintSpecificationStatus.ACCEPTED))
elif SpecificationFilter.PROPOSED in filter:
query += ' AND SprintSpecification.status = %s' % (
quote(SprintSpecificationStatus.PROPOSED))
elif SpecificationFilter.DECLINED in filter:
query += ' AND SprintSpecification.status = %s' % (
quote(SprintSpecificationStatus.DECLINED))
# ALL is the trump card
if SpecificationFilter.ALL in filter:
query = base
# Filter for specification text
for constraint in filter:
if isinstance(constraint, basestring):
# a string in the filter is a text search filter
query += ' AND Specification.fti @@ ftq(%s) ' % quote(
constraint)
return query
@property
def has_any_specifications(self):
"""See IHasSpecifications."""
return self.all_specifications.count()
@property
def all_specifications(self):
return self.specifications(filter=[SpecificationFilter.ALL])
def specifications(self, sort=None, quantity=None, filter=None,
prejoin_people=True):
"""See IHasSpecifications."""
query = self.spec_filter_clause(filter=filter)
if filter == None:
filter = []
# import here to avoid circular deps
from lp.blueprints.model.specification import Specification
# sort by priority descending, by default
if sort is None or sort == SpecificationSort.PRIORITY:
order = ['-priority', 'Specification.definition_status',
'Specification.name']
elif sort == SpecificationSort.DATE:
# we need to establish if the listing will show specs that have
# been decided only, or will include proposed specs.
show_proposed = set([
SpecificationFilter.ALL,
SpecificationFilter.PROPOSED,
])
if len(show_proposed.intersection(set(filter))) > 0:
# we are showing proposed specs so use the date proposed
# because not all specs will have a date decided.
order = ['-SprintSpecification.date_created',
'Specification.id']
else:
# this will show only decided specs so use the date the spec
# was accepted or declined for the sprint
order = ['-SprintSpecification.date_decided',
'-SprintSpecification.date_created',
'Specification.id']
results = Specification.select(query, orderBy=order, limit=quantity,
clauseTables=['SprintSpecification'])
if prejoin_people:
results = results.prejoin(['assignee', 'approver', 'drafter'])
return results
def specificationLinks(self, sort=None, quantity=None, filter=None):
"""See `ISprint`."""
query = self.spec_filter_clause(filter=filter)
# sort by priority descending, by default
if sort is None or sort == SpecificationSort.PRIORITY:
order = ['-priority', 'status', 'name']
elif sort == SpecificationSort.DATE:
order = ['-datecreated', 'id']
results = SprintSpecification.select(query,
clauseTables=['Specification'], orderBy=order, limit=quantity)
return results.prejoin(['specification'])
def getSpecificationLink(self, speclink_id):
"""See `ISprint`.
NB: we expose the horrible speclink.id because there is no unique
way to refer to a specification outside of a product or distro
context. Here we are a sprint that could cover many products and/or
distros.
"""
speclink = SprintSpecification.get(speclink_id)
assert (speclink.sprint.id == self.id)
return speclink
def acceptSpecificationLinks(self, idlist, decider):
"""See `ISprint`."""
for sprintspec in idlist:
speclink = self.getSpecificationLink(sprintspec)
speclink.acceptBy(decider)
# we need to flush all the changes we have made to disk, then try
# the query again to see if we have any specs remaining in this
# queue
flush_database_updates()
return self.specifications(
filter=[SpecificationFilter.PROPOSED]).count()
def declineSpecificationLinks(self, idlist, decider):
"""See `ISprint`."""
for sprintspec in idlist:
speclink = self.getSpecificationLink(sprintspec)
speclink.declineBy(decider)
# we need to flush all the changes we have made to disk, then try
# the query again to see if we have any specs remaining in this
# queue
flush_database_updates()
return self.specifications(
filter=[SpecificationFilter.PROPOSED]).count()
# attendance
def attend(self, person, time_starts, time_ends, is_physical):
"""See `ISprint`."""
# First see if a relevant attendance exists, and if so, update it.
attendance = Store.of(self).find(
SprintAttendance,
SprintAttendance.sprint == self,
SprintAttendance.attendee == person).one()
if attendance is None:
# Since no previous attendance existed, create a new one.
attendance = SprintAttendance(sprint=self, attendee=person)
attendance.time_starts = time_starts
attendance.time_ends = time_ends
attendance.is_physical = is_physical
return attendance
def removeAttendance(self, person):
"""See `ISprint`."""
Store.of(self).find(
SprintAttendance,
SprintAttendance.sprint == self,
SprintAttendance.attendee == person).remove()
@property
def attendances(self):
result = list(Store.of(self).find(
SprintAttendance,
SprintAttendance.sprint == self))
people = [a.attendeeID for a in result]
# In order to populate the person cache we need to materialize the
# result set. Listification should do.
list(getUtility(IPersonSet).getPrecachedPersonsFromIDs(
people, need_validity=True))
return sorted(result, key=lambda a: a.attendee.displayname.lower())
# linking to specifications
def linkSpecification(self, spec):
"""See `ISprint`."""
for speclink in self.spec_links:
if speclink.spec.id == spec.id:
return speclink
return SprintSpecification(sprint=self, specification=spec)
def unlinkSpecification(self, spec):
"""See `ISprint`."""
for speclink in self.spec_links:
if speclink.spec.id == spec.id:
SprintSpecification.delete(speclink.id)
return speclink
def isDriver(self, user):
"""See `ISprint`."""
admins = getUtility(ILaunchpadCelebrities).admin
return (user.inTeam(self.owner) or
user.inTeam(self.driver) or
user.inTeam(admins))
class SprintSet:
"""The set of sprints."""
implements(ISprintSet)
def __init__(self):
"""See `ISprintSet`."""
self.title = 'Sprints and meetings'
def __getitem__(self, name):
"""See `ISprintSet`."""
return Sprint.selectOneBy(name=name)
def __iter__(self):
"""See `ISprintSet`."""
return iter(Sprint.select("time_ends > 'NOW'", orderBy='time_starts'))
@property
def all(self):
return Sprint.select(orderBy='-time_starts')
def new(self, owner, name, title, time_zone, time_starts, time_ends,
summary, address=None, driver=None, home_page=None,
mugshot=None, logo=None, icon=None):
"""See `ISprintSet`."""
return Sprint(owner=owner, name=name, title=title,
time_zone=time_zone, time_starts=time_starts,
time_ends=time_ends, summary=summary, driver=driver,
home_page=home_page, mugshot=mugshot, icon=icon,
logo=logo, address=address)
class HasSprintsMixin:
"""A mixin class implementing the common methods for any class
implementing IHasSprints.
"""
def _getBaseQueryAndClauseTablesForQueryingSprints(self):
"""Return the base SQL query and the clauseTables to be used when
querying sprints related to this object.
Subclasses must overwrite this method if it doesn't suit them.
"""
query = """
Specification.%s = %s
AND Specification.id = SprintSpecification.specification
AND SprintSpecification.sprint = Sprint.id
AND SprintSpecification.status = %s
""" % (self._table, self.id,
quote(SprintSpecificationStatus.ACCEPTED))
return query, ['Specification', 'SprintSpecification']
@property
def sprints(self):
"""See IHasSprints."""
query, tables = self._getBaseQueryAndClauseTablesForQueryingSprints()
return Sprint.select(
query, clauseTables=tables, orderBy='-time_starts', distinct=True)
@property
def coming_sprints(self):
"""See IHasSprints."""
query, tables = self._getBaseQueryAndClauseTablesForQueryingSprints()
query += " AND Sprint.time_ends > 'NOW'"
return Sprint.select(
query, clauseTables=tables, orderBy='time_starts',
distinct=True, limit=5)
@property
def past_sprints(self):
"""See IHasSprints."""
query, tables = self._getBaseQueryAndClauseTablesForQueryingSprints()
query += " AND Sprint.time_ends <= 'NOW'"
return Sprint.select(
query, clauseTables=tables, orderBy='-time_starts',
distinct=True)
|