0

I am try to write a function that gets the username of the person logged and right after that changes de session_key into a new one.

What I have written so far is this:

def get_username(request):
    try:
        for coisa in request:
            session_key = coisa  # This seems to be wrong, but it works
        session = Session.objects.get(session_key=session_key)
        user_id = session.get_decoded().get('_auth_user_id')
        user = User.objects.get(id=user_id)   # Works very well until here, trust me
        session.cycle_key()     #this is where I'm getting the error
        return HttpResponse(user.username)
    except Exception, e:
        print 'Erro: ' + str(e)
        return HttpResponseServerError(str(e))

That request variable I'm passing is not the usual django request, it's a request built by a javascript script and has only the session_key on its data.

The error I'm getting is this: 'Session' object has no attribute 'cycle_key'

The session_key I'm using on this function came from a function that runs when the page is going to load, so that request is a "normal" request variable from django:

session_key = request.session.session_key
user2449798
  • 434
  • 1
  • 5
  • 13

2 Answers2

0

I don't understand why you're doing all this at all. The user is already in request.user, so there's no need to get it from the session, and similarly the session itself is in request.session - so you can just do request.session.cycle_key().

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • This get_username function will be used to authenticate and set the username of the person logged in on the chat server (which uses node and socket.io).My approach to this is getting the user id and session_key from django and compare them. If they came from the same user, then ok, we can continue, otherwise cancel all, but this function is called from the chat server so the request it sends is not the same as django. For this reason I can't simply do a `request.session.cycle_key()` or `request.user` – user2449798 Sep 20 '14 at 22:33
0

cycle_key() belongs to sessions.backends.db.SessionStore (inherited from SessionBase), and what you are trying to use here is models.Session, which is a session model. Normally request.session IIRC is an instance of SessionStore.

So you need to create a new SessionStore here. It initialized with session key:

session = SessionStore(session_key)
Rainy
  • 1,066
  • 8
  • 14
  • I tried it but this error: `'SessionStore' object has no attribute '_session_cache'`. Can I use the same session_key to get both objects? – user2449798 Sep 20 '14 at 22:43
  • Try to add, after creating, session._session_cache = session.load() . However, I'm not sure you're getting the right session key. Your code doesn't make much sense - you're iterating over the request and only using the last value in it to set as session key. – Rainy Sep 21 '14 at 02:05
  • Thanks, I'll try it. That loop is very strange indeed, but when I print all the results all I get is the key so it kind of works. – user2449798 Sep 21 '14 at 14:51
  • Yeah, but you're doing effectively an assignment to the last item, i.e. session_key = request[-1] – Rainy Sep 21 '14 at 17:48