Advertisement
Guest User

django-tastypie sample authentication

a guest
May 20th, 2012
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. def is_authenticated(self, request, **kwargs):
  2.         from django.contrib.sessions.models import Session
  3.         if 'sessionid' in request.COOKIES:
  4.             try:
  5.                 s = Session.objects.get(pk=request.COOKIES['sessionid'])
  6.             except Exception,e:
  7.                 logger.warning("user with sessionid : %s not found" % request.COOKIES['sessionid'])
  8.                 return False
  9.  
  10.             # get the user_id from the session
  11.             if '_auth_user_id' in s.get_decoded():
  12.                 # try to find the user associated with the given sessionid
  13.                 try:
  14.                     u = User.objects.get(pk=s.get_decoded()['_auth_user_id'])
  15.                     if u is not None:
  16.                         return True
  17.                 except Exception,e:
  18.                     return False
  19.             else:
  20.                 return False
  21.         else:
  22.             logger.warning("sessionid is missing in the request")
  23.             return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement