~launchpad-pqm/launchpad/devel

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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
Bug Filing Pages
================

Filing a Bug
------------

There are three objects on which you can file a bug. An
ObjectCreatedEvent is published when the bug is filed. Let's register
an event listener to demonstrate this.

    >>> from canonical.database.sqlbase import flush_database_updates
    >>> import transaction

    >>> from canonical.launchpad.ftests.event import TestEventListener
    >>> from lazr.lifecycle.event import IObjectCreatedEvent
    >>> from lp.bugs.interfaces.bug import IBug
    >>> def on_created_event(object, event):
    ...     print "ObjectCreatedEvent: %r" % object
    >>> on_created_listener = TestEventListener(
    ...     IBug, IObjectCreatedEvent, on_created_event)

1. Filing a bug on a distribution.

The distribution filebug page will attach a bugtask to a sourcepackage
if the user provides a valid package name when reporting the bug.

If the package name entered by the user happens to be a binary package
name, that information is recorded in the description, and the first
comment, of the bug report.

    >>> from zope.component import getMultiAdapter, getUtility
    >>> from canonical.launchpad.webapp.interfaces import (
    ...     ILaunchBag,
    ...     IOpenLaunchBag,
    ...     )
    >>> from canonical.launchpad.webapp.servers import LaunchpadTestRequest
    >>> from lp.bugs.interfaces.bug import IBugSet
    >>> from lp.bugs.interfaces.bugtask import (
    ...     BugTaskSearchParams,
    ...     IBugTaskSet,
    ...     )
    >>> from lp.registry.interfaces.distribution import IDistributionSet
    >>> from lp.registry.interfaces.person import IPersonSet
    >>> from lp.registry.interfaces.product import IProductSet

    >>> launchbag = getUtility(IOpenLaunchBag)
    >>> login("foo.bar@canonical.com")

    >>> ubuntu = getUtility(IDistributionSet).getByName("ubuntu")
    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.title': 'bug in bin pkg',
    ...           'field.comment': 'a bug in a bin pkg',
    ...           'packagename_option': 'choose',
    ...           'field.packagename': 'linux-2.6.12',
    ...           'field.actions.submit_bug': 'Submit Bug Report'})

    >>> ubuntu_filebug = getMultiAdapter((ubuntu, request), name="+filebug")
    >>> launchbag.clear()
    >>> launchbag.add(ubuntu)

    >>> ubuntu_filebug.initialize()
    ObjectCreatedEvent: <Bug at ...>

    >>> launchbag.clear()

    >>> current_user = getUtility(ILaunchBag).user
    >>> search_params = BugTaskSearchParams(
    ...     searchtext="bin pkg", user=current_user)

    >>> latest_ubuntu_bugtask = ubuntu.searchTasks(search_params)[0]

The user specified a binary package name, so that's been added to the
bug description and the first comment:

    >>> print latest_ubuntu_bugtask.bug.description
    a bug in a bin pkg

the source package from which the binary was built has been set on
the bugtask.

    >>> print latest_ubuntu_bugtask.sourcepackagename.name
    linux-source-2.6.15

2. Filing a bug on a product.

    >>> firefox = getUtility(IProductSet).getByName("firefox")
    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.title': 'a firefox bug',
    ...           'field.comment': 'a test bug',
    ...           'field.actions.submit_bug': 'Submit Bug Report'})

    >>> firefox_filebug = getMultiAdapter((firefox, request), name="+filebug")

    >>> firefox_filebug.initialize()
    ObjectCreatedEvent: <Bug at ...>

3. Filing a bug on a distribution source package.

