summaryrefslogtreecommitdiff
path: root/yaksh/middleware/one_session_per_user.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/middleware/one_session_per_user.py')
-rw-r--r--yaksh/middleware/one_session_per_user.py22
1 files changed, 22 insertions, 0 deletions
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