Skip to content

Commit

Permalink
Fully finished vakken. Vakken worden automatisch toegevoegd aan gebru…
Browse files Browse the repository at this point in the history
…ikers en er worden validatiechecks gedaan dat lesgevers wel degelijk lesgevers zijn en students hetzelfde
  • Loading branch information
ticoucke committed Mar 7, 2024
1 parent 3e3e602 commit 6b8d25b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
22 changes: 14 additions & 8 deletions api/models/vak.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
26 changes: 22 additions & 4 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit 6b8d25b

Please sign in to comment.