You can also access the +filebug page from a sourcepackage.

    >>> ubuntu_firefox = ubuntu.getSourcePackage("mozilla-firefox")

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.title': 'a firefox bug',
    ...           'field.comment': 'a test bug',
    ...           'packagename_option': 'choose',
    ...           'field.packagename': 'mozilla-firefox',
    ...           'field.actions.submit_bug': 'Submit Bug Report'})

    >>> ubuntu_firefox_filebug = getMultiAdapter(
    ...     (ubuntu_firefox, request), name="+filebug")

    >>> launchbag.add(ubuntu)

    >>> ubuntu_firefox_filebug.initialize()
    ObjectCreatedEvent: <Bug at ...>

    >>> launchbag.clear()

Adding Comments
---------------

Let's flush all changes so far to ensure we're looking at a consistent view of
the database.

    >>> flush_database_updates()
    >>> transaction.commit()

To add new comments, users POST to the +addcomment page:

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.subject': latest_ubuntu_bugtask.bug.title,
    ...           'field.comment': 'I can reproduce this bug.',
    ...           'field.actions.save': 'Save Changes'})
    >>> ubuntu_addcomment = getMultiAdapter(
    ...     (latest_ubuntu_bugtask, request), name="+addcomment-form")
    >>> ubuntu_addcomment.initialize()

They may even, by mistake, post the same comment twice:

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.subject': latest_ubuntu_bugtask.bug.title,
    ...           'field.comment': 'I can reproduce this bug.',
    ...           'field.actions.save': 'Save Changes'})
    >>> ubuntu_addcomment = getMultiAdapter(
    ...     (latest_ubuntu_bugtask, request), name="+addcomment-form")
    >>> ubuntu_addcomment.initialize()

Comments are cached in the view, so we need to flush updates and then
grab a new view to actually see them:

    >>> flush_database_updates()
    >>> transaction.commit()

    >>> ubuntu_bugview = getMultiAdapter(
    ...     (latest_ubuntu_bugtask, request), name="+index")
    >>> print len(ubuntu_bugview.comments)
    3
    >>> [ (c.index, c.owner.name, c.text_contents)
    ...  for c in ubuntu_bugview.comments ]
    [(0, u'name16', u'a bug in a bin pkg'),
     (1, u'name16', u'I can reproduce this bug.'),
     (2, u'name16', u'I can reproduce this bug.')]


Description and Comment Display
-------------------------------

When a user posts a new bug, the first comment and the description are
identical. Take as an example the first bug posted above:

    >>> print latest_ubuntu_bugtask.bug.description
    a bug in a bin pkg

Its description has the same contents as the bug's first comment:

    >>> print latest_ubuntu_bugtask.bug.messages[0].text_contents
    a bug in a bin pkg

The view class offers a method to check exactly that:

    >>> ubuntu_bugview.wasDescriptionModified()
    False

If we go ahead and modify the description, however:

    >>> latest_ubuntu_bugtask.bug.description = "A bug in the linux kernel"
    >>> flush_database_updates()
    >>> transaction.commit()

    >>> ubuntu_bugview.wasDescriptionModified()
    True

The displayable comments for a bug can be obtained from the view
property activity_and_comments.

    >>> comments = [event.get('comment') for event in
    ...     ubuntu_bugview.activity_and_comments if event.get('comment')]

Because we omit the first comment, and because the third comment is
indentical to the second, we really only display one comment:

    >>> print len(comments)
    1
    >>> [(c.index, c.owner.name, c.text_contents) for c in comments]
    [(1, u'name16', u'I can reproduce this bug.')]

(Unregister our listener, since we no longer need it.)

    >>> on_created_listener.unregister()


Bug Portlets
============

Duplicates Portlet
------------------

The duplicate bugs portlet lists duplicates of the current bug. If the
duplicate bug affects the current context, the link to the dupe will
remain in the current context. If the dupe has not been reported in
the current context, the dupe link will be to the generic
/bugs/$bug.id redirect link.

    >>> bugtaskset = getUtility(IBugTaskSet)
    >>> bugset = getUtility(IBugSet)

