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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Sprint views."""
__metaclass__ = type
__all__ = [
'SprintAddView',
'SprintAttendeesCsvExportView',
'SprintBrandingView',
'SprintEditView',
'SprintFacets',
'SprintMeetingExportView',
'SprintNavigation',
'SprintOverviewMenu',
'SprintSetBreadcrumb',
'SprintSetFacets',
'SprintSetNavigation',
'SprintSetView',
'SprintSpecificationsMenu',
'SprintTopicSetView',
'SprintView',
]
from collections import defaultdict
import csv
from StringIO import StringIO
from lazr.restful.utils import smartquote
import pytz
from zope.app.form.browser import TextAreaWidget
from zope.component import getUtility
from zope.interface import implements
from canonical.launchpad import _
from canonical.launchpad.helpers import shortlist
from canonical.launchpad.webapp import (
canonical_url,
enabled_with_permission,
GetitemNavigation,
LaunchpadView,
Link,
Navigation,
NavigationMenu,
StandardLaunchpadFacets,
)
from canonical.launchpad.webapp.batching import BatchNavigator
from canonical.launchpad.webapp.breadcrumb import Breadcrumb
from lp.app.browser.launchpadform import (
action,
custom_widget,
LaunchpadEditFormView,
LaunchpadFormView,
)
from lp.app.interfaces.headings import IMajorHeadingView
from lp.app.widgets.date import DateTimeWidget
from lp.blueprints.browser.specificationtarget import (
HasSpecificationsMenuMixin,
HasSpecificationsView,
)
from lp.blueprints.enums import (
SpecificationDefinitionStatus,
SpecificationFilter,
SpecificationPriority,
SpecificationSort,
)
from lp.blueprints.interfaces.sprint import (
ISprint,
ISprintSet,
)
from lp.blueprints.model.specificationsubscription import (
SpecificationSubscription,
)
from lp.registry.browser.branding import BrandingChangeView
from lp.registry.browser.menu import (
IRegistryCollectionNavigationMenu,
RegistryCollectionActionMenuBase,
)
from lp.registry.interfaces.person import IPersonSet
from lp.services.database.bulk import load_referencing
from lp.services.propertycache import cachedproperty
class SprintFacets(StandardLaunchpadFacets):
"""The links that will appear in the facet menu for an ISprint."""
usedfor = ISprint
enable_only = ['overview', 'specifications']
def specifications(self):
text = 'Blueprints'
summary = 'Topics for discussion at %s' % self.context.title
return Link('', text, summary)
class SprintNavigation(Navigation):
usedfor = ISprint
class SprintOverviewMenu(NavigationMenu):
"""Defines a menu used for the global actions."""
usedfor = ISprint
facet = 'overview'
links = ['attendance', 'registration', 'attendee_export', 'edit',
'branding']
def attendance(self):
text = 'Register yourself'
summary = 'Register as an attendee of the meeting'
return Link('+attend', text, summary, icon='add')
def registration(self):
text = 'Register someone else'
summary = 'Register someone else to attend the meeting'
return Link('+register', text, summary, icon='add')
@enabled_with_permission('launchpad.View')
def attendee_export(self):
text = 'Export attendees to CSV'
summary = 'Export attendee contact information to CSV format'
return Link('+attendees-csv', text, summary, icon='info')
@enabled_with_permission('launchpad.Edit')
def edit(self):
text = 'Change details'
summary = 'Modify the meeting description, dates or title'
return Link('+edit', text, summary, icon='edit')
@enabled_with_permission('launchpad.Edit')
def branding(self):
text = 'Change branding'
summary = 'Modify the imagery used to represent this meeting'
return Link('+branding', text, summary, icon='edit')
class SprintSpecificationsMenu(NavigationMenu,
HasSpecificationsMenuMixin):
usedfor = ISprint
facet = 'specifications'
links = ['assignments', 'listdeclined', 'settopics', 'new']
@enabled_with_permission('launchpad.Driver')
def settopics(self):
text = 'Set agenda'
return Link('+settopics', text, icon='edit')
class SprintSetNavigation(GetitemNavigation):
usedfor = ISprintSet
class SprintSetBreadcrumb(Breadcrumb):
"""Builds a breadcrumb for an `ISprintSet`."""
text = 'Meetings'
class SprintSetFacets(StandardLaunchpadFacets):
"""The facet menu for an ISprintSet."""
usedfor = ISprintSet
enable_only = ['overview', ]
class SprintView(HasSpecificationsView):
implements(IMajorHeadingView)
# XXX Michael Nelson 20090923 bug=435255
# This class inherits a label from HasSpecificationsView, which causes
# a second h1 to display. But as this view implements IMajorHeadingView
# it should not include an h1 below the app buttons.
label = None
@property
def page_title(self):
return '%s (sprint or meeting)' % self.context.title
def initialize(self):
self.notices = []
self.latest_specs_limit = 5
self.tzinfo = pytz.timezone(self.context.time_zone)
def attendance(self):
"""establish if this user is attending"""
if self.user is None:
return None
for subscription in self.context.subscriptions:
if subscription.person.id == self.user.id:
return subscription
return None
@cachedproperty
def spec_links(self):
"""List all of the SprintSpecifications appropriate for this view."""
filter = self.spec_filter
return shortlist(self.context.specificationLinks(filter=filter))
@cachedproperty
def count(self):
return len(self.spec_links)
@cachedproperty
def proposed_count(self):
filter = [SpecificationFilter.PROPOSED]
return self.context.specificationLinks(filter=filter).count()
@cachedproperty
def latest_approved(self):
filter = [SpecificationFilter.ACCEPTED]
return self.context.specifications(filter=filter,
quantity=self.latest_specs_limit,
sort=SpecificationSort.DATE)
def formatDateTime(self, dt):
"""Format a datetime value according to the sprint's time zone"""
dt = dt.astimezone(self.tzinfo)
return dt.strftime('%Y-%m-%d %H:%M %Z')
def formatDate(self, dt):
"""Format a date value according to the sprint's time zone"""
dt = dt.astimezone(self.tzinfo)
return dt.strftime('%Y-%m-%d')
_local_timeformat = '%H:%M %Z on %A, %Y-%m-%d'
@property
def local_start(self):
"""The sprint start time, in the local time zone, as text."""
tz = pytz.timezone(self.context.time_zone)
return self.context.time_starts.astimezone(tz).strftime(
self._local_timeformat)
@property
def local_end(self):
"""The sprint end time, in the local time zone, as text."""
tz = pytz.timezone(self.context.time_zone)
return self.context.time_ends.astimezone(tz).strftime(
self._local_timeformat)
class SprintAddView(LaunchpadFormView):
"""Form for creating sprints"""
schema = ISprint
label = "Register a meeting"
field_names = ['name', 'title', 'summary', 'home_page', 'driver',
'time_zone', 'time_starts', 'time_ends', 'address',
]
custom_widget('summary', TextAreaWidget, height=5)
custom_widget('time_starts', DateTimeWidget, display_zone=False)
custom_widget('time_ends', DateTimeWidget, display_zone=False)
custom_widget('address', TextAreaWidget, height=3)
sprint = None
def setUpWidgets(self):
LaunchpadFormView.setUpWidgets(self)
timeformat = '%Y-%m-%d %H:%M'
self.widgets['time_starts'].timeformat = timeformat
self.widgets['time_ends'].timeformat = timeformat
time_zone_widget = self.widgets['time_zone']
if time_zone_widget.hasValidInput():
tz = pytz.timezone(time_zone_widget.getInputValue())
self.widgets['time_starts'].required_time_zone = tz
self.widgets['time_ends'].required_time_zone = tz
def validate(self, data):
time_starts = data.get('time_starts')
time_ends = data.get('time_ends')
if time_starts and time_ends and time_ends < time_starts:
self.setFieldError(
'time_ends', "This event can't start after it ends")
@action(_('Add Sprint'), name='add')
def add_action(self, action, data):
self.sprint = getUtility(ISprintSet).new(
owner=self.user,
name=data['name'],
title=data['title'],
summary=data['summary'],
home_page=data['home_page'],
driver=data['driver'],
time_zone=data['time_zone'],
time_starts=data['time_starts'],
time_ends=data['time_ends'],
address=data['address'],
)
self.request.response.addInfoNotification('Sprint created.')
@property
def next_url(self):
assert self.sprint is not None, 'No sprint has been created'
return canonical_url(self.sprint)
@property
def cancel_url(self):
return canonical_url(getUtility(ISprintSet))
class SprintBrandingView(BrandingChangeView):
schema = ISprint
# sabdfl 2007-03-28 deliberately leaving icon off the list, i think it
# would be overkill, we can add it later if people ask for it
field_names = ['logo', 'mugshot']
class SprintEditView(LaunchpadEditFormView):
"""Form for editing sprints"""
schema = ISprint
label = "Edit sprint details"
field_names = ['name', 'title', 'summary', 'home_page', 'driver',
'time_zone', 'time_starts', 'time_ends', 'address',
]
custom_widget('summary', TextAreaWidget, height=5)
custom_widget('time_starts', DateTimeWidget, display_zone=False)
custom_widget('time_ends', DateTimeWidget, display_zone=False)
custom_widget('address', TextAreaWidget, height=3)
def setUpWidgets(self):
LaunchpadEditFormView.setUpWidgets(self)
timeformat = '%Y-%m-%d %H:%M'
self.widgets['time_starts'].timeformat = timeformat
self.widgets['time_ends'].timeformat = timeformat
time_zone_widget = self.widgets['time_zone']
# What time zone are the start and end values relative to?
if time_zone_widget.hasValidInput():
tz = pytz.timezone(time_zone_widget.getInputValue())
else:
tz = pytz.timezone(self.context.time_zone)
self.widgets['time_starts'].required_time_zone = tz
self.widgets['time_ends'].required_time_zone = tz
def validate(self, data):
time_starts = data.get('time_starts')
time_ends = data.get('time_ends')
if time_starts and time_ends and time_ends < time_starts:
self.setFieldError(
'time_ends', "This event can't start after it ends")
@action(_('Change'), name='change')
def change_action(self, action, data):
self.updateContextFromData(data)
@property
def next_url(self):
return canonical_url(self.context)
@property
def cancel_url(self):
return canonical_url(self.context)
class SprintTopicSetView(HasSpecificationsView, LaunchpadView):
"""Custom view class to process the results of this unusual page.
It is unusual because we want to display multiple objects with
checkboxes, then process the selected items, which is not the usual
add/edit metaphor.
"""
@property
def label(self):
return smartquote(
'Review discussion topics for "%s" sprint' % self.context.title)
page_title = label
def initialize(self):
self.status_message = None
self.process_form()
self.attendee_ids = set(
attendance.attendeeID for attendance in self.context.attendances)
@cachedproperty
def spec_filter(self):
"""Return the specification links with PROPOSED status for this
sprint.
"""
return [SpecificationFilter.PROPOSED]
@cachedproperty
def spec_links(self):
filter = self.spec_filter
return self.context.specificationLinks(filter=filter)
def process_form(self):
"""Largely copied from webapp/generalform.py, without the
schema processing bits because we are not rendering the form in the
usual way. Instead, we are creating our own form in the page
template and interpreting it here.
"""
form = self.request.form
if 'SUBMIT_CANCEL' in form:
self.status_message = 'Cancelled'
self.request.response.redirect(
canonical_url(self.context)+'/+specs')
return
if 'SUBMIT_ACCEPT' not in form and 'SUBMIT_DECLINE' not in form:
self.status_message = ''
return
if self.request.method == 'POST':
if 'speclink' not in form:
self.status_message = (
'Please select specifications to accept or decline.')
return
# determine if we are accepting or declining
if 'SUBMIT_ACCEPT' in form:
assert 'SUBMIT_DECLINE' not in form
action = 'Accepted'
else:
assert 'SUBMIT_DECLINE' in form
action = 'Declined'
selected_specs = form['speclink']
if isinstance(selected_specs, unicode):
# only a single item was selected, but we want to deal with a
# list for the general case, so convert it to a list
selected_specs = [selected_specs]
if action == 'Accepted':
action_fn = self.context.acceptSpecificationLinks
else:
action_fn = self.context.declineSpecificationLinks
leftover = action_fn(selected_specs, self.user)
# Status message like: "Accepted 27 specification(s)."
self.status_message = '%s %d specification(s).' % (
action, len(selected_specs))
if leftover == 0:
# they are all done, so redirect back to the spec listing page
self.request.response.redirect(
canonical_url(self.context)+'/+specs')
class SprintMeetingExportView(LaunchpadView):
"""View to provide information used the sprint meeting XML export view."""
def initialize(self):
self.attendees = []
attendee_set = set()
for attendance in self.context.attendances:
self.attendees.append(dict(
name=attendance.attendee.name,
displayname=attendance.attendee.displayname,
start=attendance.time_starts.strftime('%Y-%m-%dT%H:%M:%SZ'),
end=attendance.time_ends.strftime('%Y-%m-%dT%H:%M:%SZ')))
attendee_set.add(attendance.attendeeID)
model_specs = []
for spec in self.context.specifications(
filter=[SpecificationFilter.ACCEPTED]):
# skip sprints with no priority or less than low:
if spec.priority < SpecificationPriority.UNDEFINED:
continue
if (spec.definition_status not in
[SpecificationDefinitionStatus.NEW,
SpecificationDefinitionStatus.DISCUSSION,
SpecificationDefinitionStatus.DRAFT]):
continue
model_specs.append(spec)
people = defaultdict(dict)
# Attendees per specification
for subscription in load_referencing(SpecificationSubscription,
model_specs, ['specificationID']):
if subscription.personID not in attendee_set:
continue
people[subscription.specificationID][subscription.personID] = \
subscription.essential
# Spec specials - drafter/assignee. Don't need approver for performance
# as specifications() above eager loaded the people, and approvers
# don't count as a 'required person'.
for spec in model_specs:
# get the list of attendees that will attend the sprint
spec_people = people[spec.id]
if spec.assigneeID is not None:
spec_people[spec.assigneeID] = True
attendee_set.add(spec.assigneeID)
if spec.drafterID is not None:
spec_people[spec.drafterID] = True
attendee_set.add(spec.drafterID)
people_by_id = dict((person.id, person) for person in
getUtility(IPersonSet).getPrecachedPersonsFromIDs(attendee_set))
self.specifications = [
dict(spec=spec, interested=[
dict(name=people_by_id[person_id].name, required=required)
for (person_id, required) in people[spec.id].items()]
) for spec in model_specs]
def render(self):
self.request.response.setHeader('content-type',
'application/xml;charset=utf-8')
body = LaunchpadView.render(self)
return body.encode('utf-8')
class SprintSetNavigationMenu(RegistryCollectionActionMenuBase):
"""Action menu for sprints index."""
usedfor = ISprintSet
links = (
'register_team',
'register_project',
'register_sprint',
'create_account',
'view_all_sprints',
)
@enabled_with_permission('launchpad.View')
def register_sprint(self):
text = 'Register a meeting'
summary = 'Register a developer sprint, summit, or gathering'
return Link('+new', text, summary=summary, icon='add')
def view_all_sprints(self):
text = 'Show all meetings'
return Link('+all', text, icon='list')
class SprintSetView(LaunchpadView):
"""View for the /sprints top level collection page."""
implements(IRegistryCollectionNavigationMenu)
page_title = 'Meetings and sprints registered in Launchpad'
def all_batched(self):
return BatchNavigator(self.context.all, self.request)
class SprintAttendeesCsvExportView(LaunchpadView):
"""View for exporting the attendees for a sprint as CSV."""
def encode_value(self, value):
"""Encode a value for CSV.
Return the string representation of `value` encoded as UTF-8,
or the empty string if value is None."""
if value is not None:
return unicode(value).encode('utf-8')
else:
return ''
def render(self):
"""Render a CSV output of all the attendees for a sprint."""
rows = [('Launchpad username',
'Display name',
'Email',
'IRC nickname',
'Phone',
'Organization',
'City',
'Country',
'Timezone',
'Arriving',
'Leaving',
'Physically present',
)]
for attendance in self.context.attendances:
time_zone = ''
location = attendance.attendee.location
if location is not None and location.visible:
time_zone = attendance.attendee.time_zone
irc_nicknames = ', '.join(sorted(set(
[ircid.nickname for ircid
in attendance.attendee.ircnicknames])))
rows.append(
(attendance.attendee.name,
attendance.attendee.displayname,
attendance.attendee.safe_email_or_blank,
irc_nicknames,
# We used to store phone, organization, city and
# country, but this was a lie because users could not
# update these fields.
'', # attendance.attendee.phone
'', # attendance.attendee.organization
'', # attendance.attendee.city
'', # country
time_zone,
attendance.time_starts.strftime('%Y-%m-%dT%H:%M:%SZ'),
attendance.time_ends.strftime('%Y-%m-%dT%H:%M:%SZ'),
attendance.is_physical))
# CSV can't handle unicode, so we force encoding
# everything as UTF-8
rows = [[self.encode_value(column)
for column in row]
for row in rows]
self.request.response.setHeader('Content-type', 'text/csv')
self.request.response.setHeader(
'Content-disposition',
'attachment; filename=%s-attendees.csv' % self.context.name)
output = StringIO()
writer = csv.writer(output)
writer.writerows(rows)
return output.getvalue()
|