~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
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Views for BugSubscription."""

__metaclass__ = type
__all__ = [
    'AdvancedSubscriptionMixin',
    'BugMuteSelfView',
    'BugPortletSubscribersWithDetails',
    'BugSubscriptionAddView',
    'BugSubscriptionListView',
    ]

import cgi

from lazr.delegates import delegates
from lazr.restful.interfaces import (
    IJSONRequestCache,
    IWebServiceClientRequest,
    )
from simplejson import dumps
from zope import formlib
from zope.app.form import CustomWidgetFactory
from zope.app.form.browser.itemswidgets import RadioWidget
from zope.schema import Choice
from zope.schema.vocabulary import (
    SimpleTerm,
    SimpleVocabulary,
    )
from zope.security.proxy import removeSecurityProxy
from zope.traversing.browser import absoluteURL

from lp import _
from lp.app.browser.launchpadform import (
    action,
    LaunchpadFormView,
    )
from lp.bugs.browser.structuralsubscription import (
    expose_structural_subscription_data_to_js,
    )
from lp.bugs.enum import BugNotificationLevel
from lp.bugs.errors import SubscriptionPrivacyViolation
from lp.bugs.interfaces.bug import IBug
from lp.bugs.interfaces.bugsubscription import IBugSubscription
from lp.bugs.model.personsubscriptioninfo import PersonSubscriptions
from lp.bugs.model.structuralsubscription import (
    get_structural_subscriptions_for_bug,
    )
from lp.services.propertycache import cachedproperty
from lp.services.webapp import (
    canonical_url,
    LaunchpadView,
    )
from lp.services.webapp.authorization import (
    check_permission,
    precache_permission_for_objects,
    )
from lp.services.webapp.launchpadform import ReturnToReferrerMixin
from lp.services.webapp.menu import structured


class BugSubscriptionAddView(LaunchpadFormView):
    """Browser view class for subscribing someone else to a bug."""

    schema = IBugSubscription

    field_names = ['person']

    def setUpFields(self):
        """Set up 'person' as an input field."""
        super(BugSubscriptionAddView, self).setUpFields()
        self.form_fields['person'].for_input = True

    @action('Subscribe user', name='add')
    def add_action(self, action, data):
        person = data['person']
        try:
            self.context.bug.subscribe(
                person, self.user, suppress_notify=False)
        except SubscriptionPrivacyViolation as error:
            self.setFieldError('person', unicode(error))
        else:
            if person.is_team:
                message = '%s team has been subscribed to this bug.'
            else:
                message = '%s has been subscribed to this bug.'
            self.request.response.addInfoNotification(
                message % person.displayname)

    @property
    def next_url(self):
        return canonical_url(self.context)

    cancel_url = next_url

    @property
    def label(self):
        return 'Subscribe someone else to bug #%i' % self.context.bug.id

    page_title = label


class AdvancedSubscriptionMixin:
    """A mixin of advanced subscription code for views.

    In order to use this mixin in a view the view must:
     - Define a current_user_subscription property which returns the
       current BugSubscription or StructuralSubscription for request.user.
       If there's no subscription for the given user in the given
       context, current_user_subscription must return None.
     - Define a dict, _bug_notification_level_descriptions, which maps
       BugNotificationLevel values to string descriptions for the
       current context (see `BugSubscriptionSubscribeSelfView` for an
       example).
     - Update the view's setUpFields() to call
       _setUpBugNotificationLevelField().
    """

    @cachedproperty
    def _bug_notification_level_field(self):
        """Return a custom form field for bug_notification_level."""
        # We rebuild the items that we show in the field so that the
        # labels shown are human readable and specific to the +subscribe
        # form. The BugNotificationLevel descriptions are too generic.
        bug_notification_level_terms = [
            SimpleTerm(
                level, level.title,
                self._bug_notification_level_descriptions[level])
            # We reorder the items so that COMMENTS comes first.
            for level in sorted(BugNotificationLevel.items, reverse=True)]
        bug_notification_vocabulary = SimpleVocabulary(
            bug_notification_level_terms)

        if self.current_user_subscription is not None:
            default_value = (
                self.current_user_subscription.bug_notification_level)
        else:
            default_value = BugNotificationLevel.COMMENTS

        bug_notification_level_field = Choice(
            __name__='bug_notification_level', title=_("Tell me when"),
            vocabulary=bug_notification_vocabulary, required=True,
            default=default_value)
        return bug_notification_level_field

    def _setUpBugNotificationLevelField(self):
        """Set up the bug_notification_level field."""
        self.form_fields = self.form_fields.omit('bug_notification_level')
        self.form_fields += formlib.form.Fields(
            self._bug_notification_level_field)
        self.form_fields['bug_notification_level'].custom_widget = (
            CustomWidgetFactory(RadioWidget))


class BugSubscriptionSubscribeSelfView(LaunchpadFormView,
                                       ReturnToReferrerMixin,
                                       AdvancedSubscriptionMixin):
    """A view to handle the +subscribe page for a bug."""

    schema = IBugSubscription
    page_title = 'Subscription options'

    # A mapping of BugNotificationLevel values to descriptions to be
    # shown on the +subscribe page.
    _bug_notification_level_descriptions = {
        BugNotificationLevel.COMMENTS: (
            "a change is made to this bug or a new comment is added, "),
        BugNotificationLevel.METADATA: (
            "any change is made to this bug, other than a new comment "
            "being added, or"),
        BugNotificationLevel.LIFECYCLE: (
            "this bug is fixed or re-opened."),
        }

    @property
    def field_names(self):
        return ['bug_notification_level']

    @property
    def next_url(self):
        """Provided so returning to the page they came from works."""
        referer = self._return_url
        context_url = canonical_url(self.context)

        # XXX bdmurray 2010-09-30 bug=98437: work around zope's test
        # browser setting referer to localhost.
        # We also ignore the current request URL and the context URL as
        # far as referrers are concerned so that we can handle privacy
        # issues properly.
        ignored_referrer_urls = (
            'localhost', self.request.getURL(), context_url)
        if referer and referer not in ignored_referrer_urls:
            next_url = referer
        elif self._redirecting_to_bug_list:
            next_url = canonical_url(self.context.target, view_name="+bugs")
        else:
            next_url = context_url
        return next_url

    cancel_url = next_url

    @cachedproperty
    def _subscribers_for_current_user(self):
        """Return a dict of the subscribers for the current user."""
        persons_for_user = {}
        person_count = 0
        bug = self.context.bug
        for person in bug.getSubscribersForPerson(self.user):
            if person.id not in persons_for_user:
                persons_for_user[person.id] = person
                person_count += 1

        self._subscriber_count_for_current_user = person_count
        return persons_for_user.values()

    def initialize(self):
        """See `LaunchpadFormView`."""
        self._subscriber_count_for_current_user = 0
        self._redirecting_to_bug_list = False
        super(BugSubscriptionSubscribeSelfView, self).initialize()

    @cachedproperty
    def current_user_subscription(self):
        return self.context.bug.getSubscriptionForPerson(self.user)

    @cachedproperty
    def _update_subscription_term(self):
        label = "update my current subscription"
        return SimpleTerm(
            'update-subscription', 'update-subscription', label)

    @cachedproperty
    def _unsubscribe_current_user_term(self):
        if self.user_is_muted:
            label = "unmute bug mail from this bug"
        else:
            label = 'unsubscribe me from this bug'
        return SimpleTerm(self.user, self.user.name, label)

    @cachedproperty
    def _unmute_user_term(self):
        if self.user_is_subscribed_directly:
            return SimpleTerm(
                'update-subscription', 'update-subscription',
                "unmute bug mail from this bug and restore my subscription")
        else:
            return SimpleTerm(self.user, self.user.name,
                              "unmute bug mail from this bug")

    @cachedproperty
    def _subscription_field(self):
        subscription_terms = []
        self_subscribed = False
        is_really_muted = self.user_is_muted
        if is_really_muted:
            subscription_terms.insert(0, self._unmute_user_term)
        for person in self._subscribers_for_current_user:
            if person.id == self.user.id:
                if is_really_muted:
                    # We've already added the unmute option.
                    continue
                else:
                    if self.user_is_subscribed_directly:
                        subscription_terms.append(
                            self._update_subscription_term)
                    subscription_terms.insert(
                        0, self._unsubscribe_current_user_term)
                    self_subscribed = True
            else:
                subscription_terms.append(
                    SimpleTerm(
                        person, person.name,
                        'unsubscribe <a href="%s">%s</a> from this bug' % (
                            canonical_url(person),
                            cgi.escape(person.displayname))))
        if not self_subscribed:
            if not is_really_muted:
                subscription_terms.insert(0,
                    SimpleTerm(
                        self.user, self.user.name,
                        'subscribe me to this bug'))
            elif not self.user_is_subscribed_directly:
                subscription_terms.insert(0,
                    SimpleTerm(
                        'update-subscription', 'update-subscription',
                        'unmute bug mail from this bug and subscribe me to '
                        'this bug'))

        # Add punctuation to the list of terms.
        if len(subscription_terms) > 1:
            for term in subscription_terms[:-1]:
                term.title += ','
            subscription_terms[-2].title += ' or'
            subscription_terms[-1].title += '.'

        subscription_vocabulary = SimpleVocabulary(subscription_terms)
        if self.user_is_subscribed_directly or self.user_is_muted:
            default_subscription_value = self._update_subscription_term.value
        else:
            default_subscription_value = (
                subscription_vocabulary.getTermByToken(self.user.name).value)

        subscription_field = Choice(
            __name__='subscription', title=_("Subscription options"),
            vocabulary=subscription_vocabulary, required=True,
            default=default_subscription_value)
        return subscription_field

    def setUpFields(self):
        """See `LaunchpadFormView`."""
        super(BugSubscriptionSubscribeSelfView, self).setUpFields()
        if self.user is None:
            return

        self.form_fields += formlib.form.Fields(self._subscription_field)
        self._setUpBugNotificationLevelField()
        self.form_fields['subscription'].custom_widget = CustomWidgetFactory(
            RadioWidget)

    def setUpWidgets(self):
        """See `LaunchpadFormView`."""
        super(BugSubscriptionSubscribeSelfView, self).setUpWidgets()
        self.widgets['subscription'].widget_class = 'bug-subscription-basic'
        self.widgets['bug_notification_level'].widget_class = (
            'bug-notification-level-field')
        if (len(self.form_fields['subscription'].field.vocabulary) == 1):
            # We hide the subscription widget if the user isn't
            # subscribed, since we know who the subscriber is and we
            # don't need to present them with a single radio button.
            self.widgets['subscription'].visible = False
        else:
            # We show the subscription widget when the user is
            # subscribed via a team, because they can either
            # subscribe theirself or unsubscribe their team.
            self.widgets['subscription'].visible = True

        if self.user_is_subscribed_to_dupes_only:
            # If the user is subscribed via a duplicate but is not
            # directly subscribed, we hide the
            # bug_notification_level field, since it's not used.
            self.widgets['bug_notification_level'].visible = False

    @cachedproperty
    def user_is_muted(self):
        return self.context.bug.isMuted(self.user)

    @cachedproperty
    def user_is_subscribed_directly(self):
        """Is the user subscribed directly to this bug?"""
        return self.context.bug.isSubscribed(self.user)

    @cachedproperty
    def user_is_subscribed_to_dupes(self):
        """Is the user subscribed to dupes of this bug?"""
        return self.context.bug.isSubscribedToDupes(self.user)

    @property
    def user_is_subscribed(self):
        """Is the user subscribed to this bug?"""
        return (
            self.user_is_subscribed_directly or
            self.user_is_subscribed_to_dupes)

    @property
    def user_is_subscribed_to_dupes_only(self):
        """Is the user subscribed to this bug only via a dupe?"""
        return (
            self.user_is_subscribed_to_dupes and
            not self.user_is_subscribed_directly)

    def shouldShowUnsubscribeFromDupesWarning(self):
        """Should we warn the user about unsubscribing and duplicates?

        The warning should tell the user that, when unsubscribing, they
        will also be unsubscribed from dupes of this bug.
        """
        if self.user_is_subscribed:
            return True

        bug = self.context.bug
        for team in self.user.teams_participated_in:
            if bug.isSubscribed(team) or bug.isSubscribedToDupes(team):
                return True

        return False

    @action('Continue', name='continue')
    def subscribe_action(self, action, data):
        """Handle subscription requests."""
        subscription_person = self.widgets['subscription'].getInputValue()
        bug_notification_level = data.get('bug_notification_level', None)

        if (subscription_person == self._update_subscription_term.value and
            (self.user_is_subscribed or self.user_is_muted)):
            if self.user_is_muted:
                self._handleUnmute()
            if self.user_is_subscribed:
                self._handleUpdateSubscription(level=bug_notification_level)
            else:
                self._handleSubscribe(level=bug_notification_level)
        elif self.user_is_muted and subscription_person == self.user:
            self._handleUnmute()
        elif (not self.user_is_subscribed and
            (subscription_person == self.user)):
            self._handleSubscribe(bug_notification_level)
        else:
            self._handleUnsubscribe(subscription_person)
        self.request.response.redirect(self.next_url)

    def _handleSubscribe(self, level=None):
        """Handle a subscribe request."""
        self.context.bug.subscribe(self.user, self.user, level=level)
        self.request.response.addNotification(
            "You have subscribed to this bug report.")

    def _handleUnsubscribe(self, user):
        """Handle an unsubscribe request."""
        if user == self.user:
            self._handleUnsubscribeCurrentUser()
        else:
            self._handleUnsubscribeOtherUser(user)

    def _handleUnmute(self):
        """Handle an unmute request."""
        self.context.bug.unmute(self.user, self.user)

    def _handleUnsubscribeCurrentUser(self):
        """Handle the special cases for unsubscribing the current user."""
        # We call unsubscribeFromDupes() before unsubscribe(), because
        # if the bug is private, the current user will be prevented from
        # calling methods on the main bug after they unsubscribe from it.
        unsubed_dupes = self.context.bug.unsubscribeFromDupes(
            self.user, self.user)
        self.context.bug.unsubscribe(self.user, self.user)

        self.request.response.addNotification(
            structured(
                self._getUnsubscribeNotification(self.user, unsubed_dupes)))

        # Because the unsubscribe above may change what the security policy
        # says about the bug, we need to clear its cache.
        self.request.clearSecurityPolicyCache()

        if not check_permission("launchpad.View", self.context.bug):
            # Redirect the user to the bug listing, because they can no
            # longer see a private bug from which they've unsubscribed.
            self._redirecting_to_bug_list = True

    def _handleUnsubscribeOtherUser(self, user):
        """Handle unsubscribing someone other than the current user."""
        assert user != self.user, (
            "Expected a user other than the currently logged-in user.")

        # We'll also unsubscribe the other user from dupes of this bug,
        # otherwise they'll keep getting this bug's mail.
        self.context.bug.unsubscribe(user, self.user)
        unsubed_dupes = self.context.bug.unsubscribeFromDupes(user, user)
        self.request.response.addNotification(
            structured(
                self._getUnsubscribeNotification(user, unsubed_dupes)))

    def _handleUpdateSubscription(self, level):
        """Handle updating a user's subscription."""
        subscription = self.current_user_subscription
        subscription.bug_notification_level = level
        self.request.response.addNotification(
            "Your bug report subscription has been updated.")

    def _getUnsubscribeNotification(self, user, unsubed_dupes):
        """Construct and return the unsubscribe-from-bug feedback message.

        :user: The IPerson or ITeam that was unsubscribed from the bug.
        :unsubed_dupes: The list of IBugs that are dupes from which the
                        user was unsubscribed.
        """
        current_bug = self.context.bug
        current_user = self.user
        unsubed_dupes_msg_fragment = self._getUnsubscribedDupesMsgFragment(
            unsubed_dupes)

        if user == current_user:
            # Consider that the current user may have been "locked out"
            # of a bug if they unsubscribed themselves from a private
            # bug!
            if check_permission("launchpad.View", current_bug):
                # The user still has permission to see this bug, so no
                # special-casing needed.
                return (
                    "You have been unsubscribed from bug %d%s." % (
                    current_bug.id, unsubed_dupes_msg_fragment))
            else:
                return (
                    "You have been unsubscribed from bug %d%s. You no "
                    "longer have access to this private bug.") % (
                        current_bug.id, unsubed_dupes_msg_fragment)
        else:
            return "%s has been unsubscribed from bug %d%s." % (
                cgi.escape(user.displayname), current_bug.id,
                unsubed_dupes_msg_fragment)

    def _getUnsubscribedDupesMsgFragment(self, unsubed_dupes):
        """Return the duplicates fragment of the unsubscription notification.

        This piece lists the duplicates from which the user was
        unsubscribed.
        """
        if not unsubed_dupes:
            return ""

        dupe_links = []
        for unsubed_dupe in unsubed_dupes:
            dupe_links.append(
                '<a href="%s" title="%s">#%d</a>' % (
                canonical_url(unsubed_dupe), unsubed_dupe.title,
                unsubed_dupe.id))
        dupe_links_string = ", ".join(dupe_links)

        num_dupes = len(unsubed_dupes)
        if num_dupes > 1:
            plural_suffix = "s"
        else:
            plural_suffix = ""

        return (
            " and %(num_dupes)d duplicate%(plural_suffix)s "
            "(%(dupe_links_string)s)") % ({
                'num_dupes': num_dupes,
                'plural_suffix': plural_suffix,
                'dupe_links_string': dupe_links_string})