Bug 6 is a duplicate of bug 5, and since both bugs affect Firefox, the
duplicate link remains in the current context.

    >>> bug_five_in_firefox = bugtaskset.get(14)

    >>> print bug_five_in_firefox.bug.id
    5
    >>> print bug_five_in_firefox.product.name
    firefox


    >>> bug_page_view = getMultiAdapter(
    ...     (bug_five_in_firefox.bug, request), name="+portlet-duplicates")

    >>> bug_six = bugset.get(6)

    >>> getUtility(IOpenLaunchBag).add(bug_five_in_firefox)

    >>> for dupe in bug_page_view.duplicates():
    ...     print dupe['url']
    http://.../firefox/+bug/6

Bug 2 is not reported in Firefox. Let's mark bug 2 as a dupe of bug 5,
and see how the returned link changes.

    >>> bug_two = bugset.get(2)
    >>> bug_five = bugset.get(5)
    >>> bug_two.markAsDuplicate(bug_five)

    >>> bug_page_view = getMultiAdapter(
    ...     (bug_five_in_firefox.bug, request), name="+portlet-duplicates")

    >>> for dupe in bug_page_view.duplicates():
    ...     print dupe['url']
    http://.../bugs/2
    ...


Bug Attachments
---------------

We show bug attachments in two lists: patches and non-patch attachments.
Sequences with data about patch and non-patch attachments are provided
by the properties `patches` and `regular_attachments` of the class
BugView. The elements of the sequences are dictionaries containing
the the attachment itself and a ProxiedLibraryFileAlias for the
librarian file of the attachment.

    >>> from lp.bugs.browser.bug import BugView
    >>> login('foo.bar@canonical.com')
    >>> request = LaunchpadTestRequest()
    >>> bug_seven = bugset.get(7)
    >>> attachment_1 = factory.makeBugAttachment(
    ...     bug=bug_seven, description='attachment 1', is_patch=False)
    >>> attachment_2 = factory.makeBugAttachment(
    ...     bug=bug_seven, description='attachment 2', is_patch=False)
    >>> patch_1 = factory.makeBugAttachment(
    ...     bug=bug_seven, description='patch 1', is_patch=True)
    >>> patch_2 = factory.makeBugAttachment(
    ...     bug=bug_seven, description='patch 2', is_patch=True)
    >>> view = BugView(bug_seven, request)
    >>> [attachment['attachment'].title
    ...  for attachment in view.regular_attachments]
    [u'attachment 1', u'attachment 2']
    >>> [patch['attachment'].title for patch in view.patches]
    [u'patch 1', u'patch 2']
    >>> [attachment['file'].http_url
    ...  for attachment in view.regular_attachments]
    [u'http://bugs.launchpad.dev/firefox/+bug/5/+attachment/1/+files/...',
     u'http://bugs.launchpad.dev/firefox/+bug/5/+attachment/2/+files/...']
    >>> [patch['file'].http_url for patch in view.patches]
    [u'http://bugs.launchpad.dev/firefox/+bug/5/+attachment/3/+files/...',
     u'http://bugs.launchpad.dev/firefox/+bug/5/+attachment/4/+files/...']


Bug Navigation
--------------

The +subscribe link has different text depending on if the user is
subscribed to the bug, or if a team the user of a member of is
subscribed to it.

If the user isn't subscribed to the bug , 'Subscribe' is shown.

    >>> login("foo.bar@canonical.com")
    >>> foo_bar = getUtility(IPersonSet).getByEmail('foo.bar@canonical.com')
    >>> bug_one = getUtility(IBugSet).get(1)
    >>> bug_one.isSubscribed(foo_bar)
    False

    >>> from lp.bugs.browser.bug import BugContextMenu
    >>> bug_one_bugtask = bug_one.bugtasks[0]
    >>> getUtility(IOpenLaunchBag).clear()
    >>> getUtility(IOpenLaunchBag).add(bug_one_bugtask)
    >>> bug_menu = BugContextMenu(bug_one_bugtask)
    >>> bug_menu.subscription().text
    'Subscribe'

    >>> bug_menu.subscription().icon
    'add'

