From 2f966bbf4a72a718119f37af4addd3971a474c4f Mon Sep 17 00:00:00 2001 From: Alexandre Paice Date: Sat, 9 Mar 2024 16:51:59 +0100 Subject: [PATCH 1/2] afwerken views --- api/models/indiening.py | 1 + api/views/indiening.py | 20 ++++++++++++++++++-- api/views/score.py | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/api/models/indiening.py b/api/models/indiening.py index da4a7228..dd483a31 100644 --- a/api/models/indiening.py +++ b/api/models/indiening.py @@ -6,6 +6,7 @@ class Indiening(models.Model): indiener = models.ForeignKey('Groep', on_delete=models.CASCADE) indieningsbestanden = models.FileField(upload_to='uploads/') tijdstip = models.DateTimeField(null=False) + project = models.ForeignKey('Project', on_delete=models.CASCADE) def __str__(self): return self.tijdstip diff --git a/api/views/indiening.py b/api/views/indiening.py index 2ca2dcff..9bf48e74 100644 --- a/api/views/indiening.py +++ b/api/views/indiening.py @@ -10,8 +10,23 @@ def indiening_list(request, format=None): if request.method == 'GET': - lesgevers = Indiening.objects.all() - serializer = IndieningSerializer(lesgevers, many=True) + indieningen = Indiening.objects.all() + + if "project" in request.GET: + try: + project = eval(request.GET.get('project')) + indieningen = indieningen.filter(project=project) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + if "groep" in request.GET: + try: + groep = eval(request.GET.get('groep')) + indieningen = indieningen.filter(indiener=groep) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + serializer = IndieningSerializer(indieningen, many=True) return Response(serializer.data) elif request.method == 'POST': @@ -19,6 +34,7 @@ def indiening_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 indiening_detail(request, id, format=None): diff --git a/api/views/score.py b/api/views/score.py index 0ade5e17..15d4c35f 100644 --- a/api/views/score.py +++ b/api/views/score.py @@ -11,6 +11,21 @@ def score_list(request, format=None): if request.method == 'GET': scores = Score.objects.all() + + if "indiening" in request.GET: + try: + indiening = eval(request.GET.get('indiening')) + scores = scores.filter(indiening=indiening) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + + if "groep" in request.GET: + try: + groep = eval(request.GET.get('groep')) + scores = scores.filter(groep=groep) + except NameError: + return Response(status=status.HTTP_400_BAD_REQUEST) + serializer = ScoreSerializer(scores, many=True) return Response(serializer.data) @@ -19,6 +34,7 @@ def score_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 score_detail(request, id, format=None): From f96406aae4276e16e75710085e05b689e015fc10 Mon Sep 17 00:00:00 2001 From: Alexandre Paice Date: Sun, 10 Mar 2024 13:49:25 +0100 Subject: [PATCH 2/2] serializers afgewerkt --- api/models/indiening.py | 2 +- api/serializers/indiening.py | 9 ++++++++- api/serializers/project.py | 28 +++++++++++++++++++++++++++- api/serializers/score.py | 8 +++++++- 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/api/models/indiening.py b/api/models/indiening.py index dd483a31..577062ba 100644 --- a/api/models/indiening.py +++ b/api/models/indiening.py @@ -6,7 +6,7 @@ class Indiening(models.Model): indiener = models.ForeignKey('Groep', on_delete=models.CASCADE) indieningsbestanden = models.FileField(upload_to='uploads/') tijdstip = models.DateTimeField(null=False) - project = models.ForeignKey('Project', on_delete=models.CASCADE) + project = models.ForeignKey('Project', on_delete=models.CASCADE, default='0') def __str__(self): return self.tijdstip diff --git a/api/serializers/indiening.py b/api/serializers/indiening.py index a52b760b..ce2cf94b 100644 --- a/api/serializers/indiening.py +++ b/api/serializers/indiening.py @@ -8,4 +8,11 @@ class Meta: fields = '__all__' def create(self, validated_data): - return Indiening.objects.create(**validated_data) + # indiener is een groep, dus zeker student(en) + indiening = Indiening.objects.create(**validated_data) + return indiening + + def update(self, instance, validated_data): + instance = Indiening.objects.create(**validated_data) + instance.save() + return instance diff --git a/api/serializers/project.py b/api/serializers/project.py index 35223c12..9b8d1a3a 100644 --- a/api/serializers/project.py +++ b/api/serializers/project.py @@ -1,8 +1,34 @@ from rest_framework import serializers from api.models.project import Project +from datetime import datetime + class ProjectSerializer(serializers.ModelSerializer): class Meta: model = Project - fields = '__all__' \ No newline at end of file + fields = '__all__' + + def create(self, validated_data): + deadline = validated_data.pop('deadline') + + validate_deadline(deadline) + + project = Project.objects.create(**validated_data) + project.deadline.set(deadline) + return project + + def update(self, instance, validated_data): + deadline = validated_data.pop('deadline') + + validate_deadline(deadline) + + instance = Project.objects.create(**validated_data) + instance.deadline.set(deadline) + instance.save() + return instance + + +def validate_deadline(deadline): + if deadline <= datetime.now(): + raise serializers.ValidationError("Deadline moet in de toekomst liggen") \ No newline at end of file diff --git a/api/serializers/score.py b/api/serializers/score.py index a7899445..060b98f5 100644 --- a/api/serializers/score.py +++ b/api/serializers/score.py @@ -8,5 +8,11 @@ class Meta: fields = '__all__' def create(self, validated_data): - return Score.objects.create(**validated_data) + score = Score.objects.create(**validated_data) + return score + + def update(self, instance, validated_data): + instance = Score.objects.create(**validated_data) + instance.save() + return instance