Skip to content

Commit

Permalink
chore: moved everything to signals
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Apr 8, 2024
1 parent d34a9b9 commit d594cf7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
16 changes: 0 additions & 16 deletions backend/api/models/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
9 changes: 1 addition & 8 deletions backend/api/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."""

Expand Down
30 changes: 26 additions & 4 deletions backend/api/signals.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand All @@ -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)

0 comments on commit d594cf7

Please sign in to comment.