~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3691.431.11 by Tim Penhey
Branch subscription pages edited
3
4
__metaclass__ = type
5
6
__all__ = [
8588.1.1 by Paul Hummer
Got a successful implementation of branch subscription fetching through AJAX
7
    'BranchPortletSubscribersContent',
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
8
    'BranchSubscriptionAddOtherView',
3691.431.11 by Tim Penhey
Branch subscription pages edited
9
    'BranchSubscriptionAddView',
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
10
    'BranchSubscriptionEditOwnView',
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
11
    'BranchSubscriptionEditView',
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
12
    'BranchSubscriptionPrimaryContext',
3691.431.11 by Tim Penhey
Branch subscription pages edited
13
    ]
14
7675.708.16 by Tim Penhey
Lint and line length tweaks
15
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
16
from zope.interface import implements
4333.2.9 by Tim Penhey
more updates following review
17
3691.431.11 by Tim Penhey
Branch subscription pages edited
18
from canonical.launchpad.webapp import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
19
    canonical_url,
20
    LaunchpadView,
21
    )
10744.1.2 by Tim Penhey
Don't show private teams in the subscriber list.
22
from canonical.launchpad.webapp.authorization import check_permission
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
23
from canonical.launchpad.webapp.interfaces import IPrimaryContext
5594.1.7 by Maris Fogels
Fixed all of the calls to addNotification() that contain HTML so that they use the structured() class.
24
from canonical.launchpad.webapp.menu import structured
9084.1.1 by Tim Penhey
Mechanical changes for branch subscription edit.
25
from canonical.lazr.utils import smartquote
11929.9.1 by Tim Penhey
Move launchpadform into lp.app.browser.
26
from lp.app.browser.launchpadform import (
27
    action,
28
    LaunchpadEditFormView,
29
    LaunchpadFormView,
30
    )
9084.1.1 by Tim Penhey
Mechanical changes for branch subscription edit.
31
from lp.code.enums import BranchSubscriptionNotificationLevel
32
from lp.code.interfaces.branchsubscription import IBranchSubscription
3691.431.11 by Tim Penhey
Branch subscription pages edited
33
3691.432.5 by Tim Penhey
Modifications due to review comments from salgado
34
6998.1.1 by Tim Penhey
Update the primary contexts for branch merge proposals, branch subscriptions, code review comments, and bug branch links.
35
class BranchSubscriptionPrimaryContext:
36
    """The primary context is the subscription is that of the branch."""
37
38
    implements(IPrimaryContext)
39
40
    def __init__(self, branch_subscription):
41
        self.context = IPrimaryContext(branch_subscription.branch).context
42
43
8588.1.1 by Paul Hummer
Got a successful implementation of branch subscription fetching through AJAX
44
class BranchPortletSubscribersContent(LaunchpadView):
45
    """View for the contents for the subscribers portlet.
46
47
    This view is strictly for use with ajax.
48
    """
49
50
    def subscriptions(self):
51
        """Return a decorated list of branch subscriptions."""
10744.1.2 by Tim Penhey
Don't show private teams in the subscriber list.
52
        visible_subscriptions = [
53
            subscription for subscription in self.context.subscriptions
54
            if check_permission('launchpad.View', subscription.person)]
55
        return sorted(
56
            visible_subscriptions,
7675.234.1 by Curtis Hovey
Remove IPerson.browsername. Update all callsites to use displayname instead.
57
            key=lambda subscription: subscription.person.displayname)
8588.1.1 by Paul Hummer
Got a successful implementation of branch subscription fetching through AJAX
58
59
3691.431.11 by Tim Penhey
Branch subscription pages edited
60
class _BranchSubscriptionView(LaunchpadFormView):
3691.432.5 by Tim Penhey
Modifications due to review comments from salgado
61
62
    """Contains the common functionality of the Add and Edit views."""
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
63
3691.431.11 by Tim Penhey
Branch subscription pages edited
64
    schema = IBranchSubscription
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
65
    field_names = ['notification_level', 'max_diff_lines', 'review_level']
