comparison 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
comparison
equal deleted inserted replaced
626:a6bc1e2efa63 627:a4300639c6e7
10 10
11 from django.conf import settings 11 from django.conf import settings
12 import redis 12 import redis
13 13
14 from core.services import get_redis_connection 14 from core.services import get_redis_connection
15 from wiki.constants import SESSION_SET_FLAG, SESSION_SET_MEMBER
15 16
16 17
17 SESSION_KEY = 'wiki_redis_key'
18 logger = logging.getLogger(__name__) 18 logger = logging.getLogger(__name__)
19 19
20 20
21 def cookie_value(user, now): 21 def cookie_value(user, now):
22 """Creates the value for the external wiki cookie.""" 22 """Creates the value for the external wiki cookie."""
66 except redis.RedisError: 66 except redis.RedisError:
67 logger.error("Error adding wiki cookie key") 67 logger.error("Error adding wiki cookie key")
68 68
69 # Store the set member name in the session so we can delete it when the 69 # Store the set member name in the session so we can delete it when the
70 # user logs out: 70 # user logs out:
71 request.session[SESSION_KEY] = name 71 request.session[SESSION_SET_MEMBER] = name
72 72
73 73
74 def destroy_wiki_session(request, response): 74 def destroy_wiki_session(set_member, response):
75 """Destroys the session for the external wiki application. 75 """Destroys the session for the external wiki application.
76 76
77 Delete the external cookie. 77 Delete the external cookie.
78 Deletes the key from the Redis set as this entry is no longer valid. 78 Deletes the member from the Redis set as this entry is no longer valid.
79 79
80 """ 80 """
81 response.delete_cookie(settings.WIKI_COOKIE_NAME, 81 response.delete_cookie(settings.WIKI_COOKIE_NAME,
82 domain=settings.WIKI_COOKIE_DOMAIN) 82 domain=settings.WIKI_COOKIE_DOMAIN)
83 83
84 try: 84 if set_member:
85 key = request.session[SESSION_KEY] 85 conn = get_redis_connection()
86 except KeyError: 86 try:
87 # Hmmm, perhaps user logged in before this application was installed. 87 conn.zrem(settings.WIKI_REDIS_SET, set_member)
88 return 88 except redis.RedisError:
89 89 logger.error("Error deleting wiki cookie set member")
90 conn = get_redis_connection()
91 try:
92 conn.zrem(settings.WIKI_REDIS_SET, key)
93 except redis.RedisError:
94 logger.error("Error deleting wiki cookie key")
95
96 del request.session[SESSION_KEY]
97 90
98 91
99 class WikiMiddleware(object): 92 class WikiMiddleware(object):
100 """ 93 """
101 Check for flags set in the session to determine when to set or delete an 94 Check for flags set in the session to determine when to set or delete an
105 98
106 """ 99 """
107 100
108 def process_response(self, request, response): 101 def process_response(self, request, response):
109 102
110 if request.session.get('wiki_set_cookie', False): 103 if request.session.get(SESSION_SET_FLAG, False):
111 del request.session['wiki_set_cookie'] 104 del request.session[SESSION_SET_FLAG]
112 105
113 create_wiki_session(request, response) 106 create_wiki_session(request, response)
114 107
115 elif request.session.get('wiki_delete_cookie', False): 108 elif hasattr(request, 'wiki_delete_cookie'):
116 del request.session['wiki_delete_cookie']
117 109
118 destroy_wiki_session(request, response) 110 destroy_wiki_session(request.wiki_delete_cookie, response)
119 111
120 return response 112 return response