def test_post_should_logout_logged_in_user(self): user = create_user() user_logged_out.connect(self.signal_receiver) request = self.factory.post(user=user) response = self.view(request) self.assert_status_equal(response, status.HTTP_200_OK) self.assertEqual(response.data, None) self.assertTrue(self.signal_sent)
def test_none_token_model_results_in_no_operation(self): user = create_user() user_logged_out.connect(self.signal_receiver) request = self.factory.post(user=user) response = self.view(request) self.assert_status_equal(response, status.HTTP_204_NO_CONTENT) self.assertEqual(response.data, None) self.assertFalse(self.signal_sent)
class LoggedUser(models.Model): user = models.ForeignKey(User, primary_key=True, on_delete=models.CASCADE) def __unicode__(self): return self.user.username def login_user(sender, request, user, **kwargs): LoggedUser(user=user).save() def logout_user(sender, request, user, **kwargs): try: u = LoggedUser.objects.get(user=user) u.delete() except LoggedUser.DoesNotExist: pass user_logged_in.connect(login_user) user_logged_out.connect(logout_user)
class UserSession(models.Model): user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) session = models.ForeignKey(Session, on_delete=models.SET_NULL, null=True) loggedin = models.DateTimeField(auto_now_add=True) loggedout = models.DateTimeField(default=None, null=True) headers = JSONField(encoder=SafeEncoder, default=dict) def __str__(self): return self.user.username def login_user(sender, request, user, **kwargs): """Log User Session creation""" UserSession(user=user, session_id=request.session.session_key, headers=request.META).save() def logout_user(sender, request, user, **kwargs): """Log User Session destruction""" try: u = UserSession.objects.get(user=user, session_id=request.session.session_key) u.loggedout = timezone.now() u.save() # u.delete() except UserSession.DoesNotExist: pass # Event binding: user_logged_in.connect(login_user) user_logged_out.connect(logout_user) def elapsed(self): """Elapsed time since logged in""" if self.loggedout: return self.loggedout - self.loggedin else: return timezone.now() - self.loggedin def is_active(self): """Is the session active""" return (self.loggedout is None) or not (self.session_id is None)
class UserProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) login_count = models.PositiveIntegerField(default=0) time_spent = models.FloatField(default=0) # in minutes login_status = models.BooleanField(default=False) def login_user(sender, request, user, **kwargs): user.userprofile.login_count = user.userprofile.login_count + 1 user.userprofile.login_status = True user.userprofile.save() def logout_user(sender, request, user, **kwargs): user.userprofile.login_status = False # time difference between logout time and last login is added to time spent in minutes user.userprofile.time_spent = (timezone.now() - user.last_login).seconds / 60 user.userprofile.save() user_logged_in.connect(login_user) user_logged_out.connect(logout_user)
'text': json.dumps({ 'username': self.user.username, 'activity_type': 'notification', 'activity': activity }) }) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) def save_user_profile(sender, instance, **kwargs): instance.profile.save() def on_user_login(sender, **kwargs): Profile.objects.get(user=kwargs['user']).notify_login() def on_user_logout(sender, **kwargs): Profile.objects.get(user=kwargs['user']).notify_logout() post_save.connect(create_user_profile, sender=User) post_save.connect(save_user_profile, sender=User) user_logged_in.connect(on_user_login, sender=User) user_logged_out.connect(on_user_logout, sender=User)
context_id = models.CharField(max_length=512, null=True) class Meta: unique_together = ( 'badge_user', 'context_id', ) #delete when user logs out def delete_user_context_id(**kwargs): UserCurrentContextId.objects.filter( badge_user=kwargs['user']).all().delete() user_logged_out.connect(delete_user_context_id) class LtiClient(models.Model): date_created = models.DateTimeField(default=timezone.now) name = models.CharField(max_length=400, default='', blank=True, null=True) is_active = models.BooleanField(default=True) issuer = models.ForeignKey(Issuer, related_name='lti_client', on_delete=models.CASCADE) consumer_key = models.CharField(max_length=50, default=get_uuid) shared_secret = models.CharField(max_length=50, default=get_uuid)
from django.db import models from django.contrib.auth import user_logged_in, user_logged_out from actstream import action def user_action_in(sender, user, request, **kwargs): action.send(user, verb=u'log in') user_logged_in.connect(user_action_in) def user_action_out(sender, user, request, **kwargs): action.send(user, verb=u'log out') user_logged_out.connect(user_action_out) # #sample # def post_save_action(sender, **kwargs): # action.send(kwargs['instance'], verb=u'a user created') # post_save.connect(post_save_action, sender=User) class Player(models.Model): state = models.IntegerField(default=0) def __unicode__(self): return '#%d' % self.pk
""" Do something with this user here when user login success """ pass def listen_logged_out(sender, user=None, **kwargs): """ Do something with user here when user logout """ pass user_login_failed.connect(listener_login_failed) user_logged_in.connect(listener_logged_in) user_logged_out.connect(listen_logged_out) class AuthViewSet(BaseViewSet, UserLoginMixin): view_set = 'auth' serializer_class = UsernameAuthSerializer def create(self, request): """ @apiVersion 1.0.0 @api {post} /auth Login @apiName Authenticate @apiGroup VMS_API Account @apiPermission none @apiHeader {number} Type Device type (1: Mobile, 2: Android phone, 3: IOS phone, 4: Window phone, 5: Android tablet, 6: IOS tablet, 7: Mobile web, tablet web, 8: Desktop web)
from django.contrib import messages from django.contrib.auth import user_logged_out from django.db.models.signals import post_save from django.contrib.auth.models import User from django.dispatch import receiver from .models import Profile @receiver(post_save, sender=User) def create_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_profile(sender, instance, **kwargs): instance.profile.save() def show_message(sender, user, request, **kwargs): messages.info(request, 'You have been logged out.') user_logged_out.connect(show_message)
logUsuario.passw = userInstance.password logUsuario.nombre_usuario = user logUsuario.es_super_usuario = userInstance.is_superuser logUsuario.primer_nombre = userInstance.first_name logUsuario.segundo_nombre = userInstance.last_name logUsuario.email = userInstance.email logUsuario.estado_login = userInstance.is_active logUsuario.tipo_login = "******" logUsuario.dia = hoydia() logUsuario.hora = hoyhora() logUsuario.save() usuario = AuthUser.objects.get(id=userInstance.id) usuario.login = False usuario.save() user_logged_out.connect(do_loginOut) def hoydia(): ahora=datetime.datetime.now() hoy=ahora.date() return hoy def hoyhora(): ahora=datetime.datetime.now() hora=ahora.time() a_hora=str(hora) a_hora=a_hora[:8] return a_hora ################################## registro de usuarios