3691.431.11 by Tim Penhey
Branch subscription pages edited
66
3691.432.5 by Tim Penhey
Modifications due to review comments from salgado
67
    LEVELS_REQUIRING_LINES_SPECIFICATION = (
3691.431.11 by Tim Penhey
Branch subscription pages edited
68
        BranchSubscriptionNotificationLevel.DIFFSONLY,
69
        BranchSubscriptionNotificationLevel.FULL)
70
71
    @property
72
    def user_is_subscribed(self):
73
        # Since it is technically possible to get to this page when
74
        # the user is not subscribed by hacking the URL, we should
75
        # handle the case nicely.
76
        return self.context.getSubscription(self.user) is not None
77
78
    @property
79
    def next_url(self):
80
        return canonical_url(self.context)
81
6618.2.1 by Paul Hummer
Adds cancel_url, fixes tests
82
    cancel_url = next_url
83
3691.431.11 by Tim Penhey
Branch subscription pages edited
84
    def add_notification_message(self, initial,
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
85
                                 notification_level, max_diff_lines,
86
                                 review_level):
3691.432.5 by Tim Penhey
Modifications due to review comments from salgado
87
        if notification_level in self.LEVELS_REQUIRING_LINES_SPECIFICATION:
3691.431.11 by Tim Penhey
Branch subscription pages edited
88
            lines_message = '<li>%s</li>' % max_diff_lines.description
89
        else:
90
            lines_message = ''
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
91
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
92
        format_str = '%%s<ul><li>%%s</li>%s<li>%%s</li></ul>' % lines_message
5594.1.7 by Maris Fogels
Fixed all of the calls to addNotification() that contain HTML so that they use the structured() class.
93
        message = structured(format_str, initial,
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
94
                             notification_level.description,
95
                             review_level.description)
3691.431.11 by Tim Penhey
Branch subscription pages edited
96
        self.request.response.addNotification(message)
97
98
    def optional_max_diff_lines(self, notification_level, max_diff_lines):
3691.432.5 by Tim Penhey
Modifications due to review comments from salgado
99
        if notification_level in self.LEVELS_REQUIRING_LINES_SPECIFICATION:
3691.431.11 by Tim Penhey
Branch subscription pages edited
100
            return max_diff_lines
101
        else:
102
            return None
103
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
104
3691.431.11 by Tim Penhey
Branch subscription pages edited
105
class BranchSubscriptionAddView(_BranchSubscriptionView):
106
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
107
    subscribing_self = True
108
9209.1.8 by Tim Penhey
Switch branch subscription to generic edit too.
109
    page_title = label = "Subscribe to branch"
110
3691.431.11 by Tim Penhey
Branch subscription pages edited
111
    @action("Subscribe")
112
    def subscribe(self, action, data):
4620.2.1 by Tim Penhey
Fix stale post problems
113
        # To catch the stale post problem, check that the user is not
114
        # subscribed before continuing.
115
        if self.context.hasSubscription(self.user):
116
            self.request.response.addNotification(
117
                'You are already subscribed to this branch.')
118
        else:
119
            notification_level = data['notification_level']
120
            max_diff_lines = self.optional_max_diff_lines(
121
                notification_level, data['max_diff_lines'])
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
122
            review_level = data['review_level']
4620.2.1 by Tim Penhey
Fix stale post problems
123
124
            self.context.subscribe(
7675.708.3 by Tim Penhey
Add subscribed_by and unsubscribed_by to the subscribe and unsubscribe branch methods.
125
                self.user, notification_level, max_diff_lines, review_level,
126
                self.user)
4620.2.1 by Tim Penhey
Fix stale post problems
127
128
            self.add_notification_message(
129
                'You have subscribed to this branch with: ',
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
130
                notification_level, max_diff_lines, review_level)
