From abd77fdaa67d1686df64f82a919ee283b8baec9b Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Wed, 24 Apr 2019 22:25:30 +0200 Subject: Adding migration solution --- pydis_site/apps/api/serializers.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 8f045044..54f26466 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -178,5 +178,7 @@ class NominationSerializer(ModelSerializer): class Meta: model = Nomination - fields = ('active', 'author', 'reason', 'user', 'inserted_at') + fields = ( + 'id', 'active', 'author', 'reason', 'user', + 'inserted_at', 'unnominate_reason', 'unwatched_at') depth = 1 -- cgit v1.2.3 From 4a67a0646761802e5b6f1996f74ea7f7a9a1f2d9 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Thu, 25 Apr 2019 20:30:37 +0200 Subject: Adding validate to Nomination serializer --- pydis_site/apps/api/serializers.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 54f26466..6fb9d826 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -173,12 +173,22 @@ class UserSerializer(BulkSerializerMixin, ModelSerializer): class NominationSerializer(ModelSerializer): - author = PrimaryKeyRelatedField(queryset=User.objects.all()) + actor = PrimaryKeyRelatedField(queryset=User.objects.all()) user = PrimaryKeyRelatedField(queryset=User.objects.all()) class Meta: model = Nomination fields = ( - 'id', 'active', 'author', 'reason', 'user', + 'id', 'active', 'actor', 'reason', 'user', 'inserted_at', 'unnominate_reason', 'unwatched_at') depth = 1 + + def validate(self, attrs): + active = attrs.get("active") + + unnominate_reason = attrs.get("unnominate_reason") + if active and unnominate_reason: + raise ValidationError( + {'unnominate_reason': "An active nomination can't have an unnominate reason"} + ) + return attrs -- cgit v1.2.3 From b9db880d6229ddb7bf793bc8ca50b37e471956a7 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Sat, 27 Apr 2019 17:30:20 +0200 Subject: Finalizing and adding documentation to the nomination endpoint and simplifying the nomination serializer --- pydis_site/apps/api/serializers.py | 14 -- pydis_site/apps/api/viewsets/bot/nomination.py | 173 +++++++++++++++++++++++-- 2 files changed, 161 insertions(+), 26 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index d58f1fa7..abf49393 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -270,9 +270,6 @@ class UserSerializer(BulkSerializerMixin, ModelSerializer): class NominationSerializer(ModelSerializer): """A class providing (de-)serialization of `Nomination` instances.""" - actor = PrimaryKeyRelatedField(queryset=User.objects.all()) - user = PrimaryKeyRelatedField(queryset=User.objects.all()) - class Meta: """Metadata defined for the Django REST Framework.""" @@ -280,14 +277,3 @@ class NominationSerializer(ModelSerializer): fields = ( 'id', 'active', 'actor', 'reason', 'user', 'inserted_at', 'unnominate_reason', 'unwatched_at') - depth = 1 - - def validate(self, attrs): - active = attrs.get("active") - - unnominate_reason = attrs.get("unnominate_reason") - if active and unnominate_reason: - raise ValidationError( - {'unnominate_reason': "An active nomination can't have an unnominate reason"} - ) - return attrs diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 1059ffcd..888f049e 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -1,49 +1,192 @@ +from collections import ChainMap + from django.utils import timezone from django_filters.rest_framework import DjangoFilterBackend from rest_framework import status from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.filters import OrderingFilter, SearchFilter +from rest_framework.mixins import ( + CreateModelMixin, + ListModelMixin, + RetrieveModelMixin, +) from rest_framework.response import Response -from rest_framework.viewsets import ModelViewSet +from rest_framework.viewsets import GenericViewSet from pydis_site.apps.api.models.bot import Nomination from pydis_site.apps.api.serializers import NominationSerializer -class NominationViewSet(ModelViewSet): - """View providing CRUD operations on helper nominations done through the bot.""" - +class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, GenericViewSet): + """ + View providing CRUD operations on helper nominations done through the bot. + + ## Routes + ### GET /bot/nominations + Retrieve all nominations. + May be filtered and ordered by the query parameters. + + #### Query parameters + - **active** `bool`: whether the nomination is still active + - **actor__id** `int`: snowflake of the user who nominated the user + - **user__id** `int`: snowflake of the user who received the nomination + - **ordering** `str`: comma-separated sequence of fields to order the returned results + + Invalid query parameters are ignored. + + #### Response format + [ + { + 'id': 1, + 'active': false, + 'actor': 336843820513755157, + 'reason': 'They know how to explain difficult concepts', + 'user': 336843820513755157, + 'inserted_at': '2019-04-25T14:02:37.775587Z', + 'unnominate_reason': 'They were helpered after a staff-vote', + 'unwatched_at': '2019-04-26T15:12:22.123587Z' + } + ] + + #### Status codes + - 200: returned on success + + ### GET /bot/nominations/ + Retrieve a single nomination by ID. + + ### Response format + { + 'id': 1, + 'active': true, + 'actor': 336843820513755157, + 'reason': 'They know how to explain difficult concepts', + 'user': 336843820513755157, + 'inserted_at': '2019-04-25T14:02:37.775587Z', + 'unnominate_reason': 'They were helpered after a staff-vote', + 'unwatched_at': '2019-04-26T15:12:22.123587Z' + } + + ### Status codes + - 200: returned on succes + - 404: returned if an nomination with the given `id` could not be found + + ### POST /bot/nominations + Create a new, active nomination returns the created nominations. + The `user`, `reason` and `actor` fields are required and the `user` + and `actor` need to know by the site. Providing other valid fields + is not allowed and invalid fields are ignored. A `user` is only + allowed one active nomination at a time. + + #### Request body + { + 'actor': 409107086526644234 + 'reason': 'He would make a great helper', + 'user': 409107086526644234 + } + + ### Response format + See `GET /bot/nominations/` + + #### Status codes + - 201: returned on success + - 400: returned on failure for one of the following reasons: + - A user already has an active nomination; + - The `user` or `actor` are unknown to the site; + - The request contained a field that cannot be set at creation. + + ### PATCH /bot/nominations/ + Update the nomination with the given `id` and return the updated nomination. + For active nominations, only the `reason` may be updated; for inactive + nominations, both the `reason` and `unnominate_reason` may be updated. + + #### Request body + { + 'reason': 'He would make a great helper', + 'unnominate_reason': 'He needs some time to mature his Python knowledge' + } + + ### Response format + See `GET /bot/nominations/` + + ## Status codes + - 200: returned on success + - 400: if a field in the request body is invalid or disallowed + - 404: if an infraction with the given `id` could not be found + + ### PATCH /bot/nominations//end_nomination + Ends an active nomination and returns the updated nomination. + + The `unnominate_reason` field is the only allowed and required field + for this operation. The nomination will automatically be marked as + `active = false` and the datetime of this operation will be added to + the `unwatched_at` field. + + #### Request body + { + 'unnominate_reason': 'He needs some time to mature his Python knowledge' + } + + ### Response format + See `GET /bot/nominations/` + + #### Status codes + - 200: returned on success + - 400: returned on failure for the following reasons: + - `unnominate_reason` is missing from the request body; + - Any other field is present in the request body; + - The nomination was already inactiive. + - 404: if an infraction with the given `id` could not be found + """ serializer_class = NominationSerializer - queryset = Nomination.objects.prefetch_related('actor', 'user') + queryset = Nomination.objects.all() filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active') frozen_fields = ('id', 'actor', 'inserted_at', 'user', 'unwatched_at', 'active') frozen_on_create = ('unwatched_at', 'unnominate_reason', 'active', 'inserted_at') def create(self, request, *args, **kwargs): + """ + DRF method for creating a Nomination. + + Called by the Django Rest Framework in response to the corresponding HTTP request. + """ for field in request.data: if field in self.frozen_on_create: raise ValidationError({field: ['This field cannot be set at creation.']}) - serializer = self.get_serializer(data=request.data) + user_id = request.data.get("user") + if Nomination.objects.filter(active=True, user__id=user_id): + raise ValidationError({'active': ['There can only be one active nomination.']}) + + serializer = self.get_serializer( + data=ChainMap( + request.data, + {"active": True} + ) + ) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) - def update(self, request, *args, **kwargs): + def partial_update(self, request, *args, **kwargs): """ DRF method for updating a Nomination. Called by the Django Rest Framework in response to the corresponding HTTP request. """ - for field in request.data: if field in self.frozen_fields: raise ValidationError({field: ['This field cannot be updated.']}) instance = self.get_object() + + if instance.active and request.data.get('unnominate_reason'): + raise ValidationError( + {'unnominate_reason': ["An active nomination can't have an unnominate reason."]} + ) + serializer = self.get_serializer(instance, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() @@ -52,23 +195,29 @@ class NominationViewSet(ModelViewSet): @action(detail=True, methods=['patch']) def end_nomination(self, request, pk=None): + """ + DRF action for ending an active nomination. + + Creates an API endpoint /bot/nominations/{id}/end_nomination to end a nomination. See + the class docstring for documentation. + """ for field in request.data: if field != "unnominate_reason": - raise ValidationError({field: ['This field cannot be set at end_nomination']}) + raise ValidationError({field: ['This field cannot be set at end_nomination.']}) if "unnominate_reason" not in request.data: raise ValidationError( - {'unnominate_reason': ['This field is required when ending a nomination']} + {'unnominate_reason': ['This field is required when ending a nomination.']} ) instance = self.get_object() if not instance.active: raise ValidationError({'active': ['A nomination must be active to be ended.']}) - instance.active = False - instance.unwatched_at = timezone.now() serializer = self.get_serializer(instance, data=request.data, partial=True) serializer.is_valid(raise_exception=True) + instance.active = False + instance.unwatched_at = timezone.now() serializer.save() instance.save() -- cgit v1.2.3 From e714fe3750181ac49c790f6724b566885fcc5a10 Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 28 Jun 2019 20:34:32 +0200 Subject: Moving end nomination to PUT method and removing separate end point --- pydis_site/apps/api/models/bot/nomination.py | 2 +- pydis_site/apps/api/serializers.py | 2 +- pydis_site/apps/api/tests/test_nominations.py | 113 ++++++------------------- pydis_site/apps/api/viewsets/bot/nomination.py | 29 +++---- 4 files changed, 42 insertions(+), 104 deletions(-) (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/models/bot/nomination.py b/pydis_site/apps/api/models/bot/nomination.py index 227a40ec..9a80e956 100644 --- a/pydis_site/apps/api/models/bot/nomination.py +++ b/pydis_site/apps/api/models/bot/nomination.py @@ -30,7 +30,7 @@ class Nomination(ModelReprMixin, models.Model): auto_now_add=True, help_text="The creation date of this nomination." ) - unnominate_reason = models.TextField( + end_reason = models.TextField( help_text="Why the nomination was ended.", default="" ) diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index abf49393..2a6d8fce 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -276,4 +276,4 @@ class NominationSerializer(ModelSerializer): model = Nomination fields = ( 'id', 'active', 'actor', 'reason', 'user', - 'inserted_at', 'unnominate_reason', 'unwatched_at') + 'inserted_at', 'end_reason', 'unwatched_at') diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py index aa6561c4..39b2d159 100644 --- a/pydis_site/apps/api/tests/test_nominations.py +++ b/pydis_site/apps/api/tests/test_nominations.py @@ -122,19 +122,19 @@ class CreationTests(APISubdomainTestCase): 'actor': ['Invalid pk "1024" - object does not exist.'] }) - def test_returns_400_for_unnominate_reason_at_creation(self): + def test_returns_400_for_end_reason_at_creation(self): url = reverse('bot:nomination-list', host='api') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', 'actor': self.user.id, - 'unnominate_reason': "Joe Dart on the Joe Dart Bass" + 'end_reason': "Joe Dart on the Joe Dart Bass" } response = self.client.post(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'unnominate_reason': ['This field cannot be set at creation.'] + 'end_reason': ['This field cannot be set at creation.'] }) def test_returns_400_for_unwatched_at_at_creation(self): @@ -203,7 +203,7 @@ class NominationTests(APISubdomainTestCase): actor=cls.user, reason="He's pretty funky", active=False, - unnominate_reason="His neck couldn't hold the funk", + end_reason="His neck couldn't hold the funk", unwatched_at="5018-11-20T15:52:00+00:00" ) @@ -231,16 +231,16 @@ class NominationTests(APISubdomainTestCase): 'user': ['This field cannot be updated.'] }) - def test_returns_400_update_unnominate_reason_on_active(self): + def test_returns_400_update_end_reason_on_active(self): url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') data = { - 'unnominate_reason': 'He started playing jazz' + 'end_reason': 'He started playing jazz' } response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'unnominate_reason': ["An active nomination can't have an unnominate reason."] + 'end_reason': ["An active nomination can't have an unnominate reason."] }) def test_returns_200_update_reason_on_inactive(self): @@ -255,28 +255,28 @@ class NominationTests(APISubdomainTestCase): nomination = Nomination.objects.get(id=response.json()['id']) self.assertEqual(nomination.reason, data['reason']) - def test_returns_200_update_unnominate_reason_on_inactive(self): + def test_returns_200_update_end_reason_on_inactive(self): url = reverse('bot:nomination-detail', args=(self.inactive_nomination.id,), host='api') data = { - 'unnominate_reason': 'He started playing jazz' + 'end_reason': 'He started playing jazz' } response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 200) nomination = Nomination.objects.get(id=response.json()['id']) - self.assertEqual(nomination.unnominate_reason, data['unnominate_reason']) + self.assertEqual(nomination.end_reason, data['end_reason']) def test_returns_200_on_valid_end_nomination(self): url = reverse( - 'bot:nomination-end', + 'bot:nomination-detail', args=(self.active_nomination.id,), host='api' ) data = { - 'unnominate_reason': 'He started playing jazz' + 'end_reason': 'He started playing jazz' } - response = self.client.patch(url, data=data) + response = self.client.put(url, data=data) self.assertEqual(response.status_code, 200) nomination = Nomination.objects.get(id=response.json()['id']) @@ -287,48 +287,48 @@ class NominationTests(APISubdomainTestCase): delta=timedelta(seconds=2) ) self.assertFalse(nomination.active) - self.assertEqual(nomination.unnominate_reason, data['unnominate_reason']) + self.assertEqual(nomination.end_reason, data['end_reason']) def test_returns_400_on_invalid_field_end_nomination(self): url = reverse( - 'bot:nomination-end', + 'bot:nomination-detail', args=(self.active_nomination.id,), host='api' ) data = { 'reason': 'Why does a whale have feet?' } - response = self.client.patch(url, data=data) + response = self.client.put(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { 'reason': ['This field cannot be set at end_nomination.'] }) - def test_returns_400_on_missing_unnominate_reason_end_nomination(self): + def test_returns_400_on_missing_end_reason_end_nomination(self): url = reverse( - 'bot:nomination-end', + 'bot:nomination-detail', args=(self.active_nomination.id,), host='api' ) data = {} - response = self.client.patch(url, data=data) + response = self.client.put(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'unnominate_reason': ['This field is required when ending a nomination.'] + 'end_reason': ['This field is required when ending a nomination.'] }) def test_returns_400_on_ending_inactive_nomination(self): url = reverse( - 'bot:nomination-end', + 'bot:nomination-detail', args=(self.inactive_nomination.id,), host='api' ) data = { - 'unnominate_reason': 'He started playing jazz' + 'end_reason': 'He started playing jazz' } - response = self.client.patch(url, data=data) + response = self.client.put(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { 'active': ['A nomination must be active to be ended.'] @@ -362,16 +362,16 @@ class NominationTests(APISubdomainTestCase): def test_returns_404_on_end_unknown_nomination(self): url = reverse( - 'bot:nomination-end', + 'bot:nomination-detail', args=(9999,), host='api' ) data = { - 'unnominate_reason': 'He started playing jazz' + 'end_reason': 'He started playing jazz' } - response = self.client.patch(url, data=data) + response = self.client.put(url, data=data) self.assertEqual(response.status_code, 404) self.assertEqual(response.json(), { "detail": "Not found." @@ -404,15 +404,6 @@ class NominationTests(APISubdomainTestCase): "detail": "Method \"DELETE\" not allowed." }) - def test_returns_405_on_detail_put(self): - url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') - - response = self.client.put(url, data={}) - self.assertEqual(response.status_code, 405) - self.assertEqual(response.json(), { - "detail": "Method \"PUT\" not allowed." - }) - def test_returns_405_on_detail_post(self): url = reverse('bot:nomination-detail', args=(self.active_nomination.id,), host='api') @@ -431,58 +422,6 @@ class NominationTests(APISubdomainTestCase): "detail": "Method \"DELETE\" not allowed." }) - def test_returns_405_on_end_nomination_put(self): - url = reverse( - 'bot:nomination-end', - args=(self.inactive_nomination.id,), - host='api' - ) - - response = self.client.put(url, data={}) - self.assertEqual(response.status_code, 405) - self.assertEqual(response.json(), { - "detail": "Method \"PUT\" not allowed." - }) - - def test_returns_405_on_end_nomination_post(self): - url = reverse( - 'bot:nomination-end', - args=(self.inactive_nomination.id,), - host='api' - ) - - response = self.client.post(url, data={}) - self.assertEqual(response.status_code, 405) - self.assertEqual(response.json(), { - "detail": "Method \"POST\" not allowed." - }) - - def test_returns_405_on_end_nomination_delete(self): - url = reverse( - 'bot:nomination-end', - args=(self.inactive_nomination.id,), - host='api' - ) - - response = self.client.delete(url, data={}) - self.assertEqual(response.status_code, 405) - self.assertEqual(response.json(), { - "detail": "Method \"DELETE\" not allowed." - }) - - def test_returns_405_on_end_nomination_get(self): - url = reverse( - 'bot:nomination-end', - args=(self.inactive_nomination.id,), - host='api' - ) - - response = self.client.get(url, data={}) - self.assertEqual(response.status_code, 405) - self.assertEqual(response.json(), { - "detail": "Method \"GET\" not allowed." - }) - def test_filter_returns_0_objects_unknown_user__id(self): url = reverse('bot:nomination-list', host='api') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index e8d50728..3d311f72 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -44,7 +44,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ... 'reason': 'They know how to explain difficult concepts', ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', - ... 'unnominate_reason': 'They were helpered after a staff-vote', + ... 'end_reason': 'They were helpered after a staff-vote', ... 'unwatched_at': '2019-04-26T15:12:22.123587Z' ... } ... ] @@ -63,7 +63,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ... 'reason': 'They know how to explain difficult concepts', ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', - ... 'unnominate_reason': 'They were helpered after a staff-vote', + ... 'end_reason': 'They were helpered after a staff-vote', ... 'unwatched_at': '2019-04-26T15:12:22.123587Z' ... } @@ -98,12 +98,12 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ### PATCH /bot/nominations/ Update the nomination with the given `id` and return the updated nomination. For active nominations, only the `reason` may be updated; for inactive - nominations, both the `reason` and `unnominate_reason` may be updated. + nominations, both the `reason` and `end_reason` may be updated. #### Request body >>> { ... 'reason': 'He would make a great helper', - ... 'unnominate_reason': 'He needs some time to mature his Python knowledge' + ... 'end_reason': 'He needs some time to mature his Python knowledge' ... } ### Response format @@ -117,14 +117,14 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ### PATCH /bot/nominations//end_nomination Ends an active nomination and returns the updated nomination. - The `unnominate_reason` field is the only allowed and required field + The `end_reason` field is the only allowed and required field for this operation. The nomination will automatically be marked as `active = false` and the datetime of this operation will be added to the `unwatched_at` field. #### Request body >>> { - ... 'unnominate_reason': 'He needs some time to mature his Python knowledge' + ... 'end_reason': 'He needs some time to mature his Python knowledge' ... } ### Response format @@ -133,7 +133,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge #### Status codes - 200: returned on success - 400: returned on failure for the following reasons: - - `unnominate_reason` is missing from the request body; + - `end_reason` is missing from the request body; - Any other field is present in the request body; - The nomination was already inactiive. - 404: if an infraction with the given `id` could not be found @@ -143,7 +143,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active') frozen_fields = ('id', 'actor', 'inserted_at', 'user', 'unwatched_at', 'active') - frozen_on_create = ('unwatched_at', 'unnominate_reason', 'active', 'inserted_at') + frozen_on_create = ('unwatched_at', 'end_reason', 'active', 'inserted_at') def create(self, request, *args, **kwargs): """ @@ -182,9 +182,9 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge instance = self.get_object() - if instance.active and request.data.get('unnominate_reason'): + if instance.active and request.data.get('end_reason'): raise ValidationError( - {'unnominate_reason': ["An active nomination can't have an unnominate reason."]} + {'end_reason': ["An active nomination can't have an unnominate reason."]} ) serializer = self.get_serializer(instance, data=request.data, partial=True) @@ -193,8 +193,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge return Response(serializer.data) - @action(detail=True, methods=['patch']) - def end(self, request, pk=None): + def update(self, request, *args, **kwargs): """ DRF action for ending an active nomination. @@ -202,12 +201,12 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge the class docstring for documentation. """ for field in request.data: - if field != "unnominate_reason": + if field != "end_reason": raise ValidationError({field: ['This field cannot be set at end_nomination.']}) - if "unnominate_reason" not in request.data: + if "end_reason" not in request.data: raise ValidationError( - {'unnominate_reason': ['This field is required when ending a nomination.']} + {'end_reason': ['This field is required when ending a nomination.']} ) instance = self.get_object() -- cgit v1.2.3 From 3fe8e904bc2f69fa315188334c301bea0d9494ed Mon Sep 17 00:00:00 2001 From: SebastiaanZ <33516116+SebastiaanZ@users.noreply.github.com> Date: Fri, 28 Jun 2019 20:51:48 +0200 Subject: Nomination API: Changing 'unwatched_at' to 'ended_at' --- .../0037_nomination_field_name_change.py | 23 ++++++++++++++++++++++ pydis_site/apps/api/models/bot/nomination.py | 2 +- pydis_site/apps/api/serializers.py | 2 +- pydis_site/apps/api/tests/test_nominations.py | 10 +++++----- pydis_site/apps/api/viewsets/bot/nomination.py | 12 +++++------ 5 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 pydis_site/apps/api/migrations/0037_nomination_field_name_change.py (limited to 'pydis_site/apps/api/serializers.py') diff --git a/pydis_site/apps/api/migrations/0037_nomination_field_name_change.py b/pydis_site/apps/api/migrations/0037_nomination_field_name_change.py new file mode 100644 index 00000000..c5f2d0c5 --- /dev/null +++ b/pydis_site/apps/api/migrations/0037_nomination_field_name_change.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2 on 2019-06-28 18:09 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0036_alter_nominations_api'), + ] + + operations = [ + migrations.RenameField( + model_name='nomination', + old_name='unnominate_reason', + new_name='end_reason', + ), + migrations.RenameField( + model_name='nomination', + old_name='unwatched_at', + new_name='ended_at', + ), + ] diff --git a/pydis_site/apps/api/models/bot/nomination.py b/pydis_site/apps/api/models/bot/nomination.py index 9a80e956..8a8f4d36 100644 --- a/pydis_site/apps/api/models/bot/nomination.py +++ b/pydis_site/apps/api/models/bot/nomination.py @@ -34,7 +34,7 @@ class Nomination(ModelReprMixin, models.Model): help_text="Why the nomination was ended.", default="" ) - unwatched_at = models.DateTimeField( + ended_at = models.DateTimeField( auto_now_add=False, help_text="When the nomination was ended.", null=True diff --git a/pydis_site/apps/api/serializers.py b/pydis_site/apps/api/serializers.py index 2a6d8fce..f1200d34 100644 --- a/pydis_site/apps/api/serializers.py +++ b/pydis_site/apps/api/serializers.py @@ -276,4 +276,4 @@ class NominationSerializer(ModelSerializer): model = Nomination fields = ( 'id', 'active', 'actor', 'reason', 'user', - 'inserted_at', 'end_reason', 'unwatched_at') + 'inserted_at', 'end_reason', 'ended_at') diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py index 39b2d159..206dc65b 100644 --- a/pydis_site/apps/api/tests/test_nominations.py +++ b/pydis_site/apps/api/tests/test_nominations.py @@ -137,19 +137,19 @@ class CreationTests(APISubdomainTestCase): 'end_reason': ['This field cannot be set at creation.'] }) - def test_returns_400_for_unwatched_at_at_creation(self): + def test_returns_400_for_ended_at_at_creation(self): url = reverse('bot:nomination-list', host='api') data = { 'user': self.user.id, 'reason': 'Joe Dart on Fender Bass', 'actor': self.user.id, - 'unwatched_at': "Joe Dart on the Joe Dart Bass" + 'ended_at': "Joe Dart on the Joe Dart Bass" } response = self.client.post(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'unwatched_at': ['This field cannot be set at creation.'] + 'ended_at': ['This field cannot be set at creation.'] }) def test_returns_400_for_inserted_at_at_creation(self): @@ -204,7 +204,7 @@ class NominationTests(APISubdomainTestCase): reason="He's pretty funky", active=False, end_reason="His neck couldn't hold the funk", - unwatched_at="5018-11-20T15:52:00+00:00" + ended_at="5018-11-20T15:52:00+00:00" ) def test_returns_200_update_reason_on_active(self): @@ -282,7 +282,7 @@ class NominationTests(APISubdomainTestCase): nomination = Nomination.objects.get(id=response.json()['id']) self.assertAlmostEqual( - nomination.unwatched_at, + nomination.ended_at, dt.now(timezone.utc), delta=timedelta(seconds=2) ) diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 3d311f72..f8ad5017 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -45,7 +45,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', ... 'end_reason': 'They were helpered after a staff-vote', - ... 'unwatched_at': '2019-04-26T15:12:22.123587Z' + ... 'ended_at': '2019-04-26T15:12:22.123587Z' ... } ... ] @@ -64,7 +64,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', ... 'end_reason': 'They were helpered after a staff-vote', - ... 'unwatched_at': '2019-04-26T15:12:22.123587Z' + ... 'ended_at': '2019-04-26T15:12:22.123587Z' ... } ### Status codes @@ -120,7 +120,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge The `end_reason` field is the only allowed and required field for this operation. The nomination will automatically be marked as `active = false` and the datetime of this operation will be added to - the `unwatched_at` field. + the `ended_at` field. #### Request body >>> { @@ -142,8 +142,8 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge queryset = Nomination.objects.all() filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active') - frozen_fields = ('id', 'actor', 'inserted_at', 'user', 'unwatched_at', 'active') - frozen_on_create = ('unwatched_at', 'end_reason', 'active', 'inserted_at') + frozen_fields = ('id', 'actor', 'inserted_at', 'user', 'ended_at', 'active') + frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at') def create(self, request, *args, **kwargs): """ @@ -216,7 +216,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge serializer = self.get_serializer(instance, data=request.data, partial=True) serializer.is_valid(raise_exception=True) instance.active = False - instance.unwatched_at = timezone.now() + instance.ended_at = timezone.now() serializer.save() instance.save() -- cgit v1.2.3