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

# pylint: disable-msg=F0401

"""Browser views related to archive subscriptions."""

__metaclass__ = type

__all__ = [
    'ArchiveSubscribersView',
    'PersonArchiveSubscriptionView',
    'PersonArchiveSubscriptionsView',
    'traverse_archive_subscription_for_subscriber',
    ]

import datetime
from operator import attrgetter

import pytz
from zope.app.form import CustomWidgetFactory
from zope.app.form.browser import TextWidget
from zope.component import getUtility
from zope.formlib import form
from zope.interface import (
    implements,
    Interface,
    )
from zope.schema import (
    Date,
    Text,
    )

from lp import _
from lp.app.browser.launchpadform import (
    action,
    custom_widget,
    LaunchpadEditFormView,
    LaunchpadFormView,
    )
from lp.app.widgets.date import DateWidget
from lp.app.widgets.popup import PersonPickerWidget
from lp.registry.interfaces.person import IPersonSet
from lp.services.fields import PersonChoice
from lp.services.propertycache import cachedproperty
from lp.services.webapp.publisher import (
    canonical_url,
    LaunchpadView,
    )
from lp.soyuz.browser.sourceslist import SourcesListEntriesWidget
from lp.soyuz.interfaces.archive import IArchiveSet
from lp.soyuz.interfaces.archivesubscriber import (
    IArchiveSubscriberSet,
    IPersonalArchiveSubscription,
    )


def archive_subscription_ui_adapter(archive_subscription):
    """Adapt an archive subscriber to the UI interface.

    Since we are only modifying the type of fields that already exist
    on IArchiveSubscriber, we simply return the archive_subscriber record.
    """
    return archive_subscription


class PersonalArchiveSubscription:
    """See `IPersonalArchiveSubscription`."""

    implements(IPersonalArchiveSubscription)

    def __init__(self, subscriber, archive):
        self.subscriber = subscriber
        self.archive = archive

    @property
    def displayname(self):
        """See `IPersonalArchiveSubscription`."""
        return "Access to %s" % self.archive.displayname

    @property
    def title(self):
        """Required for default headings in templates."""
        return self.displayname


def traverse_archive_subscription_for_subscriber(subscriber, archive_id):
    """Return the subscription for a subscriber to an archive."""
    subscription = None
    archive = getUtility(IArchiveSet).get(archive_id)
    if archive:
        subscription = getUtility(IArchiveSubscriberSet).getBySubscriber(
            subscriber, archive=archive).first()

    if subscription is None:
        return None
    else:
        return PersonalArchiveSubscription(subscriber, archive)


class IArchiveSubscriberUI(Interface):
    """A custom interface for user interaction with archive subscriptions.

    IArchiveSubscriber uses a datetime field for date_expires, whereas
    we simply want to use a date field when users create or edit new
    subscriptions.
    """
    subscriber = PersonChoice(
        title=_("Subscriber"), required=True, vocabulary='ValidPersonOrTeam',
        description=_("The person or team to grant access."))

    date_expires = Date(
        title=_("Date of Expiration"), required=False,
        description=_("The date when the access will expire. "
                      "Leave this blank for access that should "
                      "never expire."))

    description = Text(
        title=_("Description"), required=False,
        description=_("Optional notes about this access."))


class ArchiveSubscribersView(LaunchpadFormView):
    """A view for listing and creating archive subscribers."""

    schema = IArchiveSubscriberUI
    field_names = ['subscriber', 'date_expires', 'description']
    custom_widget('description', TextWidget, displayWidth=40)
    custom_widget('date_expires', CustomWidgetFactory(DateWidget))
    custom_widget('subscriber', PersonPickerWidget,
        header="Select the subscriber")

    @property
    def label(self):
        """Return a label for the view's main heading."""
        return "Manage access to " + self.context.title

    def initialize(self):
        """Ensure that we are dealing with a private archive."""
        # If this archive is not private, then we should not be
        # managing the subscribers.
        if not self.context.private:
            self.request.response.addNotification(
                "Only private archives can have subscribers.")
            self.request.response.redirect(
                canonical_url(self.context))
            return

        super(ArchiveSubscribersView, self).initialize()

    @cachedproperty
    def subscriptions(self):
        """Return all the subscriptions for this archive."""
        result = list(getUtility(IArchiveSubscriberSet
            ).getByArchive(self.context))
        ids = set(map(attrgetter('subscriber_id'), result))
        list(getUtility(IPersonSet).getPrecachedPersonsFromIDs(ids,
            need_validity=True))
        return result

    @cachedproperty
    def has_subscriptions(self):
        """Return whether this archive has any subscribers."""
        return bool(self.subscriptions)

    def validate_new_subscription(self, action, data):
        """Ensure the subscriber isn't already subscribed.

        Also ensures that the expiry date is in the future.
        """
        form.getWidgetsData(self.widgets, 'field', data)
        subscriber = data.get('subscriber')
        date_expires = data.get('date_expires')

        if subscriber is not None:
            subscriber_set = getUtility(IArchiveSubscriberSet)
            current_subscription = subscriber_set.getBySubscriber(
                subscriber, archive=self.context)

            # XXX noodles 20090212 bug=246200: use bool() when it gets fixed
            # in storm.
            if current_subscription.any() is not None:
                self.setFieldError('subscriber',
                    "%s is already subscribed." % subscriber.displayname)

        if date_expires:
            if date_expires < datetime.date.today():
                self.setFieldError('date_expires',
                    "The expiry date must be in the future.")

    @action(u"Add", name="add",
            validator="validate_new_subscription")
    def create_subscription(self, action, data):
        """Create a subscription for the supplied user."""
        # As we present a date selection to the user for expiry, we
        # need to convert the value into a datetime with UTC:
        date_expires = data['date_expires']
        if date_expires:
            date_expires = datetime.datetime(
                date_expires.year,
                date_expires.month,
                date_expires.day,
                tzinfo=pytz.timezone('UTC'))
        self.context.newSubscription(
            data['subscriber'],
            self.user,
            description=data['description'],
            date_expires=date_expires)

        subscriber_individuals = data['subscriber'].displayname
        if data['subscriber'].is_team:
            subscriber_individuals = "Members of " + subscriber_individuals

        notification = (
            "You have granted access for %(subscriber)s to install "
            "software from %(archive)s. "
            "%(subscriber_individuals)s will be notified of the access "
            " via email."
            ) % {
                'subscriber': data['subscriber'].displayname,
                'archive': self.context.displayname,
                'subscriber_individuals': subscriber_individuals,
                }

        self.request.response.addNotification(notification)

        # Just ensure a redirect happens (back to ourselves).
        self.next_url = str(self.request.URL)


class ArchiveSubscriptionEditView(LaunchpadEditFormView):
    """A view for editing and canceling an archive subscriber."""

    schema = IArchiveSubscriberUI
    field_names = ['date_expires', 'description']
    custom_widget('description', TextWidget, displayWidth=40)
    custom_widget('date_expires', CustomWidgetFactory(DateWidget))

    @property
    def label(self):
        """Return a label for the view's main heading."""
        return "Edit " + self.context.displayname

    def validate_update_subscription(self, action, data):
        """Ensure that the date of expiry is not in the past."""
        form.getWidgetsData(self.widgets, 'field', data)
        date_expires = data.get('date_expires')

        if date_expires:
            if date_expires < datetime.date.today():
                self.setFieldError('date_expires',
                    "The expiry date must be in the future.")

    @action(
        u'Save', name='update', validator="validate_update_subscription")
    def update_subscription(self, action, data):
        """Update the context subscription with the new data."""
        # As we present a date selection to the user for expiry, we
        # need to convert the value into a datetime with UTC:
        date_expires = data['date_expires']

        if date_expires:
            data['date_expires'] = datetime.datetime(
                date_expires.year,
                date_expires.month,
                date_expires.day,
                tzinfo=pytz.timezone('UTC'))

        self.updateContextFromData(data)

        notification = "The access for %s has been updated." % (
            self.context.subscriber.displayname)
        self.request.response.addNotification(notification)

    @action(u'Revoke access', name='cancel')
    def cancel_subscription(self, action, data):
        """Cancel the context subscription."""
        self.context.cancel(self.user)

        notification = "You have revoked %s's access to %s." % (
            self.context.subscriber.displayname,
            self.context.archive.displayname)
        self.request.response.addNotification(notification)

    @property
    def next_url(self):
        """Calculate and return the url to which we want to redirect."""
        return canonical_url(self.context.archive) + "/+subscriptions"

    @property
    def cancel_url(self):
        """Return the url to which we want to go to if user cancels."""
        return self.next_url


class PersonArchiveSubscriptionsView(LaunchpadView):
    """A view for displaying a persons archive subscriptions."""

    label = "Private PPA access"

    @cachedproperty
    def subscriptions_with_tokens(self):
        """Return all the persons archive subscriptions with the token
        for each.

        The result is formatted as a list of dicts to make the TALS code
        cleaner.
        """
        subscriber_set = getUtility(IArchiveSubscriberSet)
        subs_with_tokens = subscriber_set.getBySubscriberWithActiveToken(
            self.context)

        # Turn the result set into a list of dicts so it can be easily
        # accessed in TAL. Note that we need to ensure that only one
        # PersonalArchiveSubscription is included for each archive,
        # as the person might have participation in multiple
        # subscriptions (via different teams).
        unique_archives = set()
        personal_subscription_tokens = []
        for subscription, token in subs_with_tokens:
            if subscription.archive in unique_archives:
                continue

            unique_archives.add(subscription.archive)

            personal_subscription = PersonalArchiveSubscription(
                self.context, subscription.archive)
            personal_subscription_tokens.append({
                'subscription': personal_subscription,
                'token': token
                })

        return personal_subscription_tokens


class PersonArchiveSubscriptionView(LaunchpadView, SourcesListEntriesWidget):
    """Display a user's archive subscription and relevant info.

    This includes the current sources.list entries (if the subscription
    has a current token), and the ability to generate and re-generate
    tokens.
    """

    @property
    def label(self):
        """Return the label for the view's main heading."""
        return self.context.title

    def initialize(self):
        """Process any posted actions."""
        super(PersonArchiveSubscriptionView, self).initialize()
        # Set the archive attribute so SourcesListEntriesWidget can be built
        # correctly.
        self.archive = self.context.archive

        # If an activation was requested and there isn't a currently
        # active token, then create a token, provide a notification
        # and redirect.
        if self.request.form.get('activate') and not self.active_token:
            self.context.archive.newAuthToken(self.context.subscriber)

            self.request.response.redirect(self.request.getURL())

        # Otherwise, if a regeneration was requested and there is an
        # active token, then cancel the old token, create a new one,
        # provide a notification and redirect.
        elif self.request.form.get('regenerate') and self.active_token:
            self.active_token.deactivate()

            self.context.archive.newAuthToken(self.context.subscriber)

            self.request.response.addNotification(
                "Launchpad has generated the new password you requested "
                "for your access to the archive %s. Please follow "
                "the instructions below to update your custom "
                "\"sources.list\"." % self.context.archive.displayname)

            self.request.response.redirect(self.request.getURL())