In certain circumstances I'd like to let the staff to kick out some users. My django 1.8 site stores sessions in redis.
I tried this solution in my view:
#will be removed in 1.9
from django.utils.importlib import import_module
@staff_member_required
def kickout_user(request, username):
u = User.objects.get(username = username)
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
active_users = Request.objects.active_users(seconds=60)
active_users_ids = [user.id for user in active_users]
for session in stored_sessions:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
s = SessionStore(session_key=session.session_key)
session_uid = session.get_decoded().get('_auth_user_id')
print 'session', session_uid
if session_uid == u.id:
print 'session going to be deleted for uid:', session_uid
session.delete()
print ' session deleted'+ u.username
But it gives this error:
global name 'Request' is not defined
There also some suggestions here, but they are either flawd or based writing additional middlewares that I find overkill and try to aovid.