1

Possible Duplicate:
How to get the list of the authenticated users?

Whenever I try to deploy a new version, I ask myself how many users are currently logged in. I usually check the "Users" model to see the time of my login vs others.

Is there a way I can get the currently logged in users in Admin page?

Thanks.

Community
  • 1
  • 1
lud0h
  • 2,370
  • 6
  • 33
  • 41
  • 2
    I don't think it's an exact duplicate. lud0h ask for "currently logged in" users, the other questions deals with all authenticated users, which is not quite the same. – jammon Mar 18 '11 at 12:51

2 Answers2

4

There is no easy answer. Since the HTTP protocoll is stateless, it has no concept of "logged in". Every authenticated user could possibly be logged in, until his session expires.

What you probably mean by "logged in user", is "every user who is currently using your service and might experience unwanted effects by the upgrade in the middle of his work". So you have to define logged in maybe like "somebody who accessed the site (or the admin pages) in the last 5 (or 10) minutes and has not explicitly logged out".

You could solve that by a middleware class like this (untested):

import datetime

class LastContactMiddleware:
    def process_request(self, request):
        if any_condition_you_like:
            request.session['last_contact'] = datetime.datetime.now()
        return request

Then you can query the session objects for the time limit you chose.

jammon
  • 3,404
  • 3
  • 20
  • 29
2

This thread looks promising

How to get the list of the authenticated users?

The is_active flag refers to enabled or disabled users And checking is_authenticated also doesn't work since in the case of long session expiry your user will appear logged in as long as his session is active.

Community
  • 1
  • 1
endre
  • 1,363
  • 1
  • 11
  • 22