If we subscribe Foo Bar, 'Edit subscription' is shown.

    >>> bug_one.subscribe(foo_bar, foo_bar)
    <lp.bugs.model.bugsubscription.BugSubscription ...>
    >>> bug_menu = BugContextMenu(bug_one_bugtask)
    >>> bug_menu.subscription().text
    'Edit subscription'

    >>> bug_menu.subscription().icon
    'edit'

If we subscribe one of the teams that Foo Bar is a member of, it will
still say 'Edit subscription':

    >>> launchpad_team = getUtility(IPersonSet).getByName('launchpad')
    >>> foo_bar.inTeam(launchpad_team)
    True
    >>> bug_one.subscribe(launchpad_team, launchpad_team)
    <lp.bugs.model.bugsubscription.BugSubscription ...>
    >>> bug_menu = BugContextMenu(bug_one_bugtask)
    >>> bug_menu.subscription().text
    'Edit subscription'

    >>> bug_menu.subscription().icon
    'edit'

If we now unsubscribe Foo Bar, it will say 'Subscribe', since team
unsubsription is handled by the remove icon next the the team in the
subscribers portlet.

    >>> bug_one.unsubscribe(foo_bar, foo_bar)

    >>> bug_menu = BugContextMenu(bug_one_bugtask)
    >>> bug_menu.subscription().text
    'Subscribe'

    >>> bug_menu.subscription().icon
    'add'

If the user is logged out, it says 'Subscribe/Unsubscribe', since we
can't know if the user is subscribed or not.

    >>> login(ANONYMOUS)
    >>> bug_menu = BugContextMenu(bug_one_bugtask)
    >>> bug_menu.subscription().text
    'Subscribe/Unsubscribe'

    >>> bug_menu.subscription().icon
    'edit'

    Subscribers from duplicates have the option to unsubscribe as well. For
    example, Steve Alexander can currently subscribe to bug #3.

    >>> bug_three = bugset.get(3)
    >>> bug_three_bugtask = bug_three.bugtasks[0]
    >>> getUtility(IOpenLaunchBag).clear()
    >>> getUtility(IOpenLaunchBag).add(bug_three_bugtask)

    >>> login("steve.alexander@ubuntulinux.com")

    >>> bug_menu = BugContextMenu(bug_three_bugtask)
    >>> bug_menu.subscription().text
        'Subscribe'

    Bug if bug #2, a bug that Steve is directly subscribed to, is marked as
    a dupe of bug #3, then Steve gets indirectly subscribed to bug #3, and
    is presented with the "Edit subscription" link.

    >>> bug_two.markAsDuplicate(bug_three)

    >>> bug_menu.subscription().text
    'Edit subscription'

    Now, let's revert that duplicate marking and demonstrate it again, this
    time where the subscription from the duplicate is of a /team/ of which
    the current user is a member. So, for Foo Bar, bug #3 has a simple
    Subscribe link initially.

    >>> bug_two.markAsDuplicate(None)

    >>> login("foo.bar@canonical.com")

    >>> bug_menu.subscription().text
        'Subscribe'

    Now let's subscribe Ubuntu Team directly to bug #2. When bug #2 is duped
    against bug #3, the link didn't change to Subscribe/Unsubscribe

    >>> ubuntu_team = getUtility(IPersonSet).getByName("ubuntu-team")
    >>> bug_two.subscribe(ubuntu_team, ubuntu_team)
        <lp.bugs.model.bugsubscription.BugSubscription ...>

    >>> bug_two.markAsDuplicate(bug_three)

    >>> bug_menu.subscription().text
    'Subscribe'


BugTasks and Nominations Table
------------------------------

A table is rendered at the top of the bug page which shows both bugtasks
and nominations. This table is rendered with the
+bugtasks-and-nomination-table view.

    >>> request = LaunchpadTestRequest()

    >>> bugtasks_and_nominations_view = getMultiAdapter(
    ...     (bug_one_bugtask.bug, request),
    ...     name="+bugtasks-and-nominations-table")
    >>> bugtasks_and_nominations_view.initialize()