3691.431.11 by Tim Penhey
Branch subscription pages edited
131
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
132
4333.2.2 by Tim Penhey
linked, albiet weirdly
133
class BranchSubscriptionEditOwnView(_BranchSubscriptionView):
3691.431.11 by Tim Penhey
Branch subscription pages edited
134
135
    @property
9084.3.5 by Tim Penhey
Update branch-edit-subscription.
136
    def label(self):
137
        return "Edit subscription to branch"
138
139
    @property
140
    def page_title(self):
141
        return smartquote(
142
            'Edit subscription to branch "%s"' % self.context.displayname)
143
144
    @property
3691.431.11 by Tim Penhey
Branch subscription pages edited
145
    def initial_values(self):
146
        subscription = self.context.getSubscription(self.user)
147
        if subscription is None:
148
            # This is the case of URL hacking or stale page.
149
            return {}
150
        else:
6699.2.1 by Paul Hummer
Modifies views for some of the subscription stuff
151
            return {'notification_level': subscription.notification_level,
152
                    'max_diff_lines': subscription.max_diff_lines,
153
                    'review_level': subscription.review_level}
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
154
3691.431.11 by Tim Penhey
Branch subscription pages edited
155
    @action("Change")
156
    def change_details(self, action, data):
4620.2.1 by Tim Penhey
Fix stale post problems
157
        # Be proactive in the checking to catch the stale post problem.
158
        if self.context.hasSubscription(self.user):
159
            subscription = self.context.getSubscription(self.user)
160
            subscription.notification_level = data['notification_level']
161
            subscription.max_diff_lines = self.optional_max_diff_lines(
162
                subscription.notification_level,
163
                data['max_diff_lines'])
6699.2.1 by Paul Hummer
Modifies views for some of the subscription stuff
164
            subscription.review_level = data['review_level']
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
165
4620.2.1 by Tim Penhey
Fix stale post problems
166
            self.add_notification_message(
167
                'Subscription updated to: ',
168
                subscription.notification_level,
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
169
                subscription.max_diff_lines,
170
                subscription.review_level)
4620.2.1 by Tim Penhey
Fix stale post problems
171
        else:
172
            self.request.response.addNotification(
173
                'You are not subscribed to this branch.')
3691.431.11 by Tim Penhey
Branch subscription pages edited
174
6699.2.1 by Paul Hummer
Modifies views for some of the subscription stuff
175
    @action("Unsubscribe")
176
    def unsubscribe(self, action, data):
177
        # Be proactive in the checking to catch the stale post problem.
178
        if self.context.hasSubscription(self.user):
7675.708.3 by Tim Penhey
Add subscribed_by and unsubscribed_by to the subscribe and unsubscribe branch methods.
179
            self.context.unsubscribe(self.user, self.user)
6699.2.1 by Paul Hummer
Modifies views for some of the subscription stuff
180
            self.request.response.addNotification(
181
                "You have unsubscribed from this branch.")
182
        else:
183
            self.request.response.addNotification(
184
                'You are not subscribed to this branch.')
185
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
186
187
class BranchSubscriptionAddOtherView(_BranchSubscriptionView):
4333.2.9 by Tim Penhey
more updates following review
188
    """View used to subscribe someone other than the current user."""
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
189
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
190
    field_names = [
191
        'person', 'notification_level', 'max_diff_lines', 'review_level']
5608.5.26 by Aaron Bentley
Fix lint error
192
    for_input = True
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
193
194
    # Since we are subscribing other people, the current user
4333.2.7 by Tim Penhey
Updates following review
195
    # is never considered subscribed.
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
196
    user_is_subscribed = False
197
    subscribing_self = False
198
9209.1.12 by Tim Penhey
Missed the AddOther for branch subscriptions.
199
    page_title = label = "Subscribe to branch"
200
4333.2.9 by Tim Penhey
more updates following review
201
    @action("Subscribe", name="subscribe_action")
202
    def subscribe_action(self, action, data):
203
        """Subscribe the specified user to the branch.
204
4333.2.10 by Tim Penhey
yet more review updates
205
        The user must be a member of a team in order to subscribe that team to
206
        the branch.  Launchpad Admins are special and they can subscribe any
207
        team.
4333.2.9 by Tim Penhey
more updates following review
208
        """
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
209
        notification_level = data['notification_level']
210
        max_diff_lines = self.optional_max_diff_lines(
211
            notification_level, data['max_diff_lines'])
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
212
        review_level = data['review_level']
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
213
        person = data['person']
214
        subscription = self.context.getSubscription(person)
215
        if subscription is None:
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
216
            self.context.subscribe(
7675.708.16 by Tim Penhey
Lint and line length tweaks
217
                person, notification_level, max_diff_lines, review_level,
218
                self.user)
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
219
220
            self.add_notification_message(
221
                '%s has been subscribed to this branch with: '
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
222
                % person.displayname, notification_level, max_diff_lines,
223
                review_level)
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
224
        else:
225
            self.add_notification_message(
4333.2.7 by Tim Penhey
Updates following review
226
                '%s was already subscribed to this branch with: '
227
                % person.displayname,
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
228
                subscription.notification_level, subscription.max_diff_lines,
229
                review_level)
4333.2.1 by Tim Penhey
Initial subscription works, now how to edit...
230
4333.2.2 by Tim Penhey
linked, albiet weirdly
231
232
class BranchSubscriptionEditView(LaunchpadEditFormView):
7510.3.2 by Barry Warsaw
Typo
233
    """The view for editing branch subscriptions.
4333.2.2 by Tim Penhey
linked, albiet weirdly
234
4333.2.9 by Tim Penhey
more updates following review
235
    Used when traversed to the branch subscription itself rather than
236
    through the branch action item to edit the user's own subscription.
237
    This is the only current way to edit a team branch subscription.
238
    """
4333.2.2 by Tim Penhey
linked, albiet weirdly
239
    schema = IBranchSubscription
5608.5.25 by Aaron Bentley
Add code review subscription UI, update tests
240
    field_names = ['notification_level', 'max_diff_lines', 'review_level']
4333.2.2 by Tim Penhey
linked, albiet weirdly
241
9084.1.1 by Tim Penhey
Mechanical changes for branch subscription edit.
242
    @property
243
    def page_title(self):
244
        return smartquote(
245
            'Edit subscription to branch "%s"' % self.branch.displayname)
246
247
    @property
248
    def label(self):
249
        return "Edit subscription to branch for %s" % self.person.displayname
250
4333.2.2 by Tim Penhey
linked, albiet weirdly
251
    def initialize(self):
252
        self.branch = self.context.branch
253
        self.person = self.context.person
254
        LaunchpadEditFormView.initialize(self)
255
6699.2.1 by Paul Hummer
Modifies views for some of the subscription stuff
256
    @action("Change", name="change")
257
    def change_action(self, action, data):
258
        """Update the branch subscription."""
259
        self.updateContextFromData(data)
260
4333.2.11 by Tim Penhey
final updates from review
261
    @action("Unsubscribe", name="unsubscribe")
4333.2.9 by Tim Penhey
more updates following review
262
    def unsubscribe_action(self, action, data):
263
        """Unsubscribe the team from the branch."""
7675.708.3 by Tim Penhey
Add subscribed_by and unsubscribed_by to the subscribe and unsubscribe branch methods.
264
        self.branch.unsubscribe(self.person, self.user)
4333.2.2 by Tim Penhey
linked, albiet weirdly
265
        self.request.response.addNotification(
4333.2.7 by Tim Penhey
Updates following review
266
            "%s has been unsubscribed from this branch."
267
            % self.person.displayname)
4333.2.2 by Tim Penhey
linked, albiet weirdly
268
269
    @property
270
    def next_url(self):
271
        return canonical_url(self.branch)
6618.2.1 by Paul Hummer
Adds cancel_url, fixes tests
272
273
    cancel_url = next_url
274