On Fri, Mar 03, 2017 at 01:19:18AM +0100, Melvyn Sopacua wrote:
> 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[1] 
> 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.

Keep in mind that this will only work if you use a model-based session
storage; if you use a storage like the signed_cookies session backend,
this will not be possible.

Cheers,

Michal

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20170303082823.GB23772%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Digital signature

Reply via email to