diff wiki/middleware.py @ 627:a4300639c6e7

Wiki integration. Create task to delete old cookie records. Rework logic upon logout, as session will not be available. Set an attribute on the request instead.
author Brian Neal <bgneal@gmail.com>
date Mon, 12 Nov 2012 15:10:52 -0600
parents a6bc1e2efa63
children f4c043cf55ac
line wrap: on
line diff
--- a/wiki/middleware.py	Wed Nov 07 20:17:33 2012 -0600
+++ b/wiki/middleware.py	Mon Nov 12 15:10:52 2012 -0600
@@ -12,9 +12,9 @@
 import redis
 
 from core.services import get_redis_connection
+from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER
 
 
-SESSION_KEY = 'wiki_redis_key'
 logger = logging.getLogger(__name__)
 
 
@@ -68,32 +68,25 @@
 
     # Store the set member name in the session so we can delete it when the
     # user logs out:
-    request.session[SESSION_KEY] = name
+    request.session[SESSION_SET_MEMBER] = name
 
 
-def destroy_wiki_session(request, response):
+def destroy_wiki_session(set_member, response):
     """Destroys the session for the external wiki application.
 
     Delete the external cookie.
-    Deletes the key from the Redis set as this entry is no longer valid.
+    Deletes the member from the Redis set as this entry is no longer valid.
 
     """
     response.delete_cookie(settings.WIKI_COOKIE_NAME,
                            domain=settings.WIKI_COOKIE_DOMAIN)
 
-    try:
-        key = request.session[SESSION_KEY]
-    except KeyError:
-        # Hmmm, perhaps user logged in before this application was installed.
-        return
-
-    conn = get_redis_connection()
-    try:
-        conn.zrem(settings.WIKI_REDIS_SET, key)
-    except redis.RedisError:
-        logger.error("Error deleting wiki cookie key")
-
-    del request.session[SESSION_KEY]
+    if set_member:
+        conn = get_redis_connection()
+        try:
+            conn.zrem(settings.WIKI_REDIS_SET, set_member)
+        except redis.RedisError:
+            logger.error("Error deleting wiki cookie set member")
 
 
 class WikiMiddleware(object):
@@ -107,14 +100,13 @@
 
     def process_response(self, request, response):
 
-        if request.session.get('wiki_set_cookie', False):
-            del request.session['wiki_set_cookie']
+        if request.session.get(SESSION_SET_FLAG, False):
+            del request.session[SESSION_SET_FLAG]
 
             create_wiki_session(request, response)
 
-        elif request.session.get('wiki_delete_cookie', False):
-            del request.session['wiki_delete_cookie']
+        elif hasattr(request, 'wiki_delete_cookie'):
 
-            destroy_wiki_session(request, response)
+            destroy_wiki_session(request.wiki_delete_cookie, response)
 
         return response