class BugPortletSubscribersWithDetails(LaunchpadView):
    """A view that returns a JSON dump of the subscriber details for a bug."""

    @cachedproperty
    def api_request(self):
        return IWebServiceClientRequest(self.request)

    def direct_subscriber_data(self, bug):
        """Get the direct subscriber data.

        This method is isolated from the subscriber_data_js so that query
        count testing can be done accurately and robustly.
        """
        data = []
        details = list(bug.getDirectSubscribersWithDetails())
        for person, subscribed_by, subscription in details:
            can_edit = subscription.canBeUnsubscribedByUser(self.user)
            if person == self.user:
                # Skip the current user viewing the page.
                continue
            if self.user is None and person.private:
                # Do not include private teams if there's no logged in user.
                continue

            # If we have made it to here then the logged in user can see the
            # bug, hence they can see any subscribers.
            # The security adaptor will do the job also but we don't want or
            # need the expense of running several complex SQL queries.
            precache_permission_for_objects(
                        self.request, 'launchpad.LimitedView', [person])
            subscriber = {
                'name': person.name,
                'display_name': person.displayname,
                'web_link': canonical_url(person, rootsite='mainsite'),
                'self_link': absoluteURL(person, self.api_request),
                'is_team': person.is_team,
                'can_edit': can_edit,
                'display_subscribed_by': subscription.display_subscribed_by,
                }
            record = {
                'subscriber': subscriber,
                'subscription_level': str(
                    removeSecurityProxy(subscription.bug_notification_level)),
                }
            data.append(record)
        return data

    @property
    def subscriber_data(self):
        """Return subscriber_ids in a form suitable for JavaScript use."""
        bug = IBug(self.context)
        data = self.direct_subscriber_data(bug)

        others = list(bug.getIndirectSubscribers())
        # If we have made it to here then the logged in user can see the
        # bug, hence they can see any indirect subscribers.
        include_private = self.user is not None
        if include_private:
            precache_permission_for_objects(
                self.request, 'launchpad.LimitedView', others)
        for person in others:
            if person == self.user:
                # Skip the current user viewing the page,
                continue
            if not include_private and person.private:
                # Do not include private teams if there's no logged in user.
                continue
            subscriber = {
                'name': person.name,
                'display_name': person.displayname,
                'web_link': canonical_url(person, rootsite='mainsite'),
                'self_link': absoluteURL(person, self.api_request),
                'is_team': person.is_team,
                'can_edit': False,
                }
            record = {
                'subscriber': subscriber,
                'subscription_level': 'Maybe',
                }
            data.append(record)
        return data

    @property
    def subscriber_data_js(self):
        return dumps(self.subscriber_data)

    def render(self):
        """Override the default render() to return only JSON."""
        self.request.response.setHeader('content-type', 'application/json')
        return self.subscriber_data_js


