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

"""Vostok's custom publication."""

__metaclass__ = type
__all__ = [
    'VostokBrowserRequest',
    'VostokLayer',
    'VostokRootNavigation',
    'vostok_request_publication_factory',
    ]


from zope.component import getUtility
from zope.interface import (
    implements,
    Interface,
    )
from zope.publisher.interfaces.browser import (
    IBrowserRequest,
    IDefaultBrowserLayer,
    )

from canonical.launchpad.webapp import (
    canonical_url,
    Navigation,
    )
from canonical.launchpad.webapp.publication import LaunchpadBrowserPublication
from canonical.launchpad.webapp.servers import (
    LaunchpadBrowserRequest,
    LaunchpadBrowserResponse,
    VirtualHostRequestPublicationFactory,
    )
from canonical.launchpad.webapp.vhosts import allvhosts
from lp.registry.interfaces.distribution import IDistributionSet


class VostokLayer(IBrowserRequest, IDefaultBrowserLayer):
    """The Vostok layer."""


class VostokRequestMixin:
    """This mixin defines behaviour for the real and test Vostok requests."""

    implements(VostokLayer)

    def getRootURL(self, rootsite):
        """See `IBasicLaunchpadRequest`."""
        return allvhosts.configs['vostok'].rooturl


class VostokBrowserRequest(VostokRequestMixin, LaunchpadBrowserRequest):
    """Request class for Vostok layer."""

    def _createResponse(self):
        """As per zope.publisher.browser.BrowserRequest._createResponse"""
        return VostokBrowserResponse()


class VostokBrowserResponse(LaunchpadBrowserResponse):

    def redirect(self, location, status=None, trusted=False,
                 temporary_if_possible=False):
        """Override the parent method to make redirects untrusted by default.

        This is so that we don't allow redirects to any hosts other than
        vostok's.
        """
        # Need to call LaunchpadBrowserResponse.redirect() directly because
        # the temporary_if_possible argument only exists there.
        LaunchpadBrowserResponse.redirect(
            self, location, status=status, trusted=trusted,
            temporary_if_possible=temporary_if_possible)


class IVostokRoot(Interface):
    """Marker interface for the root vostok object."""


class VostokRoot:
    """The root object for the Vostok site.

    No behaviour here, it just exists so it can have view and navigation
    registrations attached to it.
    """

    implements(IVostokRoot)


class VostokRootNavigation(Navigation):

    usedfor = IVostokRoot

    def traverse(self, name):
        distro = getUtility(IDistributionSet)[name]
        if distro is not None and distro.name != name:
            # This distro was accessed through one of its aliases, so we
            # must redirect to its canonical URL.
            return self.redirectSubTree(canonical_url(distro), status=301)
        return distro


class VostokBrowserPublication(LaunchpadBrowserPublication):
    root_object_interface = IVostokRoot


def vostok_request_publication_factory():
    return VirtualHostRequestPublicationFactory(
        'vostok', VostokBrowserRequest, VostokBrowserPublication)