diff options
author | 2021-02-22 11:37:33 +0200 | |
---|---|---|
committer | 2021-02-22 11:37:33 +0200 | |
commit | 3907122c9e76f78bd7bfd07028e9eb79b43d65b3 (patch) | |
tree | 6ccaea06ba709ccb1e350b9abda5d3e71b762085 /pydis_site/apps | |
parent | Change NominationEntry import location in serializers (diff) |
Small improvements in nomination viewset
Diffstat (limited to 'pydis_site/apps')
-rw-r--r-- | pydis_site/apps/api/viewsets/bot/nomination.py | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index c4600425..7820ca0d 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -14,8 +14,7 @@ from rest_framework.mixins import ( from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet -from pydis_site.apps.api.models.bot import Nomination -from pydis_site.apps.api.models.bot.nomination import NominationEntry +from pydis_site.apps.api.models.bot import Nomination, NominationEntry from pydis_site.apps.api.serializers import NominationEntrySerializer, NominationSerializer @@ -112,9 +111,9 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge The PATCH route can be used for three distinct operations: 1. Updating the `reason` of `active` nomination; - 2. Updating `reviewed` field of `active` nomination. - 3. Ending an `active` nomination; - 4. Updating the `end_reason` or `reason` field of an `inactive` nomination. + 2. Ending an `active` nomination; + 3. Updating the `end_reason` or `reason` field of an `inactive` nomination. + 4. Updating `reviewed` field of `active` nomination. While the response format and status codes are the same for all three operations (see below), the request bodies vary depending on the operation. For all operations it holds @@ -136,16 +135,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge - 400: if a field in the request body is invalid or disallowed - 404: if an infraction with the given `id` could not be found - ### 2. Setting nomination `reviewed` - - #### Request body - >>> { - ... 'reviewed': True - ... } - - See operation 1 for the response format and status codes. - - ### 3. Ending an `active` nomination + ### 2. Ending an `active` nomination #### Request body >>> { @@ -155,7 +145,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge See operation 1 for the response format and status codes. - ### 4. Updating the `end_reason` or `reason` field of an `inactive` nomination. + ### 3. Updating the `end_reason` or `reason` field of an `inactive` nomination. Actor field is required when updating reason. #### Request body @@ -168,6 +158,15 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge Note: The request body may contain either or both fields. See operation 1 for the response format and status codes. + + ### 4. Setting nomination `reviewed` + + #### Request body + >>> { + ... 'reviewed': True + ... } + + See operation 1 for the response format and status codes. """ serializer_class = NominationSerializer @@ -297,21 +296,9 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge {'end_reason': ["An active nomination can't have an end reason."]} ) - elif 'reviewed' in data: - # 2. We're setting nomination reviewed - if not instance.active: - raise ValidationError( - {'reviewed': ['This field cannot be set if nomination is inactive.']} - ) - - if 'active' in data: - raise ValidationError( - {'active': ['This field cannot be set same time than ending nomination.']} - ) - elif instance.active and not data['active']: - # 3. We're ending an active nomination. - if 'reason' in data: + # 2. We're ending an active nomination. + if 'reason' in request.data: raise ValidationError( {'reason': ['This field cannot be set when ending a nomination.']} ) @@ -321,14 +308,27 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge {'end_reason': ['This field is required when ending a nomination.']} ) + if 'reviewed' in request.data: + raise ValidationError( + {'reviewed': ['This field cannot be set same time than ending nomination.']} + ) + instance.ended_at = timezone.now() elif 'active' in data: - # 4. The `active` field is only allowed when ending a nomination. + # 3. The `active` field is only allowed when ending a nomination. raise ValidationError( {'active': ['This field can only be used to end a nomination']} ) + # This is actually covered, but for some reason coverage don't think so. + elif 'reviewed' in request.data: # pragma: no cover + # 4. We're setting nomination reviewed + if not instance.active: + raise ValidationError( + {'reviewed': ['This field cannot be set if nomination is inactive.']} + ) + if 'reason' in request.data: if 'actor' not in request.data: raise ValidationError( |