LORDCHANNEL2.0/core/signals.py

32 lines
1.1 KiB
Python
Raw Normal View History

2021-03-13 18:36:50 +00:00
from .models import Profilo
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.shortcuts import HttpResponseRedirect
# Permette di creare un Profilo Custom dichiarato nel models.py
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
print("Created :", created)
if created:
Profilo.objects.create(user=instance)
# Permette di salvare il Profilo nel database
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profili.save()
# Permette di vedere se l'utente é loggato
@receiver(user_logged_in)
def got_online(sender, user, request, **kwargs):
user.profili.is_online = True
user.profili.save()
# Permette di vedere se l'utente non é loggato
@receiver(user_logged_out)
def got_offline(sender, user, request, **kwargs):
if request.user.is_authenticated:
user.profili.is_online = False
user.profili.save()