tags:

views:

23

answers:

1

I have the following 4 lines of code that I keep reusing in my django views. I would like a function that passes the final value (All the objects of a logged in user) to my other functions and how to call that. As a fix, I have to keep using this lines in all my functions.

sessionkey = request.session.session_key
session = Session.objects.get(session_key=sessionkey)
uid = session.get_decoded().get('_auth_user_id')
user = UserProfile.objects.get(pk=uid)

Thanks

+1  A: 
def get_userprofile_from_session_via_request(request):
  sessionkey = request.session.session_key
  session = Session.objects.get(session_key=sessionkey)
  uid = session.get_decoded().get('_auth_user_id')
  user = UserProfile.objects.get(pk=uid)
  return user

Of course, I'm not sure why you wouldn't simplify this to:

def get_userprofile_from_session_via_request(request):
  user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
  return user
Ignacio Vazquez-Abrams
how do i call the user.is_staff. When I try `get_userprofile_from_session_via_request.user.is_staff` I get an error stating 'function' object has no attribute 'user'
It's a function. Call it, passing the `request` from the view as the first argument.
Ignacio Vazquez-Abrams