summaryrefslogtreecommitdiff
path: root/yaksh/middleware/one_session_per_user.py
blob: d9e97bbf23b459790c2659888f4ec3d1a1199a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,
                )