Skip to content

Commit

Permalink
Merge pull request #32 from SELab-2/serializers_views
Browse files Browse the repository at this point in the history
Fully finished gebruikers, vakken, projecten and groepen
  • Loading branch information
sPAICEcake authored Mar 8, 2024
2 parents 3eddd2c + f02f9d2 commit a0c49d0
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ projectenv
migrations
.env
htmlcov
data
30 changes: 0 additions & 30 deletions api/models/signals.py

This file was deleted.

1 change: 0 additions & 1 deletion api/models/vak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 30 additions & 8 deletions api/serializers/gebruiker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from rest_framework import serializers
from api.models.gebruiker import Gebruiker
from api.utils import clear


class GebruikerSerializer(serializers.ModelSerializer):
Expand All @@ -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)
26 changes: 25 additions & 1 deletion api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
6 changes: 1 addition & 5 deletions api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__'
31 changes: 25 additions & 6 deletions api/serializers/vak.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from rest_framework import serializers
from api.models.vak import Vak
from api.utils import clear


class VakSerializer(serializers.ModelSerializer):
Expand All @@ -11,34 +10,41 @@ 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):
instance.name = validated_data.pop('name')

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

Expand All @@ -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)
5 changes: 0 additions & 5 deletions api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 0 additions & 6 deletions api/views/gebruiker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions api/views/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@
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':
serializer = GroepSerializer(data=request.data)
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):
Expand Down
12 changes: 10 additions & 2 deletions api/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
4 changes: 2 additions & 2 deletions api/views/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion api/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" }]})
return JsonResponse({"associatedApplications": [{ "applicationId": "239ce609-e362-4cf6-919f-97e6935ef5f5" }]})

0 comments on commit a0c49d0

Please sign in to comment.