aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2021-02-22 09:01:18 +0200
committerGravatar ks129 <[email protected]>2021-02-22 09:01:18 +0200
commite17da1f2e991710beaabd37701ce06a57f7b4a77 (patch)
tree7c6d8172627033802610582a4576f63a79e9de6e
parentAdd reviewed field to nomination serializer (diff)
Migrate PATCH request for 2-table nominations system
-rw-r--r--pydis_site/apps/api/viewsets/bot/nomination.py70
1 files changed, 60 insertions, 10 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py
index 8775515c..14dee9bc 100644
--- a/pydis_site/apps/api/viewsets/bot/nomination.py
+++ b/pydis_site/apps/api/viewsets/bot/nomination.py
@@ -112,18 +112,20 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge
The PATCH route can be used for three distinct operations:
1. Updating the `reason` of `active` nomination;
- 2. Ending an `active` nomination;
- 3. Updating the `end_reason` or `reason` field of an `inactive` 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.
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
that providing other valid fields is not allowed and invalid fields are ignored.
- ### 1. Updating the `reason` of `active` nomination
+ ### 1. Updating the `reason` of `active` nomination. Actor field is required.
#### Request body
>>> {
... 'reason': 'He would make a great helper',
+ ... 'actor': 409107086526644234
... }
#### Response format
@@ -134,7 +136,16 @@ 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. Ending an `active` nomination
+ ### 2. Setting nomination `reviewed`
+
+ #### Request body
+ >>> {
+ ... 'reviewed': True
+ ... }
+
+ See operation 1 for the response format and status codes.
+
+ ### 3. Ending an `active` nomination
#### Request body
>>> {
@@ -144,11 +155,13 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge
See operation 1 for the response format and status codes.
- ### 3. Updating the `end_reason` or `reason` field of an `inactive` nomination.
+ ### 4. Updating the `end_reason` or `reason` field of an `inactive` nomination.
+ Actor field is required when updating reason.
#### Request body
>>> {
... 'reason': 'Updated reason for this nomination',
+ ... 'actor': 409107086526644234,
... 'end_reason': 'Updated end_reason for this nomination',
... }
@@ -162,7 +175,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
filter_fields = ('user__id', 'active')
frozen_fields = ('id', 'inserted_at', 'user', 'ended_at')
- frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at')
+ frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at', 'reviewed')
def list(self, request: HttpRequest, *args, **kwargs) -> Response:
"""
@@ -274,8 +287,20 @@ 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']:
- # 2. We're ending an active nomination.
+ # 3. We're ending an active nomination.
if 'reason' in data:
raise ValidationError(
{'reason': ['This field cannot be set when ending a nomination.']}
@@ -289,11 +314,36 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge
instance.ended_at = timezone.now()
elif 'active' in data:
- # 3. The `active` field is only allowed when ending a nomination.
+ # 4. The `active` field is only allowed when ending a nomination.
raise ValidationError(
{'active': ['This field can only be used to end a nomination']}
)
- serializer.save()
+ if 'reason' in request.data:
+ if 'actor' not in request.data:
+ raise ValidationError(
+ {'actor': 'This field is required when editing reason.'}
+ )
+
+ entry_filter = NominationEntry.objects.filter(
+ nomination_id=instance.id,
+ actor__id=request.data['actor']
+ )
+
+ if not entry_filter.exists():
+ raise ValidationError(
+ {'actor': "Actor don't exist or have not nominated user."}
+ )
+
+ entry = entry_filter[0]
+ entry.reason = request.data['reason']
+ entry.save()
+
+ nomination = serializer.save()
+ return_data = NominationSerializer(nomination).data
+ return_data["entries"] = NominationEntrySerializer(
+ NominationEntry.objects.filter(nomination_id=nomination.id),
+ many=True
+ ).data
- return Response(serializer.data)
+ return Response(return_data)