From 6b8d25babbf9819caf3a2b807e4fb2bacf467958 Mon Sep 17 00:00:00 2001 From: ticoucke Date: Thu, 7 Mar 2024 12:23:05 +0100 Subject: [PATCH] Fully finished vakken. Vakken worden automatisch toegevoegd aan gebruikers en er worden validatiechecks gedaan dat lesgevers wel degelijk lesgevers zijn en students hetzelfde --- api/models/vak.py | 22 ++++++++++++++-------- api/serializers/vak.py | 26 ++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/api/models/vak.py b/api/models/vak.py index 47b56752..d9891c1b 100644 --- a/api/models/vak.py +++ b/api/models/vak.py @@ -1,5 +1,7 @@ from django.db import models -from django.core.exceptions import ValidationError +from django.db.models.signals import m2m_changed +from django.dispatch import receiver +from api.models.gebruiker import Gebruiker class Vak(models.Model): @@ -12,11 +14,15 @@ class Vak(models.Model): def __str__(self): return self.name - def save(self, *args, **kwargs): - if self.teachers.filter(is_lesgever=False).exists(): - raise ValidationError("Niet alle gebruikers in 'teachers' zijn lesgevers.") - if self.students.filter(is_lesgever=True).exists(): - raise ValidationError("Niet alle gebruikers in 'students' zijn studenten.") - - super(Vak, self).save(*args, **kwargs) +@receiver(m2m_changed, sender=Vak.students.through) +@receiver(m2m_changed, sender=Vak.teachers.through) +def update_gebruiker_subjects(sender, instance, action, **kwargs): + if action == 'post_add': + for gebruiker_id in kwargs['pk_set']: + gebruiker = Gebruiker.objects.get(pk=gebruiker_id) + gebruiker.subjects.add(instance) + if action == 'post_remove': + for gebruiker_id in kwargs['pk_set']: + gebruiker = Gebruiker.objects.get(pk=gebruiker_id) + gebruiker.subjects.remove(instance) \ No newline at end of file diff --git a/api/serializers/vak.py b/api/serializers/vak.py index 616156e8..9f37eba9 100644 --- a/api/serializers/vak.py +++ b/api/serializers/vak.py @@ -10,6 +10,9 @@ class Meta: def create(self, validated_data): students_data = validated_data.pop('students') teachers_data = validated_data.pop('teachers') + + validate_students_teachers(students_data, teachers_data) + projects_data = validated_data.pop('projects') vak = Vak.objects.create(**validated_data) vak.students.set(students_data) @@ -20,13 +23,15 @@ def create(self, validated_data): def update(self, instance, validated_data): instance.name = validated_data.pop('name') - students_data = validated_data.pop('students', []) teachers_data = validated_data.pop('teachers', []) + + validate_students_teachers(students_data, teachers_data) + projects_data = validated_data.pop('projects', []) - instance.students.clear() - instance.teachers.clear() - instance.projects.clear() + clear(instance.students) + clear(instance.teachers) + clear(instance.projects) instance.students.set(students_data) instance.teachers.set(teachers_data) @@ -35,3 +40,16 @@ def update(self, instance, validated_data): instance.save() return instance + +def validate_students_teachers(students_data, teachers_data): + for student in students_data: + if student.is_lesgever: + raise serializers.ValidationError("Alle gebruikers in 'students' moeten studenten zijn") + + for teacher in teachers_data: + if not teacher.is_lesgever: + raise serializers.ValidationError("Alle gebruikers in 'teachers' moeten lesgevers zijn") + +def clear(set): + for item in set.all(): + set.remove(item)