Skip to content

Commit

Permalink
Merge pull request #37 from SELab-2/views-serializers
Browse files Browse the repository at this point in the history
Views serializers
  • Loading branch information
LGDTimtou authored Mar 11, 2024
2 parents 6ad52e3 + 8125d56 commit a3374f8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/models/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, default='0')

def __str__(self):
return self.tijdstip
Expand Down
9 changes: 8 additions & 1 deletion api/serializers/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 27 additions & 1 deletion api/serializers/project.py
Original file line number Diff line number Diff line change
@@ -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__'
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")
8 changes: 7 additions & 1 deletion api/serializers/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

20 changes: 18 additions & 2 deletions api/views/indiening.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@
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':
serializer = IndieningSerializer(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 indiening_detail(request, id, format=None):
Expand Down
16 changes: 16 additions & 0 deletions api/views/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down

0 comments on commit a3374f8

Please sign in to comment.