On Thursday 02 March 2017 14:47:30 Branko Zivanovic wrote:
> I need to know online status for each user. How do I do that?
from importlib import import_module
from django.conf import settings
from django.contrib.auth import get_user_model
engine = import_module(settings.SESSION_ENGINE)
# docs
store = engine.SessionStore()
store.clear_expired()
logged_in = []
User = get_user_model()
for session in store.model.objects.all():
decoded = store.decode(session.session_data)
if '_auth_user_id' in decoded:
try:
user = User.objects.get(id=decoded['_auth_user_id']
logged_in.append(user)
except user.DoesNotExist:
pass
Of course, since by default the session length is 2 weeks, this doesn't tell you much, so if you want true "online status" you'd need to use your own session store to store the additional "last_online" field and middleware to update it automatically. Code above contains ample hints for you to start digging.
--
Melvyn Sopacua