Skip to content

Commit

Permalink
Merge branch '29-model-fixes' into api_routes-implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
PJDeSmijter committed Mar 11, 2024
2 parents 1f3ca8a + e0e4235 commit 350ce8f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
15 changes: 14 additions & 1 deletion backend/pigeonhole/apps/groups/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from rest_framework import serializers
from django.core.exceptions import ValidationError

from backend.pigeonhole.apps.projects.models import Project
from backend.pigeonhole.apps.users.models import User
Expand All @@ -15,12 +16,24 @@ class Group(models.Model):

objects = models.Manager()

# a student can only be in one group per project
def clean(self):
for student in self.student.all():
existing_groups = Group.objects.filter(
project_id=self.project_id, student=student).exclude(
group_id=self.group_id)
if existing_groups.exists():
raise ValidationError(f"Student {student} is already part of "
"another group in this project.")

# a student can only be in one group per project, group_nr is
# automatically assigned and unique per project
def save(self, *args, **kwargs):
if not self.group_id:
if self.group_nr is None:
max_group_nr = Group.objects.filter(
project_id=self.project_id).aggregate(
models.Max('group_nr'))['group_nr__max'] or 0
models.Max('group_nr'))['group_nr__max'] or 0
self.group_nr = max_group_nr + 1
super().save(*args, **kwargs)

Expand Down
16 changes: 13 additions & 3 deletions backend/pigeonhole/apps/submissions/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.db import models
from rest_framework import serializers
from django.core.exceptions import ValidationError

from backend.pigeonhole.apps.groups.models import Group

Expand All @@ -10,16 +11,25 @@ class Submissions(models.Model):
group_id = models.ForeignKey(Group, on_delete=models.CASCADE, blank=False)
submission_nr = models.IntegerField()
file = models.FileField(upload_to='uploads/submissions/files/' +
str(group_id) + '/' + str(submission_nr) + '/',
str(group_id) + '/' + str(submission_nr) + '/',
null=True, blank=False, max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
output_test = models.FileField(upload_to='uploads/submissions/outputs/' +
str(group_id) + '/' + str(submission_nr) +
'/output_test/', null=True, blank=False,
str(group_id) + '/' + str(submission_nr) +
'/output_test/', null=True, blank=False,
max_length=255)

objects = models.Manager()

# submission_nr is automatically assigned and unique per group, and
# increments
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
if not self.submission_id:
max_submission_nr = Submissions.objects.filter(
group_id=self.group_id).aggregate(
models.Max('submission_nr'))['submission_nr__max'] or 0
self.submission_nr = max_submission_nr + 1
super().save(force_insert=force_insert, force_update=force_update, using=using, update_fields=update_fields)

class SubmissionsSerializer(serializers.ModelSerializer):
class Meta:
Expand Down
12 changes: 0 additions & 12 deletions backend/pigeonhole/tests/test_models/test_submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def setUp(self):
# Create submission
Submissions.objects.create(
group_id=group,
submission_nr=1,
)

def test_submission_student_relation(self):
Expand All @@ -67,17 +66,6 @@ def test_submission_project_relation(self):
project = submission.group_id.project_id
self.assertEqual(submission.group_id.project_id, project)

def test_update_and_delete_submission(self):
submission = Submissions.objects.get(submission_nr=1)
submission.submission_nr = 2
submission.save()
updated_submission = Submissions.objects.get(submission_nr=2)
self.assertEqual(updated_submission.submission_nr, 2)

submission.delete()
with self.assertRaises(Submissions.DoesNotExist):
Submissions.objects.get(submission_nr=2)

def test_submission_file_upload_and_retrieval(self):
submission = Submissions.objects.get(submission_nr=1)

Expand Down

0 comments on commit 350ce8f

Please sign in to comment.