From d594cf7214c277ab181792a36da90dd15c345d00 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 8 Apr 2024 15:14:38 +0200 Subject: [PATCH] chore: moved everything to signals --- backend/api/models/checks.py | 16 ---------------- backend/api/models/submission.py | 9 +-------- backend/api/signals.py | 30 ++++++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/backend/api/models/checks.py b/backend/api/models/checks.py index bfb0b6a4..975f46aa 100644 --- a/backend/api/models/checks.py +++ b/backend/api/models/checks.py @@ -2,10 +2,7 @@ from api.models.docker import DockerImage from api.models.extension import FileExtension from api.models.project import Project -from api.signals import run_extra_checks from django.db import models -from django.db.models.signals import post_save, pre_delete -from django.dispatch import receiver class StructureCheck(models.Model): @@ -91,16 +88,3 @@ class ExtraCheck(models.Model): blank=False, null=False ) - - -@receiver(post_save, sender=ExtraCheck) -@receiver(pre_delete, sender=ExtraCheck) -def run_checks(sender, instance: ExtraCheck, **kwargs): - for group in instance.project.groups.all(): - submissions = group.submissions.order_by("submission_time") - if submissions: - run_extra_checks.send(sender=ExtraCheck, submission=submissions[0]) - - for submission in submissions[1:]: - submission.is_valid = False - submission.save() diff --git a/backend/api/models/submission.py b/backend/api/models/submission.py index d8e9f44e..016f779b 100644 --- a/backend/api/models/submission.py +++ b/backend/api/models/submission.py @@ -2,10 +2,7 @@ get_submission_file_path) from api.models.checks import ExtraCheck from api.models.group import Group -from api.signals import run_extra_checks from django.db import models -from django.db.models.signals import post_save -from django.dispatch import receiver class Submission(models.Model): @@ -39,13 +36,9 @@ class Meta: # A group can only have one submission with a specific number unique_together = ("group", "submission_number") - -@receiver(post_save, sender=Submission) -def run_checks(sender, instance: Submission, **kwargs): - run_extra_checks.send(sender=Submission, submission=instance) +# TODO: We can use a FilePathField for this with allow_files = False and allow_folders = True and include it in Submission -# TODO: We can use a FilePathField for this with allow_files = False and allow_folders = True and include it in Submission class SubmissionFile(models.Model): """Model for a file that is part of a submission.""" diff --git a/backend/api/signals.py b/backend/api/signals.py index fec0206a..5b9e645b 100644 --- a/backend/api/signals.py +++ b/backend/api/signals.py @@ -1,10 +1,17 @@ +from api.models.checks import ExtraCheck from api.models.student import Student +from api.models.submission import Submission from authentication.models import User from authentication.signals import user_created +from django.db.models.signals import post_save, pre_delete from django.dispatch import Signal, receiver +# Signals +run_extra_checks = Signal() + +# Receivers + -# TODO: Is this a signal? @receiver(user_created) def user_creation(user: User, attributes: dict, **_): """Upon user creation, auto-populate additional properties""" @@ -14,11 +21,26 @@ def user_creation(user: User, attributes: dict, **_): Student(user_ptr=user, student_id=student_id).save_base(raw=True) -run_extra_checks = Signal() - - @receiver(run_extra_checks) def _run_extra_checks(submission, **kwargs): # TODO: Actually run the checks print("Running extra checks", flush=True) return True + + +@receiver(post_save, sender=ExtraCheck) +@receiver(pre_delete, sender=ExtraCheck) +def run_checks_extra_check(sender, instance: ExtraCheck, **kwargs): + for group in instance.project.groups.all(): + submissions = group.submissions.order_by("submission_time") + if submissions: + run_extra_checks.send(sender=ExtraCheck, submission=submissions[0]) + + for submission in submissions[1:]: + submission.is_valid = False + submission.save() + + +@receiver(post_save, sender=Submission) +def run_checks_submission(sender, instance: Submission, **kwargs): + run_extra_checks.send(sender=Submission, submission=instance)