No seu models.py da app Cadastro adicione o seguinte atributo:
class Cadastro(models.Model): user = models.OneToOneField(User, null=True, blank=True) ... session_key = models.CharField(max_length=100, null=True) ...
E na sua views de login edite conforme a necessidade, incluíndo as linhas abaixo:
... from django.contrib.auth import authenticate, logout, login as authlogin from django.contrib.sessions.backends.db import SessionStore ... u = authenticate(username=email, password=senha) if u is not None: if u.is_active: if u.cadastro.session_key: # check if user has session_key. This will be true for users logged in on another device try: s = SessionStore.objects.get(session_key=u.cadastro.session_key) except Session.DoesNotExist: pass else: s.delete() # delete the old session_key from db # set new session_key for user instance u.cadastro.session_key = request.session.session_key u.cadastro.save() # save the user u.save() authlogin(request, u) ...
hasta!
Nenhum comentário:
Postar um comentário