~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/browser/bugsubscription.py

Merge db-devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
from lp.bugs.browser.structuralsubscription import (
44
44
    expose_structural_subscription_data_to_js,
45
45
    )
46
 
from lp.bugs.enum import BugNotificationLevel, HIDDEN_BUG_NOTIFICATION_LEVELS
 
46
from lp.bugs.enum import BugNotificationLevel
47
47
from lp.bugs.interfaces.bugsubscription import IBugSubscription
48
48
from lp.bugs.model.personsubscriptioninfo import PersonSubscriptions
49
49
from lp.bugs.model.structuralsubscription import (
121
121
            SimpleTerm(
122
122
                level, level.title,
123
123
                self._bug_notification_level_descriptions[level])
124
 
            # We reorder the items so that COMMENTS comes first. We also
125
 
            # drop the NOTHING option since it just makes the UI
126
 
            # confusing.
127
 
            for level in sorted(BugNotificationLevel.items, reverse=True)
128
 
                if level not in HIDDEN_BUG_NOTIFICATION_LEVELS]
 
124
            # We reorder the items so that COMMENTS comes first.
 
125
            for level in sorted(BugNotificationLevel.items, reverse=True)]
129
126
        bug_notification_vocabulary = SimpleVocabulary(
130
127
            bug_notification_level_terms)
131
128
 
132
 
        if (self.current_user_subscription is not None and
133
 
            self.current_user_subscription.bug_notification_level not in
134
 
                HIDDEN_BUG_NOTIFICATION_LEVELS):
 
129
        if self.current_user_subscription is not None:
135
130
            default_value = (
136
131
                self.current_user_subscription.bug_notification_level)
137
132
        else:
231
226
 
232
227
    @cachedproperty
233
228
    def _update_subscription_term(self):
234
 
        if self.user_is_muted:
235
 
            label = "unmute bug mail from this bug and subscribe me to it"
236
 
        else:
237
 
            label = "update my current subscription"
 
229
        label = "update my current subscription"
238
230
        return SimpleTerm(
239
231
            'update-subscription', 'update-subscription', label)
240
232
 
247
239
        return SimpleTerm(self.user, self.user.name, label)
248
240
 
249
241
    @cachedproperty
 
242
    def _unmute_user_term(self):
 
243
        if self.user_is_subscribed_directly:
 
244
            return SimpleTerm(
 
245
                'update-subscription', 'update-subscription',
 
246
                "unmute bug mail from this bug and restore my subscription")
 
247
        else:
 
248
            return SimpleTerm(self.user, self.user.name,
 
249
                              "unmute bug mail from this bug")
 
250
 
 
251
    @cachedproperty
250
252
    def _subscription_field(self):
251
253
        subscription_terms = []
252
254
        self_subscribed = False
 
255
        is_really_muted = self._use_advanced_features and self.user_is_muted
 
256
        if is_really_muted:
 
257
            subscription_terms.insert(0, self._unmute_user_term)
253
258
        for person in self._subscribers_for_current_user:
254
259
            if person.id == self.user.id:
255
 
                if (self._use_advanced_features and
256
 
                    (self.user_is_subscribed_directly or
257
 
                    self.user_is_muted)):
 
260
                if is_really_muted:
 
261
                    # We've already added the unmute option.
 
262
                    continue
 
263
                else:
 
264
                    if (self._use_advanced_features and
 
265
                        self.user_is_subscribed_directly):
258
266
                        subscription_terms.append(
259
267
                            self._update_subscription_term)
260
 
                subscription_terms.insert(
261
 
                    0, self._unsubscribe_current_user_term)
262
 
                self_subscribed = True
 
268
                    subscription_terms.insert(
 
269
                        0, self._unsubscribe_current_user_term)
 
270
                    self_subscribed = True
263
271
            else:
264
272
                subscription_terms.append(
265
273
                    SimpleTerm(
267
275
                        'unsubscribe <a href="%s">%s</a> from this bug' % (
268
276
                            canonical_url(person),
269
277
                            cgi.escape(person.displayname))))
270
 
        if not self_subscribed:
 
278
        if not self_subscribed and not is_really_muted:
271
279
            subscription_terms.insert(0,
272
280
                SimpleTerm(
273
281
                    self.user, self.user.name, 'subscribe me to this bug'))
322
330
                # subscribe theirself or unsubscribe their team.
323
331
                self.widgets['subscription'].visible = True
324
332
 
325
 
            if (self.user_is_subscribed and
326
 
                self.user_is_subscribed_to_dupes_only):
 
333
            if (self.user_is_subscribed_to_dupes_only or
 
334
                (self._use_advanced_features and self.user_is_muted and
 
335
                 not self.user_is_subscribed)):
327
336
                # If the user is subscribed via a duplicate but is not
328
337
                # directly subscribed, we hide the
329
338
                # bug_notification_level field, since it's not used.
336
345
    @cachedproperty
337
346
    def user_is_subscribed_directly(self):
338
347
        """Is the user subscribed directly to this bug?"""
339
 
        return (
340
 
            self.context.bug.isSubscribed(self.user) and not
341
 
            self.user_is_muted)
 
348
        return self.context.bug.isSubscribed(self.user)
342
349
 
343
350
    @cachedproperty
344
351
    def user_is_subscribed_to_dupes(self):
345
352
        """Is the user subscribed to dupes of this bug?"""
346
 
        return (
347
 
            self.context.bug.isSubscribedToDupes(self.user) and not
348
 
            self.user_is_muted)
 
353
        return self.context.bug.isSubscribedToDupes(self.user)
349
354
 
350
355
    @property
351
356
    def user_is_subscribed(self):
388
393
 
389
394
        if (subscription_person == self._update_subscription_term.value and
390
395
            (self.user_is_subscribed or self.user_is_muted)):
391
 
            self._handleUpdateSubscription(level=bug_notification_level)
 
396
            if self.user_is_muted:
 
397
                self._handleUnmute()
 
398
            if self.user_is_subscribed:
 
399
                self._handleUpdateSubscription(level=bug_notification_level)
 
400
            else:
 
401
                self._handleSubscribe(level=bug_notification_level)
392
402
        elif self.user_is_muted and subscription_person == self.user:
393
 
            self._handleUnsubscribeCurrentUser()
 
403
            self._handleUnmute()
394
404
        elif (not self.user_is_subscribed and
395
405
            (subscription_person == self.user)):
396
406
            self._handleSubscribe(bug_notification_level)
411
421
        else:
412
422
            self._handleUnsubscribeOtherUser(user)
413
423
 
 
424
    def _handleUnmute(self):
 
425
        """Handle an unmute request."""
 
426
        self.context.bug.unmute(self.user, self.user)
 
427
 
414
428
    def _handleUnsubscribeCurrentUser(self):
415
429
        """Handle the special cases for unsubscribing the current user.
416
430
 
539
553
        for subscription in direct_subscriptions:
540
554
            if not check_permission('launchpad.View', subscription.person):
541
555
                continue
542
 
            if (subscription.bug_notification_level ==
543
 
                BugNotificationLevel.NOTHING):
544
 
                continue
545
556
            if subscription.person == self.user:
546
557
                can_unsubscribe = [subscription] + can_unsubscribe
547
558
            elif subscription.canBeUnsubscribedByUser(self.user):
621
632
 
622
633
    @property
623
634
    def label(self):
624
 
        return "Mute bug mail for bug %s" % self.context.bug.id
 
635
        if self.context.bug.isMuted(self.user):
 
636
            return "Unmute bug mail for bug %s" % self.context.bug.id
 
637
        else:
 
638
            return "Mute bug mail for bug %s" % self.context.bug.id
625
639
 
626
640
    page_title = label
627
641
 
632
646
    cancel_url = next_url
633
647
 
634
648
    def initialize(self):
 
649
        self.is_muted = self.context.bug.isMuted(self.user)
635
650
        super(BugMuteSelfView, self).initialize()
636
 
        # If the user is already muted, redirect them to the +subscribe
637
 
        # page, since there's no point doing its work twice.
638
 
        if self.context.bug.isMuted(self.user):
639
 
            self.request.response.redirect(
640
 
                canonical_url(self.context, view_name="+subscribe"))
641
651
 
642
 
    @action('Mute bug mail', name='mute')
 
652
    @action('Mute bug mail',
 
653
            name='mute',
 
654
            condition=lambda form, action: not form.is_muted)
643
655
    def mute_action(self, action, data):
644
656
        self.context.bug.mute(self.user, self.user)
645
657
        self.request.response.addInfoNotification(
646
658
            "Mail for bug #%s has been muted." % self.context.bug.id)
 
659
 
 
660
    @action('Unmute bug mail',
 
661
            name='unmute',
 
662
            condition=lambda form, action: form.is_muted)
 
663
    def unmute_action(self, action, data):
 
664
        self.context.bug.unmute(self.user, self.user)
 
665
        self.request.response.addInfoNotification(
 
666
            "Mail for bug #%s has been unmuted." % self.context.bug.id)