Skip to content

Commit

Permalink
Merge branch 'develop' into restricties
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoutAllaert committed Mar 30, 2024
2 parents 15c1192 + d0496a8 commit c68fd3a
Show file tree
Hide file tree
Showing 31 changed files with 849 additions and 141 deletions.
2 changes: 1 addition & 1 deletion api/models/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Indiening(models.Model):
indiening_id = models.AutoField(primary_key=True)
groep = models.ForeignKey("Groep", on_delete=models.CASCADE)
tijdstip = models.DateTimeField(auto_now_add=True)
status = models.BooleanField()
status = models.BooleanField(default=False)

def __str__(self):
return str(self.indiening_id)
Expand Down
2 changes: 1 addition & 1 deletion api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Project(models.Model):
opgave_bestand = models.FileField(upload_to=upload_to)
vak = models.ForeignKey(Vak, on_delete=models.CASCADE)
deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True)
extra_deadline = models.DateTimeField(null=True, blank=True, default=None)
max_score = models.IntegerField(default=20)
zichtbaar = models.BooleanField(default=True, blank=True)
gearchiveerd = models.BooleanField(default=False, blank=True)
Expand Down
8 changes: 4 additions & 4 deletions api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class GebruikerSerializer(serializers.ModelSerializer):
update(self, instance, validated_data): Werkt een bestaande gebruiker bij in de database.
"""

first_name = serializers.CharField(source='user.first_name', read_only=True)
last_name = serializers.CharField(source='user.last_name', read_only=True)
email = serializers.EmailField(source='user.email', read_only=True)
first_name = serializers.CharField(source="user.first_name", read_only=True)
last_name = serializers.CharField(source="user.last_name", read_only=True)
email = serializers.EmailField(source="user.email", read_only=True)

class Meta:
model = Gebruiker
fields = ['user', 'is_lesgever', 'first_name', 'last_name', 'email']
fields = ["user", "is_lesgever", "first_name", "last_name", "email"]

def create(self, validated_data):
"""
Expand Down
16 changes: 11 additions & 5 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from api.models.groep import Groep
from collections import Counter


class GroepSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -83,6 +84,15 @@ def validate_students(students_data, project, current_group=None):
serializers.ValidationError: Als een gebruiker geen student is of al in een andere groep voor dit project zit.
"""
groepen = Groep.objects.filter(project=project)
if current_group is not None:
groepen = groepen.exclude(groep_id=current_group.groep_id)

student_counts = Counter(students_data)
for student, count in student_counts.items():
if count > 1:
raise serializers.ValidationError(
f"Student {student} komt meerdere keren voor in de groep!"
)

for student in students_data:
if student.is_lesgever:
Expand All @@ -96,11 +106,7 @@ def validate_students(students_data, project, current_group=None):
)

for groep in groepen:
if (
current_group
and groep.groep_id != current_group.groep_id
and student in groep.studenten.all()
):
if student in groep.studenten.all():
raise serializers.ValidationError(
f"Student {student} zit al in een andere groep voor dit project!"
)
2 changes: 1 addition & 1 deletion api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def validate_deadlines(deadline, extra_deadline):
if deadline <= timezone.now():
raise serializers.ValidationError("Deadline moet in de toekomst liggen")

if extra_deadline <= deadline:
if extra_deadline is not None and extra_deadline <= deadline:
raise serializers.ValidationError(
"Extra deadline moet na de eerste deadline liggen"
)
Expand Down
1 change: 1 addition & 0 deletions api/serializers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def create(self, validated_data):
Returns:
Score: De aangemaakte score.
"""

