From e6cc3a71a0c41c40e95b47d7f001a7d948dba9fc Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Thu, 5 Nov 2015 15:31:56 +0530 Subject: Allow single user session active at any given time --- yaksh/middleware/one_session_per_user.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 yaksh/middleware/one_session_per_user.py (limited to 'yaksh/middleware/one_session_per_user.py') diff --git a/yaksh/middleware/one_session_per_user.py b/yaksh/middleware/one_session_per_user.py new file mode 100644 index 0000000..d9e97bb --- /dev/null +++ b/yaksh/middleware/one_session_per_user.py @@ -0,0 +1,22 @@ +from django.contrib.auth.models import User +from django.contrib.sessions.models import Session + +from yaksh.models import Visitor + + +class OneSessionPerUserMiddleware(object): + def process_request(self, request): + if isinstance(request.user, User): + current_key = request.session.session_key + if hasattr(request.user, 'visitor'): + active_key = request.user.visitor.session_key + print active_key, current_key + if active_key != current_key: + Session.objects.filter(session_key=active_key).delete() + request.user.visitor.session_key = current_key + request.user.visitor.save() + else: + Visitor.objects.create( + visitor_user=request.user, + session_key=current_key, + ) \ No newline at end of file -- cgit