The getBugTaskAndNominationViews method returns a list of views for
bugtasks and nominations to render in the table, sorted by
bugtargetdisplayname. Approved nominations are not included in the
returned results, because an approved nomination will have created a
task anyway.

    >>> from lp.bugs.interfaces.bugnomination import IBugNomination
    >>> from lp.bugs.interfaces.bugtask import IBugTask

    >>> def get_object_type(task_or_nomination):
    ...     if IBugTask.providedBy(task_or_nomination):
    ...         return "bugtask"
    ...     elif IBugNomination.providedBy(task_or_nomination):
    ...         return "nomination"
    ...     else:
    ...         return "unknown"

    >>> def print_tasks_and_nominations(task_and_nomination_views):
    ...     for task_or_nomination_view in task_and_nomination_views:
    ...         task_or_nomination = task_or_nomination_view.context
    ...         print "%s, %s, %s" % (
    ...             get_object_type(task_or_nomination),
    ...             task_or_nomination.status.title,
    ...             task_or_nomination.target.bugtargetdisplayname)

    >>> task_and_nomination_views = (
    ...     bugtasks_and_nominations_view.getBugTaskAndNominationViews())

    >>> print_tasks_and_nominations(task_and_nomination_views)
    bugtask, New, Mozilla Firefox
    nomination, Nominated, Mozilla Firefox 1.0
    bugtask, Confirmed, mozilla-firefox (Debian)
    bugtask, New, mozilla-firefox (Ubuntu)
    nomination, Nominated, Ubuntu Hoary

After creating bug supervisors for Ubuntu and Firefox Let's nominate the bug
for upstream and an Ubuntu series and see how the list changes.

    >>> from lp.testing.sampledata import (ADMIN_EMAIL)
    >>> from zope.component import getUtility
    >>> from zope.security.proxy import removeSecurityProxy
    >>>
    >>> login(ADMIN_EMAIL)
    >>> nominator = factory.makePerson(name='nominator')
    >>> ubuntu = getUtility(IDistributionSet).getByName("ubuntu")
    >>> ubuntu = removeSecurityProxy(ubuntu)
    >>> ubuntu.bug_supervisor = nominator
    >>> firefox = getUtility(IProductSet).getByName("firefox")
    >>> firefox = removeSecurityProxy(firefox)
    >>> firefox.bug_supervisor = nominator
    >>> logout()

(Login as a bug supervisor to be able to nominate.)

    >>> login_person(nominator)

    >>> current_user = getUtility(ILaunchBag).user
    >>> ubuntu_warty = ubuntu.getSeries("warty")
    >>> firefox_trunk = firefox.getSeries("trunk")

    >>> bug_one.addNomination(current_user, target=ubuntu_warty)
    <BugNomination ...>
    >>> bug_one.addNomination(current_user, target=firefox_trunk)
    <BugNomination ...>

    >>> task_and_nomination_views = (
    ...     bugtasks_and_nominations_view.getBugTaskAndNominationViews())

    >>> print_tasks_and_nominations(task_and_nomination_views)
    bugtask, New, Mozilla Firefox
    nomination, Nominated, Mozilla Firefox 1.0
    nomination, Nominated, Mozilla Firefox trunk
    bugtask, Confirmed, mozilla-firefox (Debian)
    bugtask, New, mozilla-firefox (Ubuntu)
    nomination, Nominated, Ubuntu Hoary
    nomination, Nominated, Ubuntu Warty

Let's add another affected package in Ubuntu to the bug.

    >>> evolution = ubuntu.getSourcePackage("evolution")

    >>> current_user = getUtility(ILaunchBag).user

    >>> bugtaskset.createTask(bug_one, current_user, evolution)
    <BugTask ...>