class SubscriptionAttrDecorator:
    """A BugSubscription with added attributes for HTML/JS."""
    delegates(IBugSubscription, 'subscription')

    def __init__(self, subscription):
        self.subscription = subscription

    @property
    def css_name(self):
        return 'subscriber-%s' % self.subscription.person.id


class BugSubscriptionListView(LaunchpadView):
    """A view to show all a person's subscriptions to a bug."""

    def initialize(self):
        super(BugSubscriptionListView, self).initialize()
        subscriptions = list(get_structural_subscriptions_for_bug(
            self.context.bug, self.user))
        expose_structural_subscription_data_to_js(
            self.context, self.request, self.user, subscriptions)
        subscriptions_info = PersonSubscriptions(
                self.user, self.context.bug)
        subdata, references = subscriptions_info.getDataForClient()
        cache = IJSONRequestCache(self.request).objects
        cache.update(references)
        cache['bug_subscription_info'] = subdata
        cache['bug_is_private'] = self.context.bug.private

    @property
    def label(self):
        return "Your subscriptions to bug %d" % self.context.bug.id

    page_title = label


class BugMuteSelfView(LaunchpadFormView):
    """A view to mute a user's bug mail for a given bug."""

    schema = IBugSubscription
    field_names = []

    @property
    def label(self):
        if self.context.bug.isMuted(self.user):
            return "Unmute bug mail for bug %s" % self.context.bug.id
        else:
            return "Mute bug mail for bug %s" % self.context.bug.id

    page_title = label

    @property
    def next_url(self):
        return canonical_url(self.context)

    cancel_url = next_url

    def initialize(self):
        self.is_muted = self.context.bug.isMuted(self.user)
        super(BugMuteSelfView, self).initialize()

    @action('Mute bug mail',
            name='mute',
            condition=lambda form, action: not form.is_muted)
    def mute_action(self, action, data):
        self.context.bug.mute(self.user, self.user)
        self.request.response.addInfoNotification(
            "Mail for bug #%s has been muted." % self.context.bug.id)

    @action('Unmute bug mail',
            name='unmute',
            condition=lambda form, action: form.is_muted)
    def unmute_action(self, action, data):
        self.context.bug.unmute(self.user, self.user)
        self.request.response.addInfoNotification(
            "Mail for bug #%s has been unmuted." % self.context.bug.id)