if Score.objects.filter(indiening=validated_data.get("indiening")).exists():
raise serializers.ValidationError(
"Deze indiening heeft al een bestaande score"
Expand Down
11 changes: 9 additions & 2 deletions api/tests/factories/gebruiker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib.auth.models import User
from api.models.gebruiker import Gebruiker
from factory.django import DjangoModelFactory
from factory import SubFactory, Faker
from factory import SubFactory, Faker, PostGeneration


class UserFactory(DjangoModelFactory):
Expand All @@ -12,7 +12,7 @@ class Meta:
first_name = Faker("first_name")
last_name = Faker("last_name")
email = Faker("email")
is_superuser = Faker("boolean")
is_superuser = False


class GebruikerFactory(DjangoModelFactory):
Expand All @@ -21,3 +21,10 @@ class Meta:

user = SubFactory(UserFactory)
is_lesgever = Faker("boolean")

@PostGeneration # Use the PostGeneration decorator
def make_superuser(self, create, extracted, **kwargs):
if not create:
return
self.user.is_superuser = self.is_lesgever
self.user.save()
4 changes: 3 additions & 1 deletion api/tests/factories/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ def studenten(self, create, extracted, **kwargs):
for student in extracted:
self.studenten.add(student)
else:
self.studenten.add(GebruikerFactory(is_lesgever=False))
student = GebruikerFactory(is_lesgever=False)
self.project.vak.studenten.add(student)
self.studenten.add(student)
7 changes: 5 additions & 2 deletions api/tests/factories/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ class IndieningFactory(DjangoModelFactory):
class Meta:
model = Indiening

indiening_id = factory.Sequence(lambda n: n)
groep = SubFactory(GroepFactory)
tijdstip = factory.LazyFunction(
lambda: timezone.make_aware(
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
status = factory.Faker("boolean")

indiening_bestanden = factory.RelatedFactory(
"api.tests.factories.indiening.IndieningBestandFactory", "indiening"
)


class IndieningBestandFactory(DjangoModelFactory):
class Meta:
model = IndieningBestand

indiening_bestand_id = factory.Sequence(lambda n: n)
indiening = SubFactory(IndieningFactory)
bestand = FileField(filename="test.txt", data=b"file content")
8 changes: 7 additions & 1 deletion api/tests/factories/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ProjectFactory(DjangoModelFactory):
class Meta:
model = Project

project_id = factory.Sequence(lambda n: n)
titel = factory.Faker("word")
beschrijving = factory.Faker("paragraph")
opgave_bestand = factory.django.FileField(data=b"file content")
Expand All @@ -23,4 +22,11 @@ class Meta:
fake.date_time_between(start_date="+1d", end_date="+30d")
)
)
extra_deadline = factory.LazyFunction(
lambda: timezone.make_aware(
fake.date_time_between(start_date="+30d", end_date="+40d")
)
)
max_score = factory.Faker("random_int", min=10, max=100)
zichtbaar = factory.Faker("boolean")
gearchiveerd = factory.Faker("boolean")
11 changes: 9 additions & 2 deletions api/tests/factories/score.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from api.models.score import Score
from factory.django import DjangoModelFactory
from factory import SubFactory, Faker
from factory import SubFactory
from api.tests.factories.indiening import IndieningFactory
import random


class ScoreFactory(DjangoModelFactory):
class Meta:
model = Score

score = Faker("random_int", min=0, max=100)
indiening = SubFactory(IndieningFactory)

@classmethod
def _create(cls, model_class, *args, **kwargs):
indiening = kwargs.pop("indiening")
max_score = indiening.groep.project.max_score
kwargs["score"] = random.randint(0, max_score)
return super()._create(model_class, indiening=indiening, *args, **kwargs)
10 changes: 4 additions & 6 deletions api/tests/models/test_indiening.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# test_indiening.py
from django.test import TestCase
from django.core.files.uploadedfile import SimpleUploadedFile
from api.tests.factories.indiening import IndieningFactory, IndieningBestandFactory
Expand All @@ -17,12 +16,11 @@ def test_groep(self):
def test_tijdstip(self):
self.assertIsNotNone(self.indiening.tijdstip)

def test_indiening_bestand(self):
self.assertEqual(self.indiening.indieningbestand_set.count(), 0)
def test_status(self):
self.assertIsNotNone(self.indiening.status)

def test_indiening_bestand_add(self):
IndieningBestandFactory.create(indiening=self.indiening)
self.assertEqual(self.indiening.indieningbestand_set.count(), 1)
def test_indiening_bestanden(self):
self.assertEqual(self.indiening.indiening_bestanden.count(), 1)


class IndieningBestandModelTest(TestCase):
Expand Down
15 changes: 15 additions & 0 deletions api/tests/models/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,18 @@ def test_project_vak(self):

def test_project_max_score(self):
self.assertTrue(10 <= self.project.max_score <= 100)

def test_project_zichtbaar(self):
self.assertIsNotNone(self.project.zichtbaar)

def test_project_gearchiveerd(self):
self.assertIsNotNone(self.project.gearchiveerd)

def test_project_deadline(self):
self.assertIsNotNone(self.project.deadline)

def test_project_extra_deadline(self):
self.assertIsNotNone(self.project.extra_deadline)

def test_project_opgave_bestand(self):
self.assertEqual(self.project.opgave_bestand.read(), b"file content")
16 changes: 15 additions & 1 deletion api/tests/serializers/test_gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def setUp(self):

def test_contains_expected_fields(self):
data = self.serializer.data
self.assertCountEqual(data.keys(), ["user", "is_lesgever"])
self.assertCountEqual(
data.keys(), ["user", "is_lesgever", "first_name", "last_name", "email"]
)

def test_user_field_content(self):
data = self.serializer.data
Expand All @@ -22,6 +24,18 @@ def test_is_lesgever_field_content(self):
data = self.serializer.data
self.assertEqual(data["is_lesgever"], self.gebruiker.is_lesgever)

def test_first_name_field_content(self):
data = self.serializer.data
self.assertEqual(data["first_name"], self.user.first_name)

def test_last_name_field_content(self):
data = self.serializer.data
self.assertEqual(data["last_name"], self.user.last_name)

def test_email_field_content(self):
data = self.serializer.data
self.assertEqual(data["email"], self.user.email)

def test_create(self):
data = {"user": UserFactory.create().id, "is_lesgever": False}
serializer = GebruikerSerializer(data=data)
Expand Down
88 changes: 84 additions & 4 deletions api/tests/serializers/test_groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ def test_studenten_field_content(self):
self.assertEqual(data["studenten"], students)

def test_create(self):
studenten = [
GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)
]
for student in studenten:
self.groep.project.vak.studenten.add(student)
data = {
"project": self.groep.project.project_id,
"studenten": [
GebruikerFactory.create(is_lesgever=False).user.id for _ in range(3)
],
"studenten": studenten,
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
self.assertEqual(groep.project.project_id, data["project"])
self.assertEqual(
set([student.user.id for student in groep.studenten.all()]),
set(data["studenten"]),
)

def test_create_invalid_user_already_in_a_group(self):
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data = {
"project": self.groep.project.project_id,
"studenten": [student],
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
Expand All @@ -38,17 +57,78 @@ def test_create(self):
set([student.user.id for student in groep.studenten.all()]),
set(data["studenten"]),
)
new_data = {
"project": self.groep.project.project_id,
"studenten": [student],
}
newserializer = GroepSerializer(data=new_data)
self.assertTrue(newserializer.is_valid())
self.assertRaises(ValidationError, newserializer.save, raise_exception=True)

def test_create_invalid_user_not_in_vak(self):
student = GebruikerFactory.create(is_lesgever=False).user.id
data = {
"project": self.groep.project.project_id,
"studenten": [student],
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_create_invalid_user_is_teacher(self):
data = {
"project": self.groep.project.project_id,
"studenten": [
GebruikerFactory.create(is_lesgever=True).user.id for _ in range(3)
],
}
serializer = GroepSerializer(data=data)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_update(self):
data = self.serializer.data
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data["studenten"].append(student)
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
self.assertEqual(
[student.user.id for student in groep.studenten.all()], data["studenten"]
)

def test_update_invalid_user_already_in_this_group(self):
data = self.serializer.data
self.assertEqual(len(data["studenten"]), 1)
data["studenten"].append(GebruikerFactory.create(is_lesgever=False).user.id)
student = GebruikerFactory.create(is_lesgever=False).user.id
self.groep.project.vak.studenten.add(student)
data["studenten"].append(student)
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
self.assertEqual(
[student.user.id for student in groep.studenten.all()], data["studenten"]
)
new_data = self.serializer.data
new_data["studenten"].append(student)
serializer = GroepSerializer(instance=groep, data=new_data, partial=True)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_update_invalid_user_not_in_vak(self):
data = self.serializer.data
serializer = GroepSerializer(instance=self.groep, data=data, partial=True)
self.assertTrue(serializer.is_valid())
groep = serializer.save()
self.assertEqual(
[student.user.id for student in groep.studenten.all()], data["studenten"]
)
new_data = self.serializer.data
new_data["studenten"].append(GebruikerFactory.create(is_lesgever=False).user.id)
serializer = GroepSerializer(instance=groep, data=new_data, partial=True)
self.assertTrue(serializer.is_valid())
self.assertRaises(ValidationError, serializer.save, raise_exception=True)

def test_validation_for_blank_items(self):
serializer = GroepSerializer(data={"project": "", "studenten": []})
Expand Down
Loading

0 comments on commit c68fd3a

Please sign in to comment.