A nomination row will be included for evolution now too.

    >>> bugtasks_and_nominations_view.initialize()
    >>> task_and_nomination_views = (
    ...     bugtasks_and_nominations_view.getBugTaskAndNominationViews())

    >>> print_tasks_and_nominations(task_and_nomination_views)
    bugtask, New, Mozilla Firefox
    nomination, Nominated, Mozilla Firefox 1.0
    nomination, Nominated, Mozilla Firefox trunk
    bugtask, New, evolution (Ubuntu)
    nomination, Nominated, Ubuntu Hoary
    nomination, Nominated, Ubuntu Warty
    bugtask, Confirmed, mozilla-firefox (Debian)
    bugtask, New, mozilla-firefox (Ubuntu)
    nomination, Nominated, Ubuntu Hoary
    nomination, Nominated, Ubuntu Warty

When a nomination is approved, it turns into a task; the nomination is
no longer shown. Declined nominations continue to be shown.

(First, login as an admin, to ensure we have the privileges to
approve/decline nominations.)

    >>> login("foo.bar@canonical.com")
    >>> current_user = getUtility(ILaunchBag).user

    >>> ubuntu_hoary = ubuntu.getSeries("hoary")
    >>> hoary_nomination = bug_one.getNominationFor(ubuntu_hoary)
    >>> warty_nomination = bug_one.getNominationFor(ubuntu_warty)

    >>> hoary_nomination.approve(current_user)
    >>> warty_nomination.decline(current_user)

    >>> bugtasks_and_nominations_view.initialize()
    >>> task_and_nomination_views = (
    ...     bugtasks_and_nominations_view.getBugTaskAndNominationViews())

    >>> print_tasks_and_nominations(task_and_nomination_views)
    bugtask, New, Mozilla Firefox
    nomination, Nominated, Mozilla Firefox 1.0
    nomination, Nominated, Mozilla Firefox trunk
    bugtask, New, evolution (Ubuntu)
    nomination, Declined, Ubuntu Warty
    bugtask, New, evolution (Ubuntu Hoary)
    bugtask, Confirmed, mozilla-firefox (Debian)
    bugtask, New, mozilla-firefox (Ubuntu)
    nomination, Declined, Ubuntu Warty
    bugtask, New, mozilla-firefox (Ubuntu Hoary)

Bug Edit Page
=============

The bug edit page is used to edit the summary, description,
and bug tags. If the user try to add a tag that hasn't been used in the
current context, we display a confirmation button, which shouldn't be
automatically rendered by the form template. In order to show how it
works, let's override the edit page, making it a bit shorter, and
initialize the test harness.

    >>> from lp.bugs.browser.bug import BugEditView
    >>> class BugEditViewTest(BugEditView):
    ...     def index(self):
    ...         print 'EDIT BUG'

    >>> firefox_task = bug_one.bugtasks[0]
    >>> firefox_task.bugtargetdisplayname
    u'Mozilla Firefox'
    >>> from canonical.launchpad.ftests import LaunchpadFormHarness
    >>> bug_edit = LaunchpadFormHarness(firefox_task, BugEditViewTest)

Initially, the normal edit page is shown, with a single button.

    >>> bug_edit.view.render()
    EDIT BUG
    >>> bug_edit.view.field_names
    ['title', 'description', 'tags']
    >>> [action.label for action in bug_edit.view.actions]
    ['Change']

If we fill in some values and submit the action, the view will redirect
and the bug will have been edited.

    >>> login('test@canonical.com')
    >>> edit_values = {
    ...     'field.title': u'New title',
    ...     'field.description': u'New description.',
    ...     'field.tags': u'doc'}

    >>> bug_edit.submit('change', edit_values)
    >>> bug_edit.hasErrors()
    False
    >>> bug_edit.wasRedirected()
    True

    >>> bug_one.title
    u'New title'
    >>> bug_one.description
    u'New description.'
    >>> bug_one.tags
    [u'doc']


Emails are sent out by adding entries to the bugnotification table. We
need to know how many messages are currently in that table.

    >>> from lp.bugs.model.bugnotification import BugNotification
    >>> bn_set = BugNotification.select(BugNotification.q.bugID == bug_one.id)
    >>> start_bugnotification_count = bn_set.count()

Re-adding the same tag that already exists shouldn't notify anyone.

    >>> edit_values['field.tags'] = u'doc doc'
    >>> bug_edit.submit('change', edit_values)
    >>> bug_edit.hasErrors()
    False
    >>> for message in bug_edit.view.notifications:
    ...     print message

    >>> bug_edit.wasRedirected()
    True
    >>> bn_set = BugNotification.select(BugNotification.q.bugID == bug_one.id)
    >>> bn_set.count() == start_bugnotification_count
    True

The tag 'doc' has already been used for other Firefox bugs, let's try
adding a tag that hasn't already been used. Add 'new-tag' multiple times
so that we can verify that it will only be added once.

    >>> edit_values['field.tags'] = u'new-tag doc new-tag'
    >>> bug_edit.submit('change', edit_values)

Now the form will have a notification, asking us to confirm that we indeed
want to use a new tag. The notification includes a button, with which
the user can confirm that he wants to add the tag.

    >>> bug_edit.hasErrors()
    False
    >>> for message in bug_edit.view.notifications:
    ...     print message
    The tag "new-tag" hasn't been used by Mozilla Firefox before.
    <input ...Create the new tag...>

    >>> bug_edit.wasRedirected()
    False

The form still contains the 'Change' button, though, so that the user
can edit the tag and submit the form again.

    >>> bug_edit.view.render()
    EDIT BUG
    >>> [action.label for action in bug_edit.view.actions]
    ['Change']

If we click on the 'Create the new tag' button, we'll get redirected to
the bug page, and the bug will have the new tag.

    >>> bug_edit.submit('confirm_tag', edit_values)
    >>> bug_edit.hasErrors()
    False
    >>> bug_edit.wasRedirected()
    True

    >>> bug_one.tags
    [u'doc', u'new-tag']

Since the 'new-tag' was added, a new entry in the bugnotification table
should exist.

    >>> bn_set = BugNotification.select(BugNotification.q.bugID == bug_one.id,
    ...                                 orderBy = BugNotification.q.id)
    >>> start_bugnotification_count == bn_set.count() - 1
    True
    >>> bn_set[-1].message.text_contents
    u'** Tags added: new-tag'


Displaying BugActivity interleaved with comments
------------------------------------------------

BugTaskView offers a means for us to get a list of comments and activity
for a bug, ordered by date.

First, some set-up.

    >>> import pytz
    >>> from datetime import datetime
    >>> from lp.bugs.adapters.bugchange import BugTitleChange
    >>> nowish = datetime(
    ...     2009, 3, 26, 21, 37, 45, tzinfo=pytz.timezone('UTC'))

    >>> login('foo.bar@canonical.com')
    >>> product = factory.makeProduct(name='testproduct')
    >>> bug = factory.makeBug(title="A bug title", product=product)
    >>> title_change = BugTitleChange(
    ...     when=nowish, person=foo_bar, what_changed='title',
    ...     old_value=bug.title, new_value="A new bug title")
    >>> bug.addChange(title_change)

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={'field.subject': bug.title,
    ...           'field.comment': "A comment, for the reading of.",
    ...           'field.actions.save': 'Save Changes'})
    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+addcomment-form")
    >>> view.initialize()

    >>> flush_database_updates()
    >>> transaction.commit()

    >>> request = LaunchpadTestRequest(method="GET")
    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+index")

The activity_and_comments property of BugTaskView is a list of comments
and activity on a bug, ordered by the date that they occurred. Each item
is encapsulated in a dict, in the form: {'comment': <BugComment>} or
{'activity': [<BugActivityItem>...]}. Each dict also contains a 'date'
item, which is used to sort the list.

If we iterate over the list of activity_and_comments we can examine, in
order, the comments and activity that have taken place on a bug.

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={
    ...         'testproduct.status': 'Confirmed',
    ...         'testproduct.actions.save': 'Save Changes',
    ...         })
    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+editstatus")
    >>> view.initialize()

    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+index")
    >>> view.initialize()

    >>> def print_activities(activities):
    ...     for activity in activities:
    ...         target_name = activity['target']
    ...         if target_name is None:
    ...             print "Changed:"
    ...         else:
    ...             print "Changed in %s:" % target_name
    ...         activity_items = activity['activity']
    ...         for activity_item in activity_items:
    ...             print "* %s: %s => %s" % (
    ...                 activity_item.change_summary,
    ...                 activity_item.oldvalue,
    ...                 activity_item.newvalue)

    >>> def print_comment(comment):
    ...     print comment.text_for_display
    ...     print_activities(comment.activity)

    >>> def print_activity_and_comments(activity_and_comments):
    ...     for activity_or_comment in activity_and_comments:
    ...         print "-- {person.name} --".format(**activity_or_comment)
    ...         if 'activity' in activity_or_comment:
    ...             print_activities(activity_or_comment["activity"])
    ...         if 'comment' in activity_or_comment:
    ...             print_comment(activity_or_comment["comment"])

    >>> print_activity_and_comments(view.activity_and_comments)
    -- name16 --
    Changed:
    * summary: A bug title => A new bug title
    -- name16 --
    A comment, for the reading of.
    Changed in testproduct:
    * status: New => Confirmed

If a comment and a BugActivity item occur at the same time, the activity
item will be returned in the comment's activity property rather than as
an activity item in its own right. This allows us to group coincidental
comments and activity together.

    >>> request = LaunchpadTestRequest(
    ...     method="POST",
    ...     form={
    ...         'testproduct.status': 'Confirmed',
    ...         'testproduct.importance': 'High',
    ...         'testproduct.comment_on_change': "I triaged it.",
    ...         'testproduct.actions.save': 'Save Changes',
    ...         })
    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+editstatus")
    >>> view.initialize()

    >>> view = getMultiAdapter(
    ...     (bug.bugtasks[0], request), name="+index")
    >>> view.initialize()

Looking at activity_and_comments will give us the same results as
before, plus the new comment, since the changes we just made were
grouped with that comment.

    >>> print_activity_and_comments(view.activity_and_comments)
    -- name16 --
    Changed:
    * summary: A bug title => A new bug title
    -- name16 --
    A comment, for the reading of.
    -- name16 --
    I triaged it.
    Changed in testproduct:
    * importance: Undecided => High
    * status: New => Confirmed


Getting the list of possible duplicates for a new bug
-----------------------------------------------------

It's possible to get a list of the possible duplicates for a new bug by
using the +filebug-show-similar view of a BugTarget.

The +filebug-show-similar view takes a single parameter, 'title'. It
uses this to search for similar bugs.

    >>> request = LaunchpadTestRequest(
    ...     method="GET",
    ...     form={'title': 'a'})
    >>> view = getMultiAdapter(
    ...     (firefox, request), name="+filebug-show-similar")
    >>> view.initialize()

The view offers a list of bugs similar to the one whose title we just
searched for.

    >>> for bug in view.similar_bugs:
    ...     print "Bug %s: %s" % (bug.id, bug.title)
    Bug 1:  New title
    Bug 4:  Reflow problems with complex page layouts
    Bug 5:  Firefox install instructions should be complete
    Bug 17: a firefox bug

If we refine the search critera, we'll get different results.

    >>> request = LaunchpadTestRequest(
    ...     method="GET",
    ...     form={'title': 'problems'})
    >>> view = getMultiAdapter(
    ...     (firefox, request), name="+filebug-show-similar")
    >>> view.initialize()
    >>> for bug in view.similar_bugs:
    ...     print "Bug %s: %s" % (bug.id, bug.title)
    Bug 4:  Reflow problems with complex page layouts