bgneal@626: """Signal handlers for wiki integration.
bgneal@626: 
bgneal@629: We are interested in hearing about users logging in and out, so we can create
bgneal@629: and destroy an external cookie to allow access to the wiki.
bgneal@629: 
bgneal@626: """
bgneal@626: import logging
bgneal@626: 
bgneal@626: from django.contrib.auth.signals import user_logged_in, user_logged_out
bgneal@626: 
bgneal@629: from wiki.constants import SESSION_SET_MEMBER
bgneal@626: 
bgneal@626: logger = logging.getLogger(__name__)
bgneal@626: 
bgneal@626: 
bgneal@626: def login_callback(sender, request, user, **kwargs):
bgneal@626:     """Signal callback function for a user logging in.
bgneal@626: 
bgneal@626:     Sets a flag for the middleware to create an external cookie.
bgneal@626: 
bgneal@626:     """
bgneal@626:     logger.info('User login: %s', user.username)
bgneal@626: 
bgneal@629:     request.wiki_set_cookie = True
bgneal@626: 
bgneal@626: 
bgneal@626: def logout_callback(sender, request, user, **kwargs):
bgneal@626:     """Signal callback function for a user logging in.
bgneal@626: 
bgneal@626:     Sets a flag for the middleware to delete the external cookie.
bgneal@626: 
bgneal@627:     Since the user is about to logout, her session will be wiped out after
bgneal@627:     this function returns. This forces us to set an attribute on the request
bgneal@627:     object so that the response middleware can delete the wiki's cookie.
bgneal@627: 
bgneal@626:     """
bgneal@630:     if user:
bgneal@630:         logger.info('User logout: %s', user.username)
bgneal@626: 
bgneal@630:         # Remember what Redis set member to delete by adding an attribute to the
bgneal@630:         # request object:
bgneal@630:         request.wiki_delete_cookie = request.session.get(SESSION_SET_MEMBER)
bgneal@626: 
bgneal@626: 
bgneal@923: user_logged_in.connect(login_callback, dispatch_uid='wiki.receivers.login')
bgneal@923: user_logged_out.connect(logout_callback, dispatch_uid='wiki.receivers.logout')