changeset 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 c6292e46e617
children 63603e931503
files wiki/constants.py wiki/middleware.py wiki/signals.py wiki/tests.py
diffstat 4 files changed, 16 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- 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'
--- 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
--- 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):
--- 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)