Skip to content
Snippets Groups Projects
Verified Commit 369baf3f authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Check more things in ui role endpoint

Start dates can now be blank, check for correct sponsor has been moved
to the more appropriate validate method and we now check that the role
exists before doing anything in the serializer
parent 4a9f1080
No related branches found
No related tags found
1 merge request!160Tests/increase coverage
...@@ -29,6 +29,12 @@ class RoleSerializerUi(serializers.ModelSerializer): ...@@ -29,6 +29,12 @@ class RoleSerializerUi(serializers.ModelSerializer):
] ]
def validate_start_date(self, start_date): def validate_start_date(self, start_date):
"""Enfore rules for start_date.
Must be present, can be blank, before today not allowed.
"""
if not start_date:
return start_date
today = datetime.date.today() today = datetime.date.today()
# New start dates cannot be in the past # New start dates cannot be in the past
if start_date < today: if start_date < today:
...@@ -54,9 +60,6 @@ class RoleSerializerUi(serializers.ModelSerializer): ...@@ -54,9 +60,6 @@ class RoleSerializerUi(serializers.ModelSerializer):
raise ValidationError( raise ValidationError(
"A sponsor can only make changes to roles at units they are sponsors for." "A sponsor can only make changes to roles at units they are sponsors for."
) )
# If we are updating an existing roles, we must be the sponsor of the role
if self.instance and self.instance.sponsor != sponsor:
raise ValidationError("You can only edit your own roles.")
return unit return unit
def validate(self, attrs): def validate(self, attrs):
...@@ -69,7 +72,7 @@ class RoleSerializerUi(serializers.ModelSerializer): ...@@ -69,7 +72,7 @@ class RoleSerializerUi(serializers.ModelSerializer):
max_days = today + datetime.timedelta(days=attrs["type"].max_days) max_days = today + datetime.timedelta(days=attrs["type"].max_days)
if attrs["end_date"] > max_days: if attrs["end_date"] > max_days:
raise serializers.ValidationError( raise serializers.ValidationError(
f"New end date too far into the future for this type. Must be before {max_days.strftime('%Y-%m-%d')}" f"New end date too far into the future for this type. Must be before {max_days.strftime('%Y-%m-%d')}."
) )
# Ensure end date is after start date if start date is set # Ensure end date is after start date if start date is set
if self.instance: if self.instance:
...@@ -84,6 +87,11 @@ class RoleSerializerUi(serializers.ModelSerializer): ...@@ -84,6 +87,11 @@ class RoleSerializerUi(serializers.ModelSerializer):
raise serializers.ValidationError( raise serializers.ValidationError(
"End date cannot be before start date." "End date cannot be before start date."
) )
# If we are updating an existing roles, we must be the sponsor of the role
sponsor = self.context["sponsor"]
if self.instance and self.instance.sponsor != sponsor:
raise ValidationError("You can only edit your own roles.")
return attrs return attrs
......
from django.db import transaction from django.db import transaction
from rest_framework import serializers, status from rest_framework import status
from rest_framework.authentication import BasicAuthentication, SessionAuthentication from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
...@@ -18,7 +18,10 @@ class RoleInfoViewSet(ModelViewSet): ...@@ -18,7 +18,10 @@ class RoleInfoViewSet(ModelViewSet):
serializer_class = RoleSerializerUi serializer_class = RoleSerializerUi
def partial_update(self, request, pk): def partial_update(self, request, pk):
role = Role.objects.get(pk=pk) try:
role = Role.objects.get(pk=pk)
except Role.DoesNotExist:
return Response(status=status.HTTP_400_BAD_REQUEST)
sponsor = GregUserProfile.objects.get(user=self.request.user).sponsor sponsor = GregUserProfile.objects.get(user=self.request.user).sponsor
with transaction.atomic(): with transaction.atomic():
serializer = self.serializer_class( serializer = self.serializer_class(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment