11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
1 |
# Copyright 2010 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
3 |
||
11128.9.5
by Michael Hudson
neatenings, minimise code |
4 |
"""Vostok's custom publication."""
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
5 |
|
6 |
__metaclass__ = type |
|
11128.9.5
by Michael Hudson
neatenings, minimise code |
7 |
__all__ = [ |
8 |
'VostokBrowserRequest', |
|
9 |
'VostokLayer', |
|
11151.6.3
by Guilherme Salgado
Use a custom navigation for VostokRoot so that we only traverse to distributions |
10 |
'VostokRootNavigation', |
11128.9.5
by Michael Hudson
neatenings, minimise code |
11 |
'vostok_request_publication_factory', |
12 |
]
|
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
13 |
|
14 |
||
11151.6.3
by Guilherme Salgado
Use a custom navigation for VostokRoot so that we only traverse to distributions |
15 |
from zope.component import getUtility |
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
16 |
from zope.interface import ( |
17 |
implements, |
|
18 |
Interface, |
|
19 |
)
|
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
20 |
from zope.publisher.interfaces.browser import ( |
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
21 |
IBrowserRequest, |
22 |
IDefaultBrowserLayer, |
|
23 |
)
|
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
24 |
|
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
25 |
from canonical.launchpad.webapp import ( |
26 |
canonical_url, |
|
27 |
Navigation, |
|
28 |
)
|
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
29 |
from canonical.launchpad.webapp.publication import LaunchpadBrowserPublication |
30 |
from canonical.launchpad.webapp.servers import ( |
|
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
31 |
LaunchpadBrowserRequest, |
32 |
LaunchpadBrowserResponse, |
|
33 |
VirtualHostRequestPublicationFactory, |
|
34 |
)
|
|
11128.9.19
by Michael Hudson
a more promising line of testing |
35 |
from canonical.launchpad.webapp.vhosts import allvhosts |
11151.6.3
by Guilherme Salgado
Use a custom navigation for VostokRoot so that we only traverse to distributions |
36 |
from lp.registry.interfaces.distribution import IDistributionSet |
37 |
||
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
38 |
|
39 |
class VostokLayer(IBrowserRequest, IDefaultBrowserLayer): |
|
11128.9.5
by Michael Hudson
neatenings, minimise code |
40 |
"""The Vostok layer."""
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
41 |
|
42 |
||
11128.9.19
by Michael Hudson
a more promising line of testing |
43 |
class VostokRequestMixin: |
11128.9.34
by Michael Hudson
bunch o' docstrings |
44 |
"""This mixin defines behaviour for the real and test Vostok requests."""
|
11128.9.19
by Michael Hudson
a more promising line of testing |
45 |
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
46 |
implements(VostokLayer) |
47 |
||
11128.9.19
by Michael Hudson
a more promising line of testing |
48 |
def getRootURL(self, rootsite): |
49 |
"""See `IBasicLaunchpadRequest`."""
|
|
50 |
return allvhosts.configs['vostok'].rooturl |
|
51 |
||
52 |
||
53 |
class VostokBrowserRequest(VostokRequestMixin, LaunchpadBrowserRequest): |
|
11128.9.34
by Michael Hudson
bunch o' docstrings |
54 |
"""Request class for Vostok layer."""
|
11128.9.19
by Michael Hudson
a more promising line of testing |
55 |
|
7675.782.5
by Guilherme Salgado
Make sure vostok.dev doesn't redirect users to any host other than vostok.dev |
56 |
def _createResponse(self): |
57 |
"""As per zope.publisher.browser.BrowserRequest._createResponse"""
|
|
58 |
return VostokBrowserResponse() |
|
59 |
||
60 |
||
61 |
class VostokBrowserResponse(LaunchpadBrowserResponse): |
|
62 |
||
63 |
def redirect(self, location, status=None, trusted=False, |
|
64 |
temporary_if_possible=False): |
|
65 |
"""Override the parent method to make redirects untrusted by default.
|
|
66 |
||
67 |
This is so that we don't allow redirects to any hosts other than
|
|
68 |
vostok's.
|
|
69 |
"""
|
|
70 |
# Need to call LaunchpadBrowserResponse.redirect() directly because
|
|
71 |
# the temporary_if_possible argument only exists there.
|
|
72 |
LaunchpadBrowserResponse.redirect( |
|
73 |
self, location, status=status, trusted=trusted, |
|
74 |
temporary_if_possible=temporary_if_possible) |
|
75 |
||
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
76 |
|
11128.11.9
by Michael Hudson
review comments |
77 |
class IVostokRoot(Interface): |
11128.9.11
by Michael Hudson
the test passes |
78 |
"""Marker interface for the root vostok object."""
|
79 |
||
80 |
||
81 |
class VostokRoot: |
|
11128.9.34
by Michael Hudson
bunch o' docstrings |
82 |
"""The root object for the Vostok site.
|
83 |
||
84 |
No behaviour here, it just exists so it can have view and navigation
|
|
85 |
registrations attached to it.
|
|
86 |
"""
|
|
87 |
||
11128.9.11
by Michael Hudson
the test passes |
88 |
implements(IVostokRoot) |
89 |
||
90 |
||
11151.6.3
by Guilherme Salgado
Use a custom navigation for VostokRoot so that we only traverse to distributions |
91 |
class VostokRootNavigation(Navigation): |
92 |
||
93 |
usedfor = IVostokRoot |
|
94 |
||
95 |
def traverse(self, name): |
|
96 |
distro = getUtility(IDistributionSet)[name] |
|
97 |
if distro is not None and distro.name != name: |
|
98 |
# This distro was accessed through one of its aliases, so we
|
|
99 |
# must redirect to its canonical URL.
|
|
100 |
return self.redirectSubTree(canonical_url(distro), status=301) |
|
101 |
return distro |
|
102 |
||
103 |
||
11128.9.11
by Michael Hudson
the test passes |
104 |
class VostokBrowserPublication(LaunchpadBrowserPublication): |
105 |
root_object_interface = IVostokRoot |
|
11128.9.2
by Michael Hudson
add some stuff including a test that fails for interesting reasons |
106 |
|
107 |
||
108 |
def vostok_request_publication_factory(): |
|
109 |
return VirtualHostRequestPublicationFactory( |
|
11128.9.11
by Michael Hudson
the test passes |
110 |
'vostok', VostokBrowserRequest, VostokBrowserPublication) |