From f02f9d290b8988b4ffc2a37c6f6bc4dc6f9938fb Mon Sep 17 00:00:00 2001 From: ticoucke Date: Thu, 7 Mar 2024 22:06:45 +0100 Subject: [PATCH] Fully finished gebruikers, vakken, projecten and groepen --- .gitignore | 1 + api/models/signals.py | 30 ---------------------------- api/models/vak.py | 1 - api/serializers/gebruiker.py | 38 ++++++++++++++++++++++++++++-------- api/serializers/groep.py | 26 +++++++++++++++++++++++- api/serializers/project.py | 6 +----- api/serializers/vak.py | 31 +++++++++++++++++++++++------ api/utils.py | 5 ----- api/views/gebruiker.py | 6 ------ api/views/groep.py | 21 ++++++++++++++++++-- api/views/project.py | 12 ++++++++++-- api/views/score.py | 4 ++-- api/views/views.py | 2 +- 13 files changed, 114 insertions(+), 69 deletions(-) delete mode 100644 api/models/signals.py diff --git a/.gitignore b/.gitignore index 9b92bf8e..46b3545e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ projectenv migrations .env htmlcov +data \ No newline at end of file diff --git a/api/models/signals.py b/api/models/signals.py deleted file mode 100644 index 417f639b..00000000 --- a/api/models/signals.py +++ /dev/null @@ -1,30 +0,0 @@ -from django.db.models.signals import m2m_changed -from django.dispatch import receiver -from api.models.gebruiker import Gebruiker -from api.models.vak import Vak - -@receiver(m2m_changed, sender=Gebruiker.subjects.through) -def update_gebruiker_subjects(sender, instance, action, **kwargs): - print('piemel') - if action == 'post_add': - print('hallo') - for vak_id in kwargs['pk_set']: - vak = Vak.objects.get(pk=vak_id) - vak.students.add(instance) - if action == 'post_remove': - for vak_id in kwargs['pk_set']: - vak = Vak.objects.get(pk=vak_id) - vak.students.remove(instance) - - -@receiver(m2m_changed, sender=Vak.students.through) -@receiver(m2m_changed, sender=Vak.teachers.through) -def update_subject_teachers_students(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/models/vak.py b/api/models/vak.py index 8c5813a0..bdc66f71 100644 --- a/api/models/vak.py +++ b/api/models/vak.py @@ -6,7 +6,6 @@ class Vak(models.Model): name = models.CharField(max_length=100) students = models.ManyToManyField('Gebruiker', related_name='vak_gebruikers', blank=True) teachers = models.ManyToManyField('Gebruiker', related_name='vak_lesgevers', blank=True) - projects = models.ManyToManyField('Project', related_name='vak_projecten', blank=True) def __str__(self): return self.name diff --git a/api/serializers/gebruiker.py b/api/serializers/gebruiker.py index 3cfebe4b..c2891f36 100644 --- a/api/serializers/gebruiker.py +++ b/api/serializers/gebruiker.py @@ -1,6 +1,5 @@ from rest_framework import serializers from api.models.gebruiker import Gebruiker -from api.utils import clear class GebruikerSerializer(serializers.ModelSerializer): @@ -10,15 +9,38 @@ class Meta: def create(self, validated_data): subjects_data = validated_data.pop('subjects') - student = Gebruiker.objects.create(**validated_data) - student.subjects.set(subjects_data) - return student + instance = Gebruiker.objects.create(**validated_data) + instance.subjects.set(subjects_data) + + populate_subject_gebruikers(instance) + + return instance def update(self, instance, validated_data): - subjects_data = validated_data.pop('subjects', None) - if subjects_data is not None: - clear(instance.subjects) - instance.subjects.set(subjects_data) + instance.is_lesgever = validated_data.pop('is_lesgever', False) + + + subjects_data = validated_data.pop('subjects', []) + + depopulate_subject_gebruikers(instance) + + instance.subjects.clear() + instance.subjects.set(subjects_data) + + populate_subject_gebruikers(instance) instance.save() return instance + + +def populate_subject_gebruikers(gebruiker): + for subject in gebruiker.subjects.all(): + if gebruiker.is_lesgever: + subject.teachers.add(gebruiker) + else: + subject.students.add(gebruiker) + +def depopulate_subject_gebruikers(gebruiker): + for subject in gebruiker.subjects.all(): + subject.students.remove(gebruiker) + subject.teachers.remove(gebruiker) \ No newline at end of file diff --git a/api/serializers/groep.py b/api/serializers/groep.py index 1a952555..a53984e7 100644 --- a/api/serializers/groep.py +++ b/api/serializers/groep.py @@ -8,4 +8,28 @@ class Meta: fields = '__all__' def create(self, validated_data): - return Groep.objects.create(**validated_data) + students_data = validated_data.pop('students') + + validate_students(students_data) + + instance = Groep.objects.create(**validated_data) + instance.students.set(students_data) + return instance + + def update(self, validated_data): + students_data = validated_data.pop('students') + + validate_students(students_data) + + instance = Groep.objects.create(**validated_data) + instance.students.set(students_data) + + instance.save() + return instance + + + +def validate_students(students_data): + for student in students_data: + if student.is_lesgever: + raise serializers.ValidationError("Alle gebruikers in 'students' moeten studenten zijn!") \ No newline at end of file diff --git a/api/serializers/project.py b/api/serializers/project.py index d398ddf9..35223c12 100644 --- a/api/serializers/project.py +++ b/api/serializers/project.py @@ -5,8 +5,4 @@ class ProjectSerializer(serializers.ModelSerializer): class Meta: model = Project - fields = '__all__' - - def create(self, validated_data): - return Project.objects.create(**validated_data) - + fields = '__all__' \ No newline at end of file diff --git a/api/serializers/vak.py b/api/serializers/vak.py index 02b88e1c..5f518e1f 100644 --- a/api/serializers/vak.py +++ b/api/serializers/vak.py @@ -1,6 +1,5 @@ from rest_framework import serializers from api.models.vak import Vak -from api.utils import clear class VakSerializer(serializers.ModelSerializer): @@ -11,14 +10,17 @@ class Meta: def create(self, validated_data): students_data = validated_data.pop('students') teachers_data = validated_data.pop('teachers') + projects_data = validated_data.pop('projects') validate_students_teachers(students_data, teachers_data) - projects_data = validated_data.pop('projects') vak = Vak.objects.create(**validated_data) vak.students.set(students_data) vak.teachers.set(teachers_data) vak.projects.set(projects_data) + + populate_student_teacher_vakken(vak) + return vak def update(self, instance, validated_data): @@ -26,19 +28,23 @@ def update(self, instance, validated_data): students_data = validated_data.pop('students', []) teachers_data = validated_data.pop('teachers', []) + projects_data = validated_data.pop('projects', []) validate_students_teachers(students_data, teachers_data) - projects_data = validated_data.pop('projects', []) - clear(instance.students) - clear(instance.teachers) - clear(instance.projects) + depopulate_student_teacher_vakken(instance) + + instance.students.clear() + instance.teachers.clear() + instance.projects.clear() instance.students.set(students_data) instance.teachers.set(teachers_data) instance.projects.set(projects_data) instance.save() + + populate_student_teacher_vakken(instance) return instance @@ -51,3 +57,16 @@ def validate_students_teachers(students_data, teachers_data): if not teacher.is_lesgever: raise serializers.ValidationError("Alle gebruikers in 'teachers' moeten lesgevers zijn") +def populate_student_teacher_vakken(vak): + for student in vak.students.all(): + student.subjects.add(vak) + + for teacher in vak.teachers.all(): + teacher.subjects.add(vak) + +def depopulate_student_teacher_vakken(vak): + for student in vak.students.all(): + student.subjects.remove(vak) + + for teacher in vak.teachers.all(): + teacher.subjects.remove(vak) \ No newline at end of file diff --git a/api/utils.py b/api/utils.py index eb32b536..d79a5acd 100644 --- a/api/utils.py +++ b/api/utils.py @@ -31,8 +31,3 @@ def get_graph_token(): return response.json() except: return None - - -def clear(set): - for item in set.all(): - set.remove(item) \ No newline at end of file diff --git a/api/views/gebruiker.py b/api/views/gebruiker.py index 63cbffce..b7d5a3e6 100644 --- a/api/views/gebruiker.py +++ b/api/views/gebruiker.py @@ -15,12 +15,6 @@ def gebruiker_list(request): if 'is_lesgever' in request.GET and request.GET.get('is_lesgever').lower() in ['true', 'false']: gebruikers = gebruikers.filter(is_lesgever = (request.GET.get('is_lesgever').lower() == 'true')) - if 'heeft_vak' in request.GET: - try: - vak = eval(request.GET.get('heeft_vak')) - gebruikers = gebruikers.filter(subjects=vak) - except NameError: - pass serializer = GebruikerSerializer(gebruikers, many=True) return Response(serializer.data) diff --git a/api/views/groep.py b/api/views/groep.py index 4cfbac8e..e3ba6984 100644 --- a/api/views/groep.py +++ b/api/views/groep.py @@ -10,8 +10,24 @@ def groep_list(request, format=None): if request.method == 'GET': - lesgevers = Groep.objects.all() - serializer = GroepSerializer(lesgevers, many=True) + groepen = Groep.objects.all() + + if "project" in request.GET: + try: + project = eval(request.GET.get('project')) + groepen = groepen.filter(project=project) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + if "student" in request.GET: + try: + student = eval(request.GET.get('student')) + groepen = groepen.filter(students=student) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + + serializer = GroepSerializer(groepen, many=True) return Response(serializer.data) elif request.method == 'POST': @@ -19,6 +35,7 @@ def groep_list(request, format=None): if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @api_view(['GET', 'PUT', 'DELETE']) def groep_detail(request, id, format=None): diff --git a/api/views/project.py b/api/views/project.py index 5f34eef6..21e40e47 100644 --- a/api/views/project.py +++ b/api/views/project.py @@ -10,8 +10,16 @@ def project_list(request, format=None): if request.method == 'GET': - lesgevers = Project.objects.all() - serializer = ProjectSerializer(lesgevers, many=True) + projects = Project.objects.all() + + if 'vak' in request.GET: + try: + vak = eval(request.GET.get('vak')) + projects = projects.filter(vak=vak) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + serializer = ProjectSerializer(projects, many=True) return Response(serializer.data) elif request.method == 'POST': diff --git a/api/views/score.py b/api/views/score.py index 11c88727..0ade5e17 100644 --- a/api/views/score.py +++ b/api/views/score.py @@ -10,8 +10,8 @@ def score_list(request, format=None): if request.method == 'GET': - lesgevers = Score.objects.all() - serializer = ScoreSerializer(lesgevers, many=True) + scores = Score.objects.all() + serializer = ScoreSerializer(scores, many=True) return Response(serializer.data) elif request.method == 'POST': diff --git a/api/views/views.py b/api/views/views.py index d5c2c98a..ed629af6 100644 --- a/api/views/views.py +++ b/api/views/views.py @@ -24,4 +24,4 @@ def home(request): return Response(data=API_URLS) def microsoft_association(request): - return JsonResponse({"associatedApplications": [{ "applicationId": "239ce609-e362-4cf6-919f-97e6935ef5f5" }]}) \ No newline at end of file + return JsonResponse({"associatedApplications": [{ "applicationId": "239ce609-e362-4cf6-919f-97e6935ef5f5" }]})