Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra validatie check voor groep groottes #134

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create(self, validated_data):
Gebruiker: De aangemaakte gebruiker.
"""

gepinde_vakken = validated_data.pop("gepinde_vakken")
gepinde_vakken = validated_data.pop("gepinde_vakken", [])
instance = Gebruiker.objects.create(**validated_data)
validate_gepinde_vakken(instance, gepinde_vakken)
instance.gepinde_vakken.set(gepinde_vakken)
Expand Down
16 changes: 12 additions & 4 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def create(self, validated_data):
Groep: De aangemaakte groep.
"""
students_data = validated_data.pop("studenten")
validate_students(students_data, validated_data["project"])
project = validated_data["project"]
validate_students(students_data, project)
validate_groep_grootte(students_data, project)
instance = Groep.objects.create(**validated_data)
instance.studenten.set(students_data)

Expand All @@ -48,11 +50,10 @@ def update(self, instance, validated_data):
Groep: De bijgewerkte groep.
"""
students_data = validated_data.pop("studenten", instance.studenten)
validate_students(
students_data, validated_data["project"], current_group=instance
)
new_project = validated_data.get("project", instance.project)
validate_students(students_data, new_project, current_group=instance)
validate_project(instance, new_project)
validate_groep_grootte(students_data, new_project)

super().update(instance=instance, validated_data=validated_data)
instance.studenten.set(students_data)
Expand Down Expand Up @@ -119,3 +120,10 @@ def validate_students(students_data, project, current_group=None):
raise serializers.ValidationError(
f"Student {student} zit al in een andere groep voor dit project!"
)


def validate_groep_grootte(studenten, project):
if len(studenten) > project.max_groep_grootte:
raise serializers.ValidationError(
f"Dit project heeft een maximum groep groote van {project.max_groep_grootte} studenten"
)
6 changes: 3 additions & 3 deletions api/serializers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ def create(self, validated_data):
Returns:
Score: De aangemaakte score.
"""

if Score.objects.filter(indiening=validated_data.get("indiening")).exists():
indiening = validated_data.get("indiening")
if Score.objects.filter(indiening=indiening).exists():
raise serializers.ValidationError(
"Deze indiening heeft al een bestaande score"
)
validate_score(validated_data)
validate_score(indiening, validated_data.get("score"))
return Score.objects.create(**validated_data)

def update(self, instance, validated_data):
Expand Down
Loading