Commits

luke  committed 8ceac6c

[project @ 376]
Tidied up use of thread locals and session for better encapsulation

  • Participants
  • Parent commits cb445f6

Comments (0)

Files changed (4)

File cciw/cciwmain/decorators.py

 from django.conf import settings
 from django.shortcuts import render_to_response
 
-from cciw.middleware.threadlocals import get_current_member, set_current_member
+from cciw.middleware.threadlocals import get_current_member, set_member_session
 from cciw.cciwmain.models import Permission, Member
 from cciw.cciwmain.common import standard_extra_context
 
         # The member data is correct; log in the member in and continue.
         else:
             if member.check_password(request.POST.get('password', '')):
-                request.session['member_id'] = member.user_name
                 member.last_seen = datetime.datetime.now()
                 member.save()
-                set_current_member(member)
+                set_member_session(request, member)
                 
                 if request.POST.has_key('post_data'):
                     post_data = _decode_post_data(request.POST['post_data'])

File cciw/cciwmain/views/memberadmin.py

 from django import forms
 from cciw.cciwmain.common import standard_extra_context
 from cciw.cciwmain.models import Member
-from cciw.middleware.threadlocals import set_current_member, get_current_member
+from cciw.middleware.threadlocals import set_member_session, set_current_userent_member, get_current_member
 from cciw.cciwmain.decorators import member_required
 from cciw.cciwmain import imageutils
 import md5
             else:
                 m.email = email
                 m.save()
-                request.session['member_id'] = m.user_name
-                set_current_member(m)
+                set_member_session(request, m)
                 c['stage'] = 'end'
         else:
             c['stage'] = 'invalid'
             c['error_message'] = msg
 
     ## Do this at end, so that the context_processors
-    ## are executed after set_current_member
+    ## are executed after set_member_session
     ctx = template.RequestContext(request, c)
 
     return shortcuts.render_to_response('cciw/members/signup.html', 

File cciw/cciwmain/views/members.py

 
 from cciw.cciwmain.models import Member, Message
 from cciw.cciwmain.common import standard_extra_context, get_order_option, create_breadcrumb
-from cciw.middleware.threadlocals import get_current_member
+from cciw.middleware.threadlocals import get_current_member, remove_member_session
 from cciw.cciwmain.decorators import member_required, same_member_required, member_required_for_post, _display_login_form
 from cciw.cciwmain.utils import get_member_link
 import cciw.cciwmain.templatetags.bbcode as bbcode
     if request.POST:
         if request.POST.has_key('logout'):
             try:
-                import cciw.middleware.threadlocals
-                del request.session['member_id']
-                del cciw.middleware.threadlocals._thread_locals.member
+                remove_member_session(request)
             except KeyError:
                 pass
         

File cciw/middleware/threadlocals.py

     return getattr(_thread_locals, 'member', None)
 
 def set_current_member(member):
-    # This is *very* rarely needed.
     _thread_locals.member = member
 
 def _get_member_from_request(request):
     except (KeyError, Member.DoesNotExist):
         return None
 
+def set_member_session(request, member):
+    request.session['member_id'] = member.user_name
+    set_current_member(member)
+
+def remove_member_session(request):
+    del request.session['member_id']
+    del _thread_locals.member
+
 class ThreadLocals(object):
     """Adds various objects to thread local storage from the request object."""
     def process_request(self, request):
-        _thread_locals.user = getattr(request, 'user', None)
+        set_current_user(getattr(request, 'user', None))
         
         member = _get_member_from_request(request)
         if member is not None: