Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permissies aanpassen #317

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions api/serializers/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ def update(self, instance, validated_data):
Groep: De bijgewerkte groep.
"""
students_data = validated_data.pop("studenten", instance.studenten.all())
new_project = validated_data.get("project", instance.project)
validate_students(students_data, new_project, current_group=instance)
validate_project(instance, new_project)
validate_groep_grootte(students_data, new_project)
validated_data.pop("project", instance.project)
validate_students(students_data, instance.project, current_group=instance)
validate_groep_grootte(students_data, instance.project)

super().update(instance=instance, validated_data=validated_data)
instance.studenten.set(students_data)
Expand All @@ -62,24 +61,6 @@ def update(self, instance, validated_data):
return instance


def validate_project(instance, new_project):
"""
Valideert of het project van een groep niet kan worden aangepast.

Args:
instance: De huidige instantie van het project.
new_project: Het nieuwe project waaraan de groep wil worden gekoppeld.

Raises:
serializers.ValidationError: Wordt opgegooid als het project van een groep wordt aangepast.
"""

if instance.project != new_project:
raise serializers.ValidationError(
"Het project van een groep kan niet aangepast worden"
)


def validate_students(students_data, project, current_group=None):
"""
Controleert of de opgegeven gebruikers studenten zijn en of ze al in een andere groep voor dit project zitten.
Expand Down
23 changes: 11 additions & 12 deletions api/views/groep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from api.models.groep import Groep
from api.serializers.groep import GroepSerializer
from api.utils import has_permissions, contains
from api.utils import has_permissions


@api_view(["GET", "POST"])
Expand Down Expand Up @@ -78,17 +78,16 @@ def groep_detail(request, id, format=None):
serializer = GroepSerializer(groep)
return Response(serializer.data)

if has_permissions(request.user) or contains(groep.studenten, request.user):
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 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):
if request.method == "DELETE":
groep.delete()
Expand Down
Loading