Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

django-tastypie sample authentication

By: a guest on May 20th, 2012  |  syntax: Python  |  size: 0.97 KB  |  views: 266  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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