# HG changeset patch # User Brian Neal # Date 1352836206 21600 # Node ID f4c043cf55ac29d11d4e2f48a6b7141a02fe61f9 # Parent c6292e46e6170b6110db41d49d2e2ce24640dd82 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. diff -r c6292e46e617 -r f4c043cf55ac wiki/constants.py --- a/wiki/constants.py Mon Nov 12 16:40:54 2012 -0600 +++ b/wiki/constants.py Tue Nov 13 13:50:06 2012 -0600 @@ -2,5 +2,4 @@ """ -SESSION_SET_FLAG = 'wiki_set_cookie' SESSION_SET_MEMBER = 'wiki_redis_set_member' diff -r c6292e46e617 -r f4c043cf55ac wiki/middleware.py --- a/wiki/middleware.py Mon Nov 12 16:40:54 2012 -0600 +++ b/wiki/middleware.py Tue Nov 13 13:50:06 2012 -0600 @@ -12,7 +12,7 @@ import redis from core.services import get_redis_connection -from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER +from wiki.constants import SESSION_SET_MEMBER logger = logging.getLogger(__name__) @@ -100,13 +100,9 @@ def process_response(self, request, response): - if request.session.get(SESSION_SET_FLAG, False): - del request.session[SESSION_SET_FLAG] - + if hasattr(request, 'wiki_set_cookie'): create_wiki_session(request, response) - elif hasattr(request, 'wiki_delete_cookie'): - destroy_wiki_session(request.wiki_delete_cookie, response) return response diff -r c6292e46e617 -r f4c043cf55ac wiki/signals.py --- a/wiki/signals.py Mon Nov 12 16:40:54 2012 -0600 +++ b/wiki/signals.py Tue Nov 13 13:50:06 2012 -0600 @@ -1,11 +1,14 @@ """Signal handlers for wiki integration. +We are interested in hearing about users logging in and out, so we can create +and destroy an external cookie to allow access to the wiki. + """ import logging from django.contrib.auth.signals import user_logged_in, user_logged_out -from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER +from wiki.constants import SESSION_SET_MEMBER logger = logging.getLogger(__name__) @@ -18,7 +21,7 @@ """ logger.info('User login: %s', user.username) - request.session[SESSION_SET_FLAG] = True + request.wiki_set_cookie = True def logout_callback(sender, request, user, **kwargs): diff -r c6292e46e617 -r f4c043cf55ac wiki/tests.py --- a/wiki/tests.py Mon Nov 12 16:40:54 2012 -0600 +++ b/wiki/tests.py Tue Nov 13 13:50:06 2012 -0600 @@ -13,7 +13,7 @@ from core.services import get_redis_connection from wiki.middleware import WikiMiddleware -from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER +from wiki.constants import SESSION_SET_MEMBER class MiddleWareTestCase(TestCase): @@ -28,18 +28,20 @@ def tearDown(self): self.conn.delete(settings.WIKI_REDIS_SET) - def test_middleware(self): - + def create_request(self): request = self.factory.get('/contact/') request.session = {} request.user = self.user + return request + + def test_middleware(self): + + request = self.create_request() response = HttpResponse() - request.session[SESSION_SET_FLAG] = True + request.wiki_set_cookie = True response = self.mw.process_response(request, response) - self.assertIsNone(request.session.get('wiki_set_cookie')) - cookie = response.cookies.get(settings.WIKI_COOKIE_NAME) cookie_val = '' self.assertIsNotNone(cookie) @@ -74,6 +76,7 @@ # test the destroy session logic + request = self.create_request() request.wiki_delete_cookie = member response = self.mw.process_response(request, response)