views:

104

answers:

2

Hi,

I would like to display the list of the authenticated users.

On the documentation: http://docs.djangoproject.com/en/dev/topics/auth/

class models.User is_authenticated()¶ Always returns True. This is a way to tell if the user has been authenticated. ...

You can know on the template side is the current User is authenticated or not:

{% if user.is_authenticated %} {% endif %}

But i didn't found the way the get the list of the authenticated users....

Any idea ?

Cheers,

Gerald

A: 

There is no easy, built-in way to do what you want that I know of. I'd try a combination of expiring sessions and filtering on last_login. Maybe even write a custom manager for that.

rz
+4  A: 

Going along with rz's answer, you could query the Session model for non-expired sessions, then turn the session data into users. Once you've got that you could turn it into a template tag which could render the list on any given page.

(This is all untested, but hopefully will be close to working).

Fetch all the logged in users...

from django.contrib.auth.models import User
from django.contrib.sessions.models import Session
from datetime import datetime

def get_all_logged_in_users():
    # Query all non-expired sessions
    sessions = Session.objects.filter(expire_date__gte=datetime.now())
    uid_list = []

    # Build a list of user ids from that query
    for session in sessions:
        data = session.get_decoded()
        uid_list.append(data.get('_auth_user_id', None))

    # Query all logged in users based on id list
    return User.objects.filter(id__in=uid_list)

Using this, you can make a simple inclusion template tag...

from django import template
from wherever import get_all_logged_in_users
register = template.Library()

@register.inclusion_tag('templatetags/logged_in_user_list.html')
def render_logged_in_user_list():
    return { 'users': get_all_logged_in_users() }

logged_in_user_list.html

{% if users %}
<ul class="user-list">
    {% for user in users %}
    <li>{{ user }}</li>
    {% endfor %}
</ul>
{% endif %}

Then in your main page you can simply use it where you like...

{% load your_library_name %}
{% render_logged_in_user_list %}

EDIT

For those talking about the 2-week persistent issue, I'm assuming that anyone wanting to have an "active users" type of listing will be making use of the SESSION_EXPIRE_AT_BROWSER_CLOSE setting, though I recognize this isn't always the case.

T. Stone
Thanks a lot, it was exactly what i was looking for.Gerald
Um, just so you know "non-expired sessions" will give you everyone who has been logged in for *two weeks*… Look at django-tracking instead if you would like currently active users.
jonwd7
same comment as @jonwd7. It is probably better to check for last_login or some more sophisticated filtering. 2 weeks hardly counts as "currently"
rz
You could probably throw together some middleware that sets a session variable to timestamp the most recent request for all users and then filter on those less than 5 minutes old. Then the above solution would work pretty well.
DrBloodmoney