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
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""
LaunchBag
The collection of stuff we have traversed.
"""
__metaclass__ = type
import pytz
from zope import thread
from zope.component import getUtility
from zope.interface import implements
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.blueprints.interfaces.specification import ISpecification
from lp.bugs.interfaces.bug import IBug
from lp.bugs.interfaces.bugtask import IBugTask
from lp.registry.interfaces.distribution import IDistribution
from lp.registry.interfaces.distroseries import IDistroSeries
from lp.registry.interfaces.person import IPerson
from lp.registry.interfaces.product import IProduct
from lp.registry.interfaces.projectgroup import IProjectGroup
from lp.registry.interfaces.sourcepackage import ISourcePackage
from lp.services.database.sqlbase import block_implicit_flushes
from lp.services.identity.interfaces.account import IAccount
from lp.services.webapp.interaction import get_current_principal
from lp.services.webapp.interfaces import (
ILaunchBag,
ILoggedInEvent,
IOpenLaunchBag,
)
from lp.soyuz.interfaces.distroarchseries import IDistroArchSeries
_utc_tz = pytz.timezone('UTC')
class LaunchBag:
implements(IOpenLaunchBag)
# Map Interface to attribute name.
_registry = {
IPerson: 'person',
IProjectGroup: 'project',
IProduct: 'product',
IDistribution: 'distribution',
IDistroSeries: 'distroseries',
IDistroArchSeries: 'distroarchseries',
ISourcePackage: 'sourcepackage',
ISpecification: 'specification',
IBug: 'bug',
IBugTask: 'bugtask',
}
_store = thread.local()
def setLogin(self, login):
'''See IOpenLaunchBag.'''
self._store.login = login
@property
def login(self):
return getattr(self._store, 'login', None)
def setDeveloper(self, is_developer):
'''See IOpenLaunchBag.'''
self._store.developer = is_developer
@property
def developer(self):
return getattr(self._store, 'developer', False)
@property
@block_implicit_flushes
def account(self):
return IAccount(get_current_principal(), None)
@property
@block_implicit_flushes
def user(self):
return IPerson(get_current_principal(), None)
def add(self, obj):
store = self._store
for interface, attribute in self._registry.items():
if interface.providedBy(obj):
setattr(store, attribute, obj)
def clear(self):
store = self._store
for attribute in self._registry.values():
setattr(store, attribute, None)
store.login = None
store.time_zone = None
@property
def person(self):
return self._store.person
@property
def project(self):
store = self._store
if store.project is not None:
return store.project
elif store.product is not None:
return store.product.project
else:
return None
@property
def product(self):
return getattr(self._store, "product", None)
@property
def distribution(self):
return getattr(self._store, "distribution", None)
@property
def distroseries(self):
return self._store.distroseries
@property
def distroarchseries(self):
return self._store.distroarchseries
@property
def sourcepackage(self):
return self._store.sourcepackage
@property
def sourcepackagereleasepublishing(self):
return self._store.sourcepackagereleasepublishing
@property
def specification(self):
return self._store.specification
@property
def bug(self):
if self._store.bug:
return self._store.bug
if self._store.bugtask:
return self._store.bugtask.bug
@property
def bugtask(self):
return getattr(self._store, "bugtask", None)
@property
def time_zone(self):
if getattr(self._store, "time_zone", None) is None:
if self.user and self.user.time_zone:
self._store.time_zone = pytz.timezone(self.user.time_zone)
else:
# fall back to UTC
self._store.time_zone = _utc_tz
return self._store.time_zone
class LaunchBagView(object):
def __init__(self, context, request):
self.context = context
self.request = request
self.bag = getUtility(ILaunchBag)
def set_login_in_launchbag_when_principal_identified(event):
"""This IPrincipalIdentifiedEvent subscriber sets 'login' in launchbag.
"""
launchbag = getUtility(IOpenLaunchBag)
# Basic auths principal identified event is also an ILoggedInEvent.
# Cookie auth seperates these two events.
loggedinevent = ILoggedInEvent(event, None)
if loggedinevent is None:
# We must be using session auth.
launchbag.setLogin(event.login)
else:
launchbag.setLogin(loggedinevent.login)
def set_developer_in_launchbag_before_traversal(event):
"""Subscriber for IBeforeTraverseEvent
Sets the 'user is a launchpad developer flag' early, as we need
it available if an exception occurs; If we leave it until needed,
we may no longer have the functionality we need to look this up.
"""
launchbag = getUtility(IOpenLaunchBag)
user = launchbag.user
if user is None:
launchbag.setDeveloper(False)
else:
celebrities = getUtility(ILaunchpadCelebrities)
is_developer = user.inTeam(celebrities.launchpad_developers)
launchbag.setDeveloper(is_developer)
def reset_login_in_launchbag_on_logout(event):
"""Subscriber for ILoggedOutEvent that sets 'login' in launchbag to None.
"""
launchbag = getUtility(IOpenLaunchBag)
launchbag.setLogin(None)
def reset_developer_in_launchbag_on_logout(event):
"""Subscriber for ILoggedOutEvent that resets the developer flag."""
launchbag = getUtility(IOpenLaunchBag)
launchbag.setDeveloper(False)
|