annotate wiki/signals.py @ 629:f4c043cf55ac
Wiki integration. Requests don't always have sessions.
In particular this occurs when a request is made without a trailing slash.
The Common middleware redirects when this happens, and the middleware
process_request() processing stops before a session can get added.
So just set an attribute on the request object for each operation.
This seemed weird to me at first, but there are plenty of examples of this
in the Django code base already.
author |
Brian Neal <bgneal@gmail.com> |
date |
Tue, 13 Nov 2012 13:50:06 -0600 |
parents |
a4300639c6e7 |
children |
63603e931503 |
rev |
line source |
bgneal@626
|
1 """Signal handlers for wiki integration.
|
bgneal@626
|
2
|
bgneal@629
|
3 We are interested in hearing about users logging in and out, so we can create
|
bgneal@629
|
4 and destroy an external cookie to allow access to the wiki.
|
bgneal@629
|
5
|
bgneal@626
|
6 """
|
bgneal@626
|
7 import logging
|
bgneal@626
|
8
|
bgneal@626
|
9 from django.contrib.auth.signals import user_logged_in, user_logged_out
|
bgneal@626
|
10
|
bgneal@629
|
11 from wiki.constants import SESSION_SET_MEMBER
|
bgneal@626
|
12
|
bgneal@626
|
13 logger = logging.getLogger(__name__)
|
bgneal@626
|
14
|
bgneal@626
|
15
|
bgneal@626
|
16 def login_callback(sender, request, user, **kwargs):
|
bgneal@626
|
17 """Signal callback function for a user logging in.
|
bgneal@626
|
18
|
bgneal@626
|
19 Sets a flag for the middleware to create an external cookie.
|
bgneal@626
|
20
|
bgneal@626
|
21 """
|
bgneal@626
|
22 logger.info('User login: %s', user.username)
|
bgneal@626
|
23
|
bgneal@629
|
24 request.wiki_set_cookie = True
|
bgneal@626
|
25
|
bgneal@626
|
26
|
bgneal@626
|
27 def logout_callback(sender, request, user, **kwargs):
|
bgneal@626
|
28 """Signal callback function for a user logging in.
|
bgneal@626
|
29
|
bgneal@626
|
30 Sets a flag for the middleware to delete the external cookie.
|
bgneal@626
|
31
|
bgneal@627
|
32 Since the user is about to logout, her session will be wiped out after
|
bgneal@627
|
33 this function returns. This forces us to set an attribute on the request
|
bgneal@627
|
34 object so that the response middleware can delete the wiki's cookie.
|
bgneal@627
|
35
|
bgneal@626
|
36 """
|
bgneal@626
|
37 logger.info('User logout: %s', user.username)
|
bgneal@626
|
38
|
bgneal@627
|
39 # Remember what Redis set member to delete by adding an attribute to the
|
bgneal@627
|
40 # request object:
|
bgneal@627
|
41 request.wiki_delete_cookie = request.session.get(SESSION_SET_MEMBER)
|
bgneal@626
|
42
|
bgneal@626
|
43
|
bgneal@626
|
44 user_logged_in.connect(login_callback, dispatch_uid='wiki.signals.login')
|
bgneal@626
|
45 user_logged_out.connect(logout_callback, dispatch_uid='wiki.signals.logout')
|