Update signal handling for Period model and auto-clockin for Student

This commit is contained in:
Nathan Chapman 2021-02-08 21:32:54 -07:00
parent 03642b3adc
commit df447059c3
4 changed files with 29 additions and 17 deletions

View File

@ -3,3 +3,6 @@ from django.apps import AppConfig
class AttendanceConfig(AppConfig): class AttendanceConfig(AppConfig):
name = 'attendance' name = 'attendance'
def ready(self):
import attendance.signals

View File

@ -2,8 +2,7 @@ from datetime import datetime
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from accounts.models import Student from accounts.models import Student
from django.db.models.signals import post_save
from django.dispatch import receiver
class Code(models.Model): class Code(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE) student = models.ForeignKey(Student, on_delete=models.CASCADE)
@ -48,10 +47,3 @@ class Period(models.Model):
if self.clocked_out != None: if self.clocked_out != None:
self.duration = self.clocked_out - self.clocked_in self.duration = self.clocked_out - self.clocked_in
super(Period, self).save(*args, **kwargs) super(Period, self).save(*args, **kwargs)
@receiver(post_save, sender=Period)
def auto_clockin_student(sender, instance, created, **kwargs):
if created:
instance.student.is_clocked_in = True
instance.student.current_period_id = instance.pk
instance.student.save()

24
attendance/signals.py Normal file
View File

@ -0,0 +1,24 @@
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()

View File

@ -12,6 +12,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMix
from django.utils import timezone from django.utils import timezone
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from accounts.models import Instructor, Student from accounts.models import Instructor, Student
from .models import Code, Period from .models import Code, Period
from .forms import AttendanceUpdateForm, PeriodForm from .forms import AttendanceUpdateForm, PeriodForm
@ -123,14 +124,6 @@ class PeriodUpdateView(LoginRequiredMixin, UpdateView):
form_class = PeriodForm form_class = PeriodForm
template_name = 'attendance/period_form.html' template_name = 'attendance/period_form.html'
# When editing a period, check if it matches the current period of the student and clock them out
def form_valid(self, form):
student = form.instance.student
if form.instance.id == student.current_period_id:
student.is_clocked_in = False
student.save()
return super().form_valid(form)
def get_success_url(self): def get_success_url(self):
pk = self.kwargs["pk"] pk = self.kwargs["pk"]
return reverse('period-detail', kwargs={'pk': pk}) return reverse('period-detail', kwargs={'pk': pk})