Retrieving information stored in other sessions with gae-sessions
I made a simple login system with gae-sessions, and I want to show a logged in user how many users are logged in and who they are.
To count the number of people logged in, when I log a user in I immediately save the session to the datastore with save(persist_even_if_using_cookie=True). Then I use SessionModel.al开发者_如何学JAVAl().count() to retrieve the number of logged in accounts.
I'm having trouble retrieving information on other sessions though. I'm not sure how to do it. I tried this:
logged_in = []
for activesession in SessionModel.all():
logged_in.append(activesession['user'])
But I'm getting this error:
TypeError: 'SessionModel' object is unsubscriptable
I also tried activesession.get('user'), but it results in another error:
BadKeyError: Invalid string key user.
How can I do this?
The Session object and the SessionModel are separate from each other. SessionModel only stores the contents of the session, it can't be read from like a Session object.
I have a feeling that this is a bad idea, and you should find another way to store/retrieve the list of logged in users. This method may return expired sessions that haven't been deleted yet, and will probably be really slow.
The method you want to call is __decode_data. I think something like this will work:
for activesession in SessionModel.all():
data = Session._Session__decode_data(activesession.pdump)
logged_in.append(data['user'])
精彩评论