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
|
The LaunchBag is a collection of all the 'stuff' we have traversed. It
contains the request's 'context' or environment, which can be used to
filter or otherwise specialize views or behavior.
First, we'll set up various imports and stub objects.
>>> from zope.component import getUtility
>>> from lp.services.webapp.interfaces import ILaunchBag
>>> from lp.services.webapp.interfaces import BasicAuthLoggedInEvent
>>> from lp.services.webapp.interfaces import LoggedOutEvent
>>> from lp.services.webapp.interfaces import \
... CookieAuthPrincipalIdentifiedEvent
>>> class Principal(object):
... id = 23
>>> principal = Principal()
>>> class Participation(object):
... principal = principal
... interaction = None
>>> class Response(object):
... def getCookie(self, name):
... return None
>>> class Request(object):
... principal = principal
... response = Response()
... cookies = {}
... def setPrincipal(self, principal):
... pass
>>> request = Request()
There have been no logins, so launchbag.login will be None.
>>> launchbag = getUtility(ILaunchBag)
>>> print launchbag.login
None
Let's send a basic auth login event.
>>> login = "foo.bar@canonical.com"
>>> event = BasicAuthLoggedInEvent(request, login, principal)
>>> from zope.event import notify
>>> notify(event)
Now, launchbag.login will be 'foo.bar@canonical.com'.
>>> print launchbag.login
foo.bar@canonical.com
Login should be set back to None on a logout.
>>> event = LoggedOutEvent(request)
>>> notify(event)
>>> print launchbag.login
None
'user' will also be set to None:
>>> print launchbag.user
None
Let's do a cookie auth principal identification. In this case, the login
will be cookie@example.com.
>>> event = CookieAuthPrincipalIdentifiedEvent(
... principal, request, 'cookie@example.com')
>>> notify(event)
>>> print launchbag.login
cookie@example.com
== time_zone ==
The time_zone attribute gives the user's time zone, as an pytz object.
>>> from lp.testing.factory import LaunchpadObjectFactory
>>> factory = LaunchpadObjectFactory()
>>> person = factory.makePerson()
>>> login_person(person)
>>> launchbag.time_zone
<UTC>
It's cached, so even if the user's time zone is changed, it will stay
the same. This is to optimize the look-up time, since some pages look it
up a lot of times.
>>> person.setLocation(
... launchbag.user.latitude,
... launchbag.user.longitude,
... 'Europe/Paris',
... launchbag.user)
>>> launchbag.time_zone
<UTC>
After the LaunchBag has been cleared, the correct time zone is returned.
>>> launchbag.clear()
>>> launchbag.time_zone
<... 'Europe/Paris' ...>
|