diff --git a/gregui/api/serializers/role.py b/gregui/api/serializers/role.py index 722c8e8f4a71f8387d525093d766c778263b8b60..0b867f092a1ca0f6eea1d9a1f71cbd9aa703a175 100644 --- a/gregui/api/serializers/role.py +++ b/gregui/api/serializers/role.py @@ -29,6 +29,12 @@ class RoleSerializerUi(serializers.ModelSerializer): ] 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() # New start dates cannot be in the past if start_date < today: @@ -54,9 +60,6 @@ class RoleSerializerUi(serializers.ModelSerializer): raise ValidationError( "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 def validate(self, attrs): @@ -69,7 +72,7 @@ class RoleSerializerUi(serializers.ModelSerializer): max_days = today + datetime.timedelta(days=attrs["type"].max_days) if attrs["end_date"] > max_days: 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 if self.instance: @@ -84,6 +87,11 @@ class RoleSerializerUi(serializers.ModelSerializer): raise serializers.ValidationError( "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 diff --git a/gregui/api/views/role.py b/gregui/api/views/role.py index 7424b2858c5d60b845c3d5eb8146491cd41a7e74..f81e83aa57a9af7717157d90a5ace70873c5d7d9 100644 --- a/gregui/api/views/role.py +++ b/gregui/api/views/role.py @@ -1,5 +1,5 @@ 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.viewsets import ModelViewSet from rest_framework.permissions import IsAuthenticated @@ -18,7 +18,10 @@ class RoleInfoViewSet(ModelViewSet): serializer_class = RoleSerializerUi 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 with transaction.atomic(): serializer = self.serializer_class(