Skip to content

Commit

Permalink
Merge pull request #319 from SELab-2/backend_extras
Browse files Browse the repository at this point in the history
Permissie breach oplossen bij groepen
  • Loading branch information
LGDTimtou authored May 23, 2024
2 parents f8f3f17 + da0a5e6 commit 368a2f9
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions api/views/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,25 @@ def groep_detail(request, id, format=None):
return Response(serializer.data)

if request.method in ["PUT", "PATCH"]:
if request.method == "PUT":
serializer = GroepSerializer(groep, data=request.data)
else:
serializer = GroepSerializer(groep, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
if has_permissions(request.user) or validate_new_students(request.user, groep, request.data):
if request.method == "PUT":
serializer = GroepSerializer(groep, data=request.data)
else:
serializer = GroepSerializer(groep, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_403_FORBIDDEN)

if has_permissions(request.user):
if request.method == "DELETE":
groep.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=status.HTTP_403_FORBIDDEN)


def validate_new_students(user, current_groep, data):
old = set(map(lambda student: student.user.id, current_groep.studenten.all()))
new = set(data.get('studenten'))
return (old - new).union(new - old) == {user.id}

0 comments on commit 368a2f9

Please sign in to comment.