from django.db.models.signals import post_save, post_delete from django.dispatch import receiver from .models import Period @receiver(post_save, sender=Period) def student_period_created_updated(sender, instance, created, **kwargs): if created and not instance.clocked_out: instance.student.is_clocked_in = True instance.student.current_period_id = instance.pk instance.student.save() elif not created and not instance.clocked_out: instance.student.is_clocked_in = True instance.student.current_period_id = instance.pk instance.student.save() elif instance.clocked_out and not created: instance.student.is_clocked_in = False instance.student.save() @receiver(post_delete, sender=Period) def student_period_deleted(sender, instance, **kwargs): if instance.student.current_period_id == instance.pk: instance.student.is_clocked_in = False instance.student.current_period_id = None instance.student.save()