~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-05-23 18:43:31 UTC
  • mfrom: (13084.2.6 page-match-rewrite-url)
  • Revision ID: launchpad@pqm.canonical.com-20110523184331-dhd2c7cgfuu49epw
[r=sinzui][bug=784273] Adds facility to the PageMatch to handle bad
        URIs

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
 
46
from lp.bugs.enum import BugNotificationLevel, HIDDEN_BUG_NOTIFICATION_LEVELS
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.
125
 
            for level in sorted(BugNotificationLevel.items, reverse=True)]
 
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]
126
129
        bug_notification_vocabulary = SimpleVocabulary(
127
130
            bug_notification_level_terms)
128
131
 
129
 
        if self.current_user_subscription is not None:
 
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):
130
135
            default_value = (
131
136
                self.current_user_subscription.bug_notification_level)
132
137
        else:
226
231
 
227
232
    @cachedproperty
228
233
    def _update_subscription_term(self):
229
 
        label = "update my current subscription"
 
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"
230
238
        return SimpleTerm(
231
239
            'update-subscription', 'update-subscription', label)
232
240
 
239
247
        return SimpleTerm(self.user, self.user.name, label)
240
248
 
241
249
    @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
252
250
    def _subscription_field(self):
253
251
        subscription_terms = []
254
252
        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)
258
253
        for person in self._subscribers_for_current_user:
259
254
            if person.id == self.user.id:
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):
 
255
                if (self._use_advanced_features and
 
256
                    (self.user_is_subscribed_directly or
 
257
                    self.user_is_muted)):
266
258
                        subscription_terms.append(
267
259
                            self._update_subscription_term)
268
 
                    subscription_terms.insert(
269
 
                        0, self._unsubscribe_current_user_term)
270
 
                    self_subscribed = True
 
260
                subscription_terms.insert(
 
261
                    0, self._unsubscribe_current_user_term)
 
262
                self_subscribed = True
271
263
            else:
272
264
                subscription_terms.append(
273
265
                    SimpleTerm(
276
268
                            canonical_url(person),
277
269
                            cgi.escape(person.displayname))))
278
270
        if not self_subscribed:
279
 
            if not is_really_muted:
280
 
                subscription_terms.insert(0,
281
 
                    SimpleTerm(
282
 
                        self.user, self.user.name,
283
 
                        'subscribe me to this bug'))
284
 
            elif not self.user_is_subscribed_directly:
285
 
                subscription_terms.insert(0,
286
 
                    SimpleTerm(
287
 
                        'update-subscription', 'update-subscription',
288
 
                        'unmute bug mail from this bug and subscribe me to '
289
 
                        'this bug'))
 
271
            subscription_terms.insert(0,
 
272
                SimpleTerm(
 
273
                    self.user, self.user.name, 'subscribe me to this bug'))
290
274
 
291
275
        # Add punctuation to the list of terms.
292
276
        if len(subscription_terms) > 1:
297
281
 
298
282
        subscription_vocabulary = SimpleVocabulary(subscription_terms)
299
283
        if (self._use_advanced_features and
300
 
            (self.user_is_subscribed_directly or self.user_is_muted)):
 
284
            self.user_is_subscribed_directly):
301
285
            default_subscription_value = self._update_subscription_term.value
302
286
        else:
303
287
            default_subscription_value = (
327
311
        if self._use_advanced_features:
328
312
            self.widgets['bug_notification_level'].widget_class = (
329
313
                'bug-notification-level-field')
330
 
            if (len(self.form_fields['subscription'].field.vocabulary)==1):
 
314
            if self._subscriber_count_for_current_user == 0:
331
315
                # We hide the subscription widget if the user isn't
332
316
                # subscribed, since we know who the subscriber is and we
333
317
                # don't need to present them with a single radio button.
338
322
                # subscribe theirself or unsubscribe their team.
339
323
                self.widgets['subscription'].visible = True
340
324
 
341
 
            if self.user_is_subscribed_to_dupes_only:
 
325
            if (self.user_is_subscribed and
 
326
                self.user_is_subscribed_to_dupes_only):
342
327
                # If the user is subscribed via a duplicate but is not
343
328
                # directly subscribed, we hide the
344
329
                # bug_notification_level field, since it's not used.
351
336
    @cachedproperty
352
337
    def user_is_subscribed_directly(self):
353
338
        """Is the user subscribed directly to this bug?"""
354
 
        return self.context.bug.isSubscribed(self.user)
 
339
        return (
 
340
            self.context.bug.isSubscribed(self.user) and not
 
341
            self.user_is_muted)
355
342
 
356
343
    @cachedproperty
357
344
    def user_is_subscribed_to_dupes(self):
358
345
        """Is the user subscribed to dupes of this bug?"""
359
 
        return self.context.bug.isSubscribedToDupes(self.user)
 
346
        return (
 
347
            self.context.bug.isSubscribedToDupes(self.user) and not
 
348
            self.user_is_muted)
360
349
 
361
350
    @property
362
351
    def user_is_subscribed(self):
399
388
 
400
389
        if (subscription_person == self._update_subscription_term.value and
401
390
            (self.user_is_subscribed or self.user_is_muted)):
402
 
            if self.user_is_muted:
403
 
                self._handleUnmute()
404
 
            if self.user_is_subscribed:
405
 
                self._handleUpdateSubscription(level=bug_notification_level)
406
 
            else:
407
 
                self._handleSubscribe(level=bug_notification_level)
 
391
            self._handleUpdateSubscription(level=bug_notification_level)
408
392
        elif self.user_is_muted and subscription_person == self.user:
409
 
            self._handleUnmute()
 
393
            self._handleUnsubscribeCurrentUser()
410
394
        elif (not self.user_is_subscribed and
411
395
            (subscription_person == self.user)):
412
396
            self._handleSubscribe(bug_notification_level)
418
402
        """Handle a subscribe request."""
419
403
        self.context.bug.subscribe(self.user, self.user, level=level)
420
404
        self.request.response.addNotification(
421
 
            "You have subscribed to this bug report.")
 
405
            "You have been subscribed to this bug.")
422
406
 
423
407
    def _handleUnsubscribe(self, user):
424
408
        """Handle an unsubscribe request."""
427
411
        else:
428
412
            self._handleUnsubscribeOtherUser(user)
429
413
 
430
 
    def _handleUnmute(self):
431
 
        """Handle an unmute request."""
432
 
        self.context.bug.unmute(self.user, self.user)
433
 
 
434
414
    def _handleUnsubscribeCurrentUser(self):
435
415
        """Handle the special cases for unsubscribing the current user.
436
416
 
476
456
        subscription = self.current_user_subscription
477
457
        subscription.bug_notification_level = level
478
458
        self.request.response.addNotification(
479
 
            "Your bug report subscription has been updated.")
 
459
            "Your subscription to this bug has been updated.")
480
460
 
481
461
    def _getUnsubscribeNotification(self, user, unsubed_dupes):
482
462
        """Construct and return the unsubscribe-from-bug feedback message.
559
539
        for subscription in direct_subscriptions:
560
540
            if not check_permission('launchpad.View', subscription.person):
561
541
                continue
 
542
            if (subscription.bug_notification_level ==
 
543
                BugNotificationLevel.NOTHING):
 
544
                continue
562
545
            if subscription.person == self.user:
563
546
                can_unsubscribe = [subscription] + can_unsubscribe
564
547
            elif subscription.canBeUnsubscribedByUser(self.user):
638
621
 
639
622
    @property
640
623
    def label(self):
 
624
        return "Mute bug mail for bug %s" % self.context.bug.id
 
625
 
 
626
    page_title = label
 
627
 
 
628
    @property
 
629
    def next_url(self):
 
630
        return canonical_url(self.context)
 
631
 
 
632
    cancel_url = next_url
 
633
 
 
634
    def initialize(self):
 
635
        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.
641
638
        if self.context.bug.isMuted(self.user):
642
 
            return "Unmute bug mail for bug %s" % self.context.bug.id
643
 
        else:
644
 
            return "Mute bug mail for bug %s" % self.context.bug.id
645
 
 
646
 
    page_title = label
647
 
 
648
 
    @property
649
 
    def next_url(self):
650
 
        return canonical_url(self.context)
651
 
 
652
 
    cancel_url = next_url
653
 
 
654
 
    def initialize(self):
655
 
        self.is_muted = self.context.bug.isMuted(self.user)
656
 
        super(BugMuteSelfView, self).initialize()
657
 
 
658
 
    @action('Mute bug mail',
659
 
            name='mute',
660
 
            condition=lambda form, action: not form.is_muted)
 
639
            self.request.response.redirect(
 
640
                canonical_url(self.context, view_name="+subscribe"))
 
641
 
 
642
    @action('Mute bug mail', name='mute')
661
643
    def mute_action(self, action, data):
662
644
        self.context.bug.mute(self.user, self.user)
663
645
        self.request.response.addInfoNotification(
664
646
            "Mail for bug #%s has been muted." % self.context.bug.id)
665
 
 
666
 
    @action('Unmute bug mail',
667
 
            name='unmute',
668
 
            condition=lambda form, action: form.is_muted)
669
 
    def unmute_action(self, action, data):
670
 
        self.context.bug.unmute(self.user, self.user)
671
 
        self.request.response.addInfoNotification(
672
 
            "Mail for bug #%s has been unmuted." % self.context.bug.id)