tags:

views:

446

answers:

4

Is there any way to track active users in django?

I have seen a few online solutions to the problem:

http://magicpc.wordpress.com/2009/09/22/get-online-users-in-django/

being one of those. But it seems rather odd to modify the model structure just to solve this simple problem. I would like to know if there is any quick solution around.

The last_login field in django auth is also not of much use since it only records the time when a user logs in and the field is not updated if the user has been in session for a long time

A: 

Don't think of modifying the user table structure as a "framework patch", think of it as extending the functionality of something existent.

You can't do it without storing the activities somewhere, and the user table seems to be the most logical place to store it.

Prody
+1  A: 

Modifying the auth User model is probably not a very "flexible" approach.

The simplest way to go about it would probably be to provide a profile - Django has a builtin support for providing a user profile model for storing additional user-related data without needing to modify any of the builtin models.

See Django manual

You would simply store the last active time in the user's profile.

Jani Hartikainen
+1  A: 

I agree that the best way to do this would be to use a profile. This would have a foreign key to the Users table rather than modifying the existing table.

In terms of how to record the user actions, one pattern might be to write a decorator that wraps all the views you want to record. The decorator would simply record the time and the action against the relevant user's profile.

Daniel Roseman
+1  A: 

Hi, I'm the one who wrote that blog :)

I didn't modify the User model, I extended it, it is the same as profiles, but I wanted to make it in a way that if you have already defined a profile you can still use my method.

I think extending the User model is the best way to handle it, I don't know if there are any points against it.

mpcabd
I am already using an extended model. What I meant to say was I needed to directly get the active user status without adding "last_activity_ip" and "last_activity_date" to the model. But your solution seems to be the the best way to go for in such a situation.
Saurabh