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

"""View support classes for feeds."""

__metaclass__ = type

__all__ = [
    'AnnouncementsFeedLink',
    'BranchFeedLink',
    'BugFeedLink',
    'BugTargetLatestBugsFeedLink',
    'FeedLinkBase',
    'FeedsMixin',
    'FeedsNavigation',
    'FeedsRootUrlData',
    'PersonBranchesFeedLink',
    'PersonRevisionsFeedLink',
    'ProductBranchesFeedLink',
    'ProductRevisionsFeedLink',
    'ProjectBranchesFeedLink',
    'ProjectRevisionsFeedLink',
    'RootAnnouncementsFeedLink',
    ]

from zope.component import getUtility
from zope.interface import implements
from zope.publisher.interfaces import NotFound
from zope.security.interfaces import Unauthorized

from lp.services.config import config
from lp.layers import FeedsLayer
from lp.services.webapp import (
    canonical_name,
    canonical_url,
    Navigation,
    stepto,
    )
from lp.services.webapp.interfaces import (
    ICanonicalUrlData,
    ILaunchpadRoot,
    )
from lp.services.webapp.publisher import RedirectionView
from lp.services.webapp.url import urlappend
from lp.services.webapp.vhosts import allvhosts
from lp.app.errors import NotFoundError
from lp.bugs.interfaces.bug import IBugSet
from lp.bugs.interfaces.bugtarget import IHasBugs
from lp.bugs.interfaces.bugtask import (
    IBugTask,
    IBugTaskSet,
    )
from lp.code.interfaces.branch import IBranch
from lp.registry.interfaces.announcement import (
    IAnnouncementSet,
    IHasAnnouncements,
    )
from lp.registry.interfaces.person import (
    IPerson,
    IPersonSet,
    )
from lp.registry.interfaces.pillar import IPillarNameSet
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.projectgroup import IProjectGroup
from lp.services.feeds.interfaces import IFeedsApplication


class FeedsRootUrlData:
    """`ICanonicalUrlData` for Feeds."""

    implements(ICanonicalUrlData)

    path = ''
    inside = None
    rootsite = 'feeds'

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


class FeedsNavigation(Navigation):
    """Navigation for `IFeedsApplication`."""

    usedfor = IFeedsApplication

    newlayer = FeedsLayer

    @stepto('+index')
    def redirect_index(self):
        """Redirect /+index to help.launchpad.net/Feeds site.

        This provides a useful destination for users who visit
        http://feeds.launchpad.net in their browser.  It is also useful to
        avoid OOPSes when some RSS feeders (e.g. Safari) that make a request
        to the default site.
        """
        return self.redirectSubTree(
            'https://help.launchpad.net/Feeds', status=301)

    def traverse(self, name):
        """Traverse the paths of a feed.

        If a query string is provided it is normalized.  'bugs' paths and
        persons ('~') are special cased.
        """
        # Normalize the query string so caching is more effective.  This is
        # done by simply sorting the entries.

        # XXX bac 20071019, we would like to normalize with respect to case
        # too but cannot due to a problem with the bug search requiring status
        # values to be of a particular case.  See bug 154562.
        query_string = self.request.get('QUERY_STRING', '')
        fields = sorted(query_string.split('&'))
        normalized_query_string = '&'.join(fields)
        if query_string != normalized_query_string:
            # We must empty the traversal stack to prevent an error
            # when calling RedirectionView.publishTraverse().
            self.request.setTraversalStack([])
            target = "%s%s?%s" % (self.request.getApplicationURL(),
                                  self.request['PATH_INFO'],
                                  normalized_query_string)
            redirect = RedirectionView(target, self.request, 301)
            return redirect

        # Handle the two formats of urls:
        # http://feeds.launchpad.net/bugs/+bugs.atom?...
        # http://feeds.launchpad.net/bugs/1/bug.atom
        if name == 'bugs':
            stack = self.request.getTraversalStack()
            if len(stack) == 0:
                raise NotFound(self, '', self.request)
            bug_id = stack.pop()
            if bug_id.startswith('+'):
                if config.launchpad.is_bug_search_feed_active:
                    return getUtility(IBugTaskSet)
                else:
                    raise Unauthorized("Bug search feed deactivated")
            else:
                self.request.stepstogo.consume()
                return getUtility(IBugSet).getByNameOrID(bug_id)

        # Redirect to the canonical name before doing the lookup.
        if canonical_name(name) != name:
            return self.redirectSubTree(
                canonical_url(self.context) + canonical_name(name),
                status=301)

        try:
            if name.startswith('~'):
                # Handle persons and teams.
                # http://feeds.launchpad.net/~salgado/latest-bugs.html
                person = getUtility(IPersonSet).getByName(name[1:])
                return person
            else:
                # Otherwise, handle products, projects, and distros
                return getUtility(IPillarNameSet)[name]
        except NotFoundError:
            raise NotFound(self, name, self.request)


class FeedLinkBase:
    """Base class for formatting an Atom <link> tag.

    Subclasses must override:
        href: Url pointing to atom feed.

    Subclasses can override:
        title: The name of the feed as it appears in a browser.
    """
    title = 'Atom Feed'
    href = None
    rooturl = allvhosts.configs['feeds'].rooturl

    def __init__(self, context):
        self.context = context
        assert self.usedfor.providedBy(context), (
            "Context %r does not provide interface %r"
            % (context, self.usedfor))

    @classmethod
    def allowFeed(cls, context):
        """Return True if a feed is allowed for the given context.

        Subclasses should override this method as necessary.
        """
        return True


class BugFeedLink(FeedLinkBase):
    usedfor = IBugTask

    @property
    def title(self):
        return 'Bug %s Feed' % self.context.bug.id

    @property
    def href(self):
        return urlappend(self.rooturl,
                         'bugs/' + str(self.context.bug.id) + '/bug.atom')

    @classmethod
    def allowFeed(cls, context):
        """See `FeedLinkBase`"""
        # No feeds for private bugs.
        return not context.bug.private


class BugTargetLatestBugsFeedLink(FeedLinkBase):
    usedfor = IHasBugs

    @property
    def title(self):
        return 'Latest Bugs for %s' % self.context.displayname

    @property
    def href(self):
        return urlappend(canonical_url(self.context, rootsite='feeds'),
                         'latest-bugs.atom')


class AnnouncementsFeedLink(FeedLinkBase):
    usedfor = IHasAnnouncements

    @property
    def title(self):
        if IAnnouncementSet.providedBy(self.context):
            return 'All Announcements'
        else:
            return 'Announcements for %s' % self.context.displayname

    @property
    def href(self):
        if IAnnouncementSet.providedBy(self.context):
            return urlappend(self.rooturl, 'announcements.atom')
        else:
            return urlappend(canonical_url(self.context, rootsite='feeds'),
                             'announcements.atom')


class RootAnnouncementsFeedLink(AnnouncementsFeedLink):
    usedfor = ILaunchpadRoot

    @property
    def title(self):
        return 'All Announcements'

    @property
    def href(self):
        return urlappend(self.rooturl, 'announcements.atom')


class BranchesFeedLinkBase(FeedLinkBase):
    """Base class for objects with branches."""

    @property
    def title(self):
        return 'Latest Branches for %s' % self.context.displayname

    @property
    def href(self):
        return urlappend(canonical_url(self.context, rootsite='feeds'),
                         'branches.atom')


class ProjectBranchesFeedLink(BranchesFeedLinkBase):
    """Feed links for branches on a project."""
    usedfor = IProjectGroup


class ProductBranchesFeedLink(BranchesFeedLinkBase):
    """Feed links for branches on a product."""
    usedfor = IProduct


class PersonBranchesFeedLink(BranchesFeedLinkBase):
    """Feed links for branches on a person."""
    usedfor = IPerson


class RevisionsFeedLinkBase(FeedLinkBase):
    """Base class for objects with revisions."""

    @property
    def title(self):
        return 'Latest Revisions for %s' % self.context.displayname

    @property
    def href(self):
        """The location of the feed.

        E.g.  http://feeds.launchpad.net/firefox/revisions.atom
        """
        return urlappend(canonical_url(self.context, rootsite='feeds'),
                         'revisions.atom')


class ProjectRevisionsFeedLink(RevisionsFeedLinkBase):
    """Feed links for revisions on a project."""
    usedfor = IProjectGroup


class ProductRevisionsFeedLink(RevisionsFeedLinkBase):
    """Feed links for revisions on a product."""
    usedfor = IProduct


class BranchFeedLink(FeedLinkBase):
    """Feed links for revisions on a branch."""
    usedfor = IBranch

    @property
    def title(self):
        return 'Latest Revisions for Branch %s' % self.context.displayname

    @property
    def href(self):
        return urlappend(canonical_url(self.context, rootsite="feeds"),
                         'branch.atom')

    @classmethod
    def allowFeed(cls, context):
        """See `FeedLinkBase`"""
        # No feeds for private branches.
        return not context.private


class PersonRevisionsFeedLink(FeedLinkBase):
    """Feed links for revisions created by a person."""
    usedfor = IPerson

    @property
    def title(self):
        if self.context.is_team:
            return 'Latest Revisions by members of %s' % (
                self.context.displayname)
        else:
            return 'Latest Revisions by %s' % self.context.displayname

    @property
    def href(self):
        return urlappend(canonical_url(self.context, rootsite="feeds"),
                         'revisions.atom')


class FeedsMixin:
    """Mixin which adds the feed_links attribute to a view object.

    feed_types: This class attribute can be overridden to reduce the
        feed links that are added to the page.

    feed_links: Returns a list of objects subclassed from FeedLinkBase.
    """
    feed_types = (
        AnnouncementsFeedLink,
        BranchFeedLink,
        BugFeedLink,
        BugTargetLatestBugsFeedLink,
        PersonBranchesFeedLink,
        PersonRevisionsFeedLink,
        ProductBranchesFeedLink,
        ProductRevisionsFeedLink,
        ProjectBranchesFeedLink,
        ProjectRevisionsFeedLink,
        RootAnnouncementsFeedLink,
        )

    @property
    def feed_links(self):

        def allowFeed(feed_type, context):
            return (feed_type.usedfor.providedBy(context) and
                feed_type.allowFeed(context))

        return [feed_type(self.context)
            for feed_type in self.feed_types
            if allowFeed(feed_type, self.context)]