Last active
May 8, 2023 17:10
-
-
Save peterdemin/5829440 to your computer and use it in GitHub Desktop.
Revisions
-
peterdemin revised this gist
Jun 21, 2013 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ from one_session_per_user.models import User, Visitor from django.contrib.sessions.models import Session File renamed without changes. -
peterdemin created this gist
Jun 21, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ from myapp.models import User, Visitor from django.contrib.sessions.models import Session class OneSessionPerUserMiddleware(object): """http://stackoverflow.com/a/1814797""" 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( pupil=request.user, session_key=current_key, ) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ class Visitor(models.Model): pupil = models.OneToOneField(User, null=False) session_key = models.CharField(null=False, max_length=40)