From 17b0570e3785e50b347d9eba3f9bd4516cf08ed5 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Wed, 27 Jan 2021 03:20:12 +0530 Subject: Apply LimitOffsetPagination in GET Infraction with default Page Size 100 --- pydis_site/apps/api/viewsets/bot/infraction.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 423e806e..b89c3386 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -3,6 +3,7 @@ from django_filters.rest_framework import DjangoFilterBackend from rest_framework.decorators import action from rest_framework.exceptions import ValidationError from rest_framework.filters import OrderingFilter, SearchFilter +from rest_framework.pagination import LimitOffsetPagination from rest_framework.mixins import ( CreateModelMixin, DestroyModelMixin, @@ -133,6 +134,7 @@ class InfractionViewSet( serializer_class = InfractionSerializer queryset = Infraction.objects.all() + pagination_class = LimitOffsetPagination filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type') search_fields = ('$reason',) -- cgit v1.2.3 From a67d3d9abfebfc45cef2ed98bb727de02156d113 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Wed, 27 Jan 2021 04:01:33 +0530 Subject: fix: flake8 error --- pydis_site/apps/api/viewsets/bot/infraction.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index b89c3386..2ccb35dd 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -3,21 +3,15 @@ from django_filters.rest_framework import DjangoFilterBackend 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, DestroyModelMixin, + ListModelMixin, RetrieveModelMixin) from rest_framework.pagination import LimitOffsetPagination -from rest_framework.mixins import ( - CreateModelMixin, - DestroyModelMixin, - ListModelMixin, - RetrieveModelMixin -) from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet from pydis_site.apps.api.models.bot.infraction import Infraction -from pydis_site.apps.api.serializers import ( - ExpandedInfractionSerializer, - InfractionSerializer -) +from pydis_site.apps.api.serializers import (ExpandedInfractionSerializer, + InfractionSerializer) class InfractionViewSet( -- cgit v1.2.3 From d1955b7a87fcd126b3c0a6b33ec2e7df6495b3de Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Thu, 28 Jan 2021 02:40:17 +0530 Subject: update paginated response in list infraction --- pydis_site/apps/api/viewsets/bot/infraction.py | 15 +++++++++++++++ pydis_site/settings.py | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 2ccb35dd..851eae34 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -133,6 +133,21 @@ class InfractionViewSet( filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type') search_fields = ('$reason',) frozen_fields = ('id', 'inserted_at', 'type', 'user', 'actor', 'hidden') + LimitOffsetPagination.default_limit = 100 + + def list(self, request: HttpRequest, *args, **kwargs) -> Response: + """ + DRF method for listing Infraction entries. + + Called by the Django Rest Framework in response to the corresponding HTTP request. + """ + queryset = self.filter_queryset(self.get_queryset()) + page = self.paginate_queryset(queryset) + if page: + serializer = self.get_serializer(page, many=True) + return Response(self.get_paginated_response(serializer.data).data.get('results')) + serializer = InfractionSerializer(queryset, many=True) + return Response(serializer.data) def partial_update(self, request: HttpRequest, *_args, **_kwargs) -> Response: """Method that handles the nuts and bolts of updating an Infraction.""" diff --git a/pydis_site/settings.py b/pydis_site/settings.py index a98b5712..8f5c58ef 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -230,9 +230,8 @@ REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.DjangoModelPermissions', ), - 'TEST_REQUEST_DEFAULT_FORMAT': 'json', - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', - 'PAGE_SIZE': 100, + 'TEST_REQUEST_DEFAULT_FORMAT': 'json' + } # Logging -- cgit v1.2.3 From 83b5f8ebd9c17fe6a444765f8d3a8ead431fa916 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Thu, 28 Jan 2021 03:05:53 +0530 Subject: Styling fixes --- pydis_site/apps/api/viewsets/bot/infraction.py | 17 ++++++++++++----- pydis_site/settings.py | 1 - 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 851eae34..6464c1b9 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -3,15 +3,21 @@ from django_filters.rest_framework import DjangoFilterBackend 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, DestroyModelMixin, - ListModelMixin, RetrieveModelMixin) +from rest_framework.mixins import ( + CreateModelMixin, + DestroyModelMixin, + ListModelMixin, + RetrieveModelMixin +) from rest_framework.pagination import LimitOffsetPagination from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet from pydis_site.apps.api.models.bot.infraction import Infraction -from pydis_site.apps.api.serializers import (ExpandedInfractionSerializer, - InfractionSerializer) +from pydis_site.apps.api.serializers import ( + ExpandedInfractionSerializer, + InfractionSerializer +) class InfractionViewSet( @@ -145,7 +151,8 @@ class InfractionViewSet( page = self.paginate_queryset(queryset) if page: serializer = self.get_serializer(page, many=True) - return Response(self.get_paginated_response(serializer.data).data.get('results')) + result = self.get_paginated_response(serializer.data) + return Response(result.data.get('results')) serializer = InfractionSerializer(queryset, many=True) return Response(serializer.data) diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 8f5c58ef..50caab80 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -231,7 +231,6 @@ REST_FRAMEWORK = { 'rest_framework.permissions.DjangoModelPermissions', ), 'TEST_REQUEST_DEFAULT_FORMAT': 'json' - } # Logging -- cgit v1.2.3 From 51c57d3707c684bb908195f419e53f4ed164ba3b Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 22 Feb 2021 08:23:04 +0200 Subject: Update nominations viewset GET and POST to make this working with 2-table system --- pydis_site/apps/api/viewsets/bot/nomination.py | 119 ++++++++++++++++++++----- 1 file changed, 96 insertions(+), 23 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index cf6e262f..8775515c 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -15,7 +15,8 @@ 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.serializers import NominationSerializer +from pydis_site.apps.api.models.bot.nomination import NominationEntry +from pydis_site.apps.api.serializers import NominationEntrySerializer, NominationSerializer class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, GenericViewSet): @@ -29,7 +30,6 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge #### 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 @@ -40,12 +40,18 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ... { ... 'id': 1, ... 'active': false, - ... 'actor': 336843820513755157, - ... 'reason': 'They know how to explain difficult concepts', ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', ... 'end_reason': 'They were helpered after a staff-vote', - ... 'ended_at': '2019-04-26T15:12:22.123587Z' + ... 'ended_at': '2019-04-26T15:12:22.123587Z', + ... 'entries': [ + ... { + ... 'actor': 336843820513755157, + ... 'reason': 'They know how to explain difficult concepts', + ... 'inserted_at': '2019-04-25T14:02:37.775587Z' + ... } + ... ], + ... 'reviewed': true ... } ... ] @@ -59,12 +65,18 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge >>> { ... 'id': 1, ... 'active': true, - ... 'actor': 336843820513755157, - ... 'reason': 'They know how to explain difficult concepts', ... 'user': 336843820513755157, ... 'inserted_at': '2019-04-25T14:02:37.775587Z', ... 'end_reason': 'They were helpered after a staff-vote', - ... 'ended_at': '2019-04-26T15:12:22.123587Z' + ... 'ended_at': '2019-04-26T15:12:22.123587Z', + ... 'entries': [ + ... { + ... 'actor': 336843820513755157, + ... 'reason': 'They know how to explain difficult concepts', + ... 'inserted_at': '2019-04-25T14:02:37.775587Z' + ... } + ... ], + ... 'reviewed': false ... } ### Status codes @@ -75,8 +87,9 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge 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. + is not allowed and invalid fields are ignored. If `user` already have + active nomination, new nomination entry will be created assigned to + active nomination. #### Request body >>> { @@ -91,7 +104,6 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge #### 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. @@ -148,10 +160,44 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge serializer_class = NominationSerializer queryset = Nomination.objects.all() filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) - filter_fields = ('user__id', 'actor__id', 'active') - frozen_fields = ('id', 'actor', 'inserted_at', 'user', 'ended_at') + filter_fields = ('user__id', 'active') + frozen_fields = ('id', 'inserted_at', 'user', 'ended_at') frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at') + def list(self, request: HttpRequest, *args, **kwargs) -> Response: + """ + DRF method for listing Nominations. + + Called by the Django Rest Framework in response to the corresponding HTTP request. + """ + queryset = self.filter_queryset(self.get_queryset()) + data = NominationSerializer(queryset, many=True).data + + for i, nomination in enumerate(data): + entries = NominationEntrySerializer( + NominationEntry.objects.filter(nomination_id=nomination["id"]), + many=True + ).data + data[i]["entries"] = entries + + return Response(data) + + def retrieve(self, request: HttpRequest, *args, **kwargs) -> Response: + """ + DRF method for retrieving a Nomination. + + Called by the Django Rest Framework in response to the corresponding HTTP request. + """ + nomination = self.get_object() + + data = NominationSerializer(nomination).data + data["entries"] = NominationEntrySerializer( + NominationEntry.objects.filter(nomination_id=nomination.id), + many=True + ).data + + return Response(data) + def create(self, request: HttpRequest, *args, **kwargs) -> Response: """ DRF method for creating a Nomination. @@ -163,19 +209,46 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge raise ValidationError({field: ['This field cannot be set at creation.']}) user_id = request.data.get("user") - if Nomination.objects.filter(active=True, user__id=user_id).exists(): - raise ValidationError({'active': ['There can only be one active nomination.']}) + nomination_filter = Nomination.objects.filter(active=True, user__id=user_id) + + if not nomination_filter.exists(): + serializer = NominationSerializer( + data=ChainMap( + request.data, + {"active": True} + ) + ) + serializer.is_valid(raise_exception=True) + nomination = Nomination.objects.create(**serializer.validated_data) - serializer = self.get_serializer( - data=ChainMap( - request.data, - {"active": True} + # Serializer truncate unnecessary data away + entry_serializer = NominationEntrySerializer( + data=ChainMap(request.data, {"nomination": nomination.id}) ) + entry_serializer.is_valid(raise_exception=True) + + entry = NominationEntry.objects.create(**entry_serializer.validated_data) + + data = NominationSerializer(nomination).data + data["entries"] = NominationEntrySerializer([entry], many=True).data + + headers = self.get_success_headers(data) + return Response(data, status=status.HTTP_201_CREATED, headers=headers) + + entry_serializer = NominationEntrySerializer( + data=ChainMap(request.data, {"nomination": nomination_filter[0].id}) ) - 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) + entry_serializer.is_valid(raise_exception=True) + NominationEntry.objects.create(**entry_serializer.validated_data) + + data = NominationSerializer(nomination_filter[0]).data + data["entries"] = NominationEntrySerializer( + NominationEntry.objects.filter(nomination_id=nomination_filter[0].id), + many=True + ).data + + headers = self.get_success_headers(data) + return Response(data, status=status.HTTP_201_CREATED, headers=headers) def partial_update(self, request: HttpRequest, *args, **kwargs) -> Response: """ -- cgit v1.2.3 From e17da1f2e991710beaabd37701ce06a57f7b4a77 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 22 Feb 2021 09:01:18 +0200 Subject: Migrate PATCH request for 2-table nominations system --- pydis_site/apps/api/viewsets/bot/nomination.py | 70 ++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 10 deletions(-) (limited to 'pydis_site/apps/api/viewsets') 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) -- cgit v1.2.3 From 0ae69d9892db73029336e7b9ca95164dd80f828f Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 22 Feb 2021 09:05:54 +0200 Subject: Disable creating multiple nomination entries of one nomination for one actor --- pydis_site/apps/api/viewsets/bot/nomination.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 14dee9bc..81fb43f7 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -252,6 +252,16 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge data=ChainMap(request.data, {"nomination": nomination_filter[0].id}) ) entry_serializer.is_valid(raise_exception=True) + + # Don't allow user creating many nomination entries for one nomination + if NominationEntry.objects.filter( + nomination_id=nomination_filter[0].id, + actor__id=entry_serializer.validated_data["actor"].id + ).exists(): + raise ValidationError( + {'actor': 'This actor have already created nomination entry for this nomination.'} + ) + NominationEntry.objects.create(**entry_serializer.validated_data) data = NominationSerializer(nomination_filter[0]).data -- cgit v1.2.3 From 698cbc17405a49fe42669fedd6054ab6c9008a4a Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 22 Feb 2021 09:37:12 +0200 Subject: Wrap validation errors to [] --- pydis_site/apps/api/viewsets/bot/nomination.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 81fb43f7..c4600425 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -259,7 +259,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge actor__id=entry_serializer.validated_data["actor"].id ).exists(): raise ValidationError( - {'actor': 'This actor have already created nomination entry for this nomination.'} + {'actor': ['This actor have already created nomination entry for this nomination.']} ) NominationEntry.objects.create(**entry_serializer.validated_data) @@ -301,12 +301,12 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge # 2. We're setting nomination reviewed if not instance.active: raise ValidationError( - {'reviewed': 'This field cannot be set if nomination is inactive.'} + {'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.'} + {'active': ['This field cannot be set same time than ending nomination.']} ) elif instance.active and not data['active']: @@ -332,7 +332,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if 'reason' in request.data: if 'actor' not in request.data: raise ValidationError( - {'actor': 'This field is required when editing reason.'} + {'actor': ['This field is required when editing reason.']} ) entry_filter = NominationEntry.objects.filter( @@ -342,7 +342,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if not entry_filter.exists(): raise ValidationError( - {'actor': "Actor don't exist or have not nominated user."} + {'actor': ["Actor don't exist or have not nominated user."]} ) entry = entry_filter[0] -- cgit v1.2.3 From 3907122c9e76f78bd7bfd07028e9eb79b43d65b3 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Mon, 22 Feb 2021 11:37:33 +0200 Subject: Small improvements in nomination viewset --- pydis_site/apps/api/viewsets/bot/nomination.py | 62 +++++++++++++------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'pydis_site/apps/api/viewsets') 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 @@ -167,6 +157,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. """ @@ -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( -- cgit v1.2.3 From 3edb416ba86c62d62c0258d4c69f967e47765186 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Wed, 24 Feb 2021 08:38:13 +0200 Subject: Simplify nominations viewset After moving entries to nomination serializer we can get rid from GET request handlers and let DRF handle this. Also PATCH and POST handlers got some simplification by removing manual entries setting. --- pydis_site/apps/api/viewsets/bot/nomination.py | 51 ++------------------------ 1 file changed, 3 insertions(+), 48 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 7820ca0d..e3e71ca2 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -176,40 +176,6 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge frozen_fields = ('id', 'inserted_at', 'user', 'ended_at') frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at', 'reviewed') - def list(self, request: HttpRequest, *args, **kwargs) -> Response: - """ - DRF method for listing Nominations. - - Called by the Django Rest Framework in response to the corresponding HTTP request. - """ - queryset = self.filter_queryset(self.get_queryset()) - data = NominationSerializer(queryset, many=True).data - - for i, nomination in enumerate(data): - entries = NominationEntrySerializer( - NominationEntry.objects.filter(nomination_id=nomination["id"]), - many=True - ).data - data[i]["entries"] = entries - - return Response(data) - - def retrieve(self, request: HttpRequest, *args, **kwargs) -> Response: - """ - DRF method for retrieving a Nomination. - - Called by the Django Rest Framework in response to the corresponding HTTP request. - """ - nomination = self.get_object() - - data = NominationSerializer(nomination).data - data["entries"] = NominationEntrySerializer( - NominationEntry.objects.filter(nomination_id=nomination.id), - many=True - ).data - - return Response(data) - def create(self, request: HttpRequest, *args, **kwargs) -> Response: """ DRF method for creating a Nomination. @@ -238,11 +204,9 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge data=ChainMap(request.data, {"nomination": nomination.id}) ) entry_serializer.is_valid(raise_exception=True) - - entry = NominationEntry.objects.create(**entry_serializer.validated_data) + NominationEntry.objects.create(**entry_serializer.validated_data) data = NominationSerializer(nomination).data - data["entries"] = NominationEntrySerializer([entry], many=True).data headers = self.get_success_headers(data) return Response(data, status=status.HTTP_201_CREATED, headers=headers) @@ -264,10 +228,6 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge NominationEntry.objects.create(**entry_serializer.validated_data) data = NominationSerializer(nomination_filter[0]).data - data["entries"] = NominationEntrySerializer( - NominationEntry.objects.filter(nomination_id=nomination_filter[0].id), - many=True - ).data headers = self.get_success_headers(data) return Response(data, status=status.HTTP_201_CREATED, headers=headers) @@ -349,11 +309,6 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge 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 + serializer.save() - return Response(return_data) + return Response(serializer.data) -- cgit v1.2.3 From 26e19ff1537856918aaf9b7d717fa10dc70fabc8 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Thu, 25 Feb 2021 06:10:02 +0530 Subject: Add custom paginator class to override default resonse --- pydis_site/apps/api/viewsets/bot/infraction.py | 23 +++++------------------ pydis_site/apps/api/viewsets/bot/pagination.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 18 deletions(-) create mode 100644 pydis_site/apps/api/viewsets/bot/pagination.py (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 6464c1b9..2b0bbf3e 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -9,7 +9,6 @@ from rest_framework.mixins import ( ListModelMixin, RetrieveModelMixin ) -from rest_framework.pagination import LimitOffsetPagination from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet @@ -18,6 +17,7 @@ from pydis_site.apps.api.serializers import ( ExpandedInfractionSerializer, InfractionSerializer ) +from pydis_site.apps.api.viewsets.bot.pagination import LimitSetPagination class InfractionViewSet( @@ -39,6 +39,8 @@ class InfractionViewSet( - **active** `bool`: whether the infraction is still active - **actor__id** `int`: snowflake of the user which applied the infraction - **hidden** `bool`: whether the infraction is a shadow infraction + - **limit** `int`: default limit is 100 + - **offset** `int`: default is 0 - **search** `str`: regular expression applied to the infraction's reason - **type** `str`: the type of the infraction - **user__id** `int`: snowflake of the user to which the infraction was applied @@ -47,6 +49,7 @@ class InfractionViewSet( Invalid query parameters are ignored. #### Response format + - Response are paginated but only the actual data is returned >>> [ ... { ... 'id': 5, @@ -134,27 +137,11 @@ class InfractionViewSet( serializer_class = InfractionSerializer queryset = Infraction.objects.all() - pagination_class = LimitOffsetPagination + pagination_class = LimitSetPagination filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type') search_fields = ('$reason',) frozen_fields = ('id', 'inserted_at', 'type', 'user', 'actor', 'hidden') - LimitOffsetPagination.default_limit = 100 - - def list(self, request: HttpRequest, *args, **kwargs) -> Response: - """ - DRF method for listing Infraction entries. - - Called by the Django Rest Framework in response to the corresponding HTTP request. - """ - queryset = self.filter_queryset(self.get_queryset()) - page = self.paginate_queryset(queryset) - if page: - serializer = self.get_serializer(page, many=True) - result = self.get_paginated_response(serializer.data) - return Response(result.data.get('results')) - serializer = InfractionSerializer(queryset, many=True) - return Response(serializer.data) def partial_update(self, request: HttpRequest, *_args, **_kwargs) -> Response: """Method that handles the nuts and bolts of updating an Infraction.""" diff --git a/pydis_site/apps/api/viewsets/bot/pagination.py b/pydis_site/apps/api/viewsets/bot/pagination.py new file mode 100644 index 00000000..f9a8ccae --- /dev/null +++ b/pydis_site/apps/api/viewsets/bot/pagination.py @@ -0,0 +1,12 @@ +from rest_framework.pagination import LimitOffsetPagination +from rest_framework.response import Response + + +class LimitSetPagination(LimitOffsetPagination): + """Extend LimitOffsetPagination.""" + + default_limit = 100 + + def get_paginated_response(self, data: list) -> Response: + """Override default response.""" + return Response(data) -- cgit v1.2.3 From 4a5fa2d3c8e0aa2d71e13d15ca7b31ef46535001 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Fri, 26 Feb 2021 13:10:05 +0530 Subject: Update pagination docs and pagination class location --- pydis_site/apps/api/pagination.py | 49 ++++++++++++++++++++++++++ pydis_site/apps/api/viewsets/bot/infraction.py | 11 +++--- pydis_site/apps/api/viewsets/bot/pagination.py | 12 ------- 3 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 pydis_site/apps/api/pagination.py delete mode 100644 pydis_site/apps/api/viewsets/bot/pagination.py (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/pagination.py b/pydis_site/apps/api/pagination.py new file mode 100644 index 00000000..bd8d963d --- /dev/null +++ b/pydis_site/apps/api/pagination.py @@ -0,0 +1,49 @@ +import typing + +from rest_framework.pagination import LimitOffsetPagination +from rest_framework.response import Response + + +class LimitOffsetPaginationExtended(LimitOffsetPagination): + """ + Extend LimitOffsetPagination to customise the default response. + + For example: + + ## Default response + { + "count": 1, + "next": null, + "previous": null, + "results": [{ + "id": 6, + "inserted_at": "2021-01-26T21:13:35.477879Z", + "expires_at": null, + "active": false, + "user": 1, + "actor": 2, + "type": "warning", + "reason": null, + "hidden": false + }] + } + ## Required response + [{ + "id": 6, + "inserted_at": "2021-01-26T21:13:35.477879Z", + "expires_at": null, + "active": false, + "user": 1, + "actor": 2, + "type": "warning", + "reason": null, + "hidden": false + }] + + """ + + default_limit = 100 + + def get_paginated_response(self, data: typing.Any) -> Response: + """Override to skip metadata i.e. `count`, `next` and `previous`.""" + return Response(data) diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 2b0bbf3e..1bf89203 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -13,11 +13,11 @@ from rest_framework.response import Response from rest_framework.viewsets import GenericViewSet from pydis_site.apps.api.models.bot.infraction import Infraction +from pydis_site.apps.api.pagination import LimitOffsetPaginationExtended from pydis_site.apps.api.serializers import ( ExpandedInfractionSerializer, InfractionSerializer ) -from pydis_site.apps.api.viewsets.bot.pagination import LimitSetPagination class InfractionViewSet( @@ -39,8 +39,8 @@ class InfractionViewSet( - **active** `bool`: whether the infraction is still active - **actor__id** `int`: snowflake of the user which applied the infraction - **hidden** `bool`: whether the infraction is a shadow infraction - - **limit** `int`: default limit is 100 - - **offset** `int`: default is 0 + - **limit** `int`: number of results return per page. default limit is 100 + - **offset** `int`: the initial index from which to return the results, default is 0 - **search** `str`: regular expression applied to the infraction's reason - **type** `str`: the type of the infraction - **user__id** `int`: snowflake of the user to which the infraction was applied @@ -49,7 +49,8 @@ class InfractionViewSet( Invalid query parameters are ignored. #### Response format - - Response are paginated but only the actual data is returned + - Response are paginated but the result is returned without any pagination metadata. + Below is the returned format. >>> [ ... { ... 'id': 5, @@ -137,7 +138,7 @@ class InfractionViewSet( serializer_class = InfractionSerializer queryset = Infraction.objects.all() - pagination_class = LimitSetPagination + pagination_class = LimitOffsetPaginationExtended filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) filter_fields = ('user__id', 'actor__id', 'active', 'hidden', 'type') search_fields = ('$reason',) diff --git a/pydis_site/apps/api/viewsets/bot/pagination.py b/pydis_site/apps/api/viewsets/bot/pagination.py deleted file mode 100644 index f9a8ccae..00000000 --- a/pydis_site/apps/api/viewsets/bot/pagination.py +++ /dev/null @@ -1,12 +0,0 @@ -from rest_framework.pagination import LimitOffsetPagination -from rest_framework.response import Response - - -class LimitSetPagination(LimitOffsetPagination): - """Extend LimitOffsetPagination.""" - - default_limit = 100 - - def get_paginated_response(self, data: list) -> Response: - """Override default response.""" - return Response(data) -- cgit v1.2.3 From 54eca6da46d83d8872746b89b95a22f7cf0c2b52 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 5 Mar 2021 07:48:32 +0200 Subject: Fix grammar of nomination endpoints documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leon Sandøy Co-authored-by: Joe Banks --- pydis_site/apps/api/viewsets/bot/nomination.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index e3e71ca2..c208df46 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -86,8 +86,8 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge 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. If `user` already have - active nomination, new nomination entry will be created assigned to + is not allowed and invalid fields are ignored. If `user` already has an + active nomination, a new nomination entry will be created and assigned as the active nomination. #### Request body @@ -119,7 +119,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge 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. Actor field is required. + ### 1. Updating the `reason` of `active` nomination. The `actor` field is required. #### Request body >>> { @@ -199,7 +199,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge serializer.is_valid(raise_exception=True) nomination = Nomination.objects.create(**serializer.validated_data) - # Serializer truncate unnecessary data away + # The serializer will truncate and get rid of excessive data entry_serializer = NominationEntrySerializer( data=ChainMap(request.data, {"nomination": nomination.id}) ) @@ -283,7 +283,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge # 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 + # 4. We are altering the reviewed state of the nomination. if not instance.active: raise ValidationError( {'reviewed': ['This field cannot be set if nomination is inactive.']} -- cgit v1.2.3 From 72693a6b72332c1ca9411f73ae14d395c5dd8933 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 5 Mar 2021 07:49:46 +0200 Subject: Replace double quotes with single quotes Co-authored-by: Joe Banks --- pydis_site/apps/api/viewsets/bot/nomination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index c208df46..ef7d1dab 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -302,7 +302,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if not entry_filter.exists(): raise ValidationError( - {'actor': ["Actor don't exist or have not nominated user."]} + {'actor': ['Actor don't exist or have not nominated user.']} ) entry = entry_filter[0] -- cgit v1.2.3 From 9075231a5fd68986b0845327d6a2bffe70447fc8 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 5 Mar 2021 08:18:17 +0200 Subject: Use double quotes instead apostrophe because string contain "don't" --- pydis_site/apps/api/viewsets/bot/nomination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index ef7d1dab..c208df46 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -302,7 +302,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if not entry_filter.exists(): raise ValidationError( - {'actor': ['Actor don't exist or have not nominated user.']} + {'actor': ["Actor don't exist or have not nominated user."]} ) entry = entry_filter[0] -- cgit v1.2.3 From d8751ad37ed5a493554575ea3adb264def342664 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 5 Mar 2021 08:23:28 +0200 Subject: Fix grammar of error messages and change tests to match with changes --- pydis_site/apps/api/tests/test_nominations.py | 8 ++++---- pydis_site/apps/api/viewsets/bot/nomination.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py index 1daa6f3f..c07679c5 100644 --- a/pydis_site/apps/api/tests/test_nominations.py +++ b/pydis_site/apps/api/tests/test_nominations.py @@ -78,7 +78,7 @@ class CreationTests(APISubdomainTestCase): response2 = self.client.post(url, data=data) self.assertEqual(response2.status_code, 400) self.assertEqual(response2.json(), { - 'actor': ['This actor have already created nomination entry for this nomination.'] + 'actor': ['This actor has already endorsed this nomination.'] }) def test_returns_400_for_missing_user(self): @@ -498,7 +498,7 @@ class NominationTests(APISubdomainTestCase): response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'reviewed': ['This field cannot be set if nomination is inactive.'] + 'reviewed': ['This field cannot be set if the nomination is inactive.'] }) def test_patch_nomination_set_reviewed_and_end(self): @@ -508,7 +508,7 @@ class NominationTests(APISubdomainTestCase): response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'reviewed': ['This field cannot be set same time than ending nomination.'] + 'reviewed': ['This field cannot be set while you are ending a nomination.'] }) def test_modifying_reason_without_actor(self): @@ -518,7 +518,7 @@ class NominationTests(APISubdomainTestCase): response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'actor': ['This field is required when editing reason.'] + 'actor': ['This field is required when editing the reason.'] }) def test_modifying_reason_with_unknown_actor(self): diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index c208df46..451264f4 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -222,7 +222,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge actor__id=entry_serializer.validated_data["actor"].id ).exists(): raise ValidationError( - {'actor': ['This actor have already created nomination entry for this nomination.']} + {'actor': ['This actor has already endorsed this nomination.']} ) NominationEntry.objects.create(**entry_serializer.validated_data) @@ -270,7 +270,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if 'reviewed' in request.data: raise ValidationError( - {'reviewed': ['This field cannot be set same time than ending nomination.']} + {'reviewed': ['This field cannot be set while you are ending a nomination.']} ) instance.ended_at = timezone.now() @@ -286,13 +286,13 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge # 4. We are altering the reviewed state of the nomination. if not instance.active: raise ValidationError( - {'reviewed': ['This field cannot be set if nomination is inactive.']} + {'reviewed': ['This field cannot be set if the nomination is inactive.']} ) if 'reason' in request.data: if 'actor' not in request.data: raise ValidationError( - {'actor': ['This field is required when editing reason.']} + {'actor': ['This field is required when editing the reason.']} ) entry_filter = NominationEntry.objects.filter( -- cgit v1.2.3 From ce5a082458d8cc1433ce623d3b666fb6e0f82672 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 6 Mar 2021 14:11:56 +0200 Subject: Change as -> to in nomination viewset docs --- pydis_site/apps/api/viewsets/bot/nomination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 451264f4..9b9aa280 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -87,7 +87,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge 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. If `user` already has an - active nomination, a new nomination entry will be created and assigned as the + active nomination, a new nomination entry will be created and assigned to the active nomination. #### Request body -- cgit v1.2.3 From 9202d92d3cb27111eaf5d3cc1aee859f9cbe8918 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 6 Mar 2021 14:12:56 +0200 Subject: Fix grammar of nomination viewset command about single entry for user --- pydis_site/apps/api/viewsets/bot/nomination.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 9b9aa280..0792a2a6 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -216,7 +216,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge ) entry_serializer.is_valid(raise_exception=True) - # Don't allow user creating many nomination entries for one nomination + # Don't allow a user to create many nomination entries in a single nomination if NominationEntry.objects.filter( nomination_id=nomination_filter[0].id, actor__id=entry_serializer.validated_data["actor"].id -- cgit v1.2.3 From 48eff5d27715acd658f6284f4d4e1cdbbf71bee6 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 6 Mar 2021 14:13:50 +0200 Subject: Fix grammar of unknown actor error and change tests --- pydis_site/apps/api/tests/test_nominations.py | 2 +- pydis_site/apps/api/viewsets/bot/nomination.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/tests/test_nominations.py b/pydis_site/apps/api/tests/test_nominations.py index c07679c5..9cefbd8f 100644 --- a/pydis_site/apps/api/tests/test_nominations.py +++ b/pydis_site/apps/api/tests/test_nominations.py @@ -528,5 +528,5 @@ class NominationTests(APISubdomainTestCase): response = self.client.patch(url, data=data) self.assertEqual(response.status_code, 400) self.assertEqual(response.json(), { - 'actor': ["Actor don't exist or have not nominated user."] + 'actor': ["The actor doesn't exist or has not nominated the user."] }) diff --git a/pydis_site/apps/api/viewsets/bot/nomination.py b/pydis_site/apps/api/viewsets/bot/nomination.py index 0792a2a6..144daab0 100644 --- a/pydis_site/apps/api/viewsets/bot/nomination.py +++ b/pydis_site/apps/api/viewsets/bot/nomination.py @@ -302,7 +302,7 @@ class NominationViewSet(CreateModelMixin, RetrieveModelMixin, ListModelMixin, Ge if not entry_filter.exists(): raise ValidationError( - {'actor': ["Actor don't exist or have not nominated user."]} + {'actor': ["The actor doesn't exist or has not nominated the user."]} ) entry = entry_filter[0] -- cgit v1.2.3 From 4f9c088f6b0458eb0ebb52ef899cdfdc57f2c43c Mon Sep 17 00:00:00 2001 From: Boris Muratov <8bee278@gmail.com> Date: Sun, 7 Mar 2021 00:59:41 +0200 Subject: Add route to get a member's data for helper review Added route for getting a user's join date, total messages, and top 3 channels by activity. This information will be used to auto-review nominees. --- postgres/init.sql | 18 ++++++++++++- pydis_site/apps/api/models/bot/metricity.py | 39 +++++++++++++++++++++++++++++ pydis_site/apps/api/viewsets/bot/user.py | 15 +++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/postgres/init.sql b/postgres/init.sql index 740063e7..ae86fca0 100644 --- a/postgres/init.sql +++ b/postgres/init.sql @@ -13,12 +13,28 @@ INSERT INTO users VALUES ( current_timestamp ); +CREATE TABLE channels ( + id varchar, + name varchar, + primary key(id) +); + +INSERT INTO channels VALUES( + '267659945086812160', + 'python-general' +); + +INSERT INTO channels VALUES( + '1234', + 'zebra' +); + CREATE TABLE messages ( id varchar, author_id varchar references users(id), is_deleted boolean, created_at timestamp, - channel_id varchar, + channel_id varchar references channels(id), primary key(id) ); diff --git a/pydis_site/apps/api/models/bot/metricity.py b/pydis_site/apps/api/models/bot/metricity.py index cae630f1..af5e1f3b 100644 --- a/pydis_site/apps/api/models/bot/metricity.py +++ b/pydis_site/apps/api/models/bot/metricity.py @@ -89,3 +89,42 @@ class Metricity: raise NotFound() return values[0] + + def top_channel_activity(self, user_id: str) -> int: + """ + Query the top three channels in which the user is most active. + + Help channels are grouped under "the help channels", + and off-topic channels are grouped under "off-topic". + """ + self.cursor.execute( + """ + SELECT + CASE + WHEN channels.name ILIKE 'help-%%' THEN 'the help channels' + WHEN channels.name ILIKE 'ot%%' THEN 'off-topic' + ELSE channels.name + END, + COUNT(1) + FROM + messages + LEFT JOIN channels ON channels.id = messages.channel_id + WHERE + author_id = '%s' + GROUP BY + 1 + ORDER BY + 2 DESC + LIMIT + 3; + """, + [user_id] + ) + + values = self.cursor.fetchall() + print(values) + + if not values: + raise NotFound() + + return values diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 829e2694..5e1f8775 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -262,3 +262,18 @@ class UserViewSet(ModelViewSet): except NotFound: return Response(dict(detail="User not found in metricity"), status=status.HTTP_404_NOT_FOUND) + + @action(detail=True) + def metricity_review_data(self, request: Request, pk: str = None) -> Response: + """Request handler for metricity_review_data endpoint.""" + user = self.get_object() + + with Metricity() as metricity: + try: + data = metricity.user(user.id) + data["total_messages"] = metricity.total_messages(user.id) + data["top_channel_activity"] = metricity.top_channel_activity(user.id) + return Response(data, status=status.HTTP_200_OK) + except NotFound: + return Response(dict(detail="User not found in metricity"), + status=status.HTTP_404_NOT_FOUND) -- cgit v1.2.3 From 5e2ebf3bf8c6a1baf88c84137b78019196068110 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Sun, 7 Mar 2021 20:29:59 +0530 Subject: Update doc in pydis_site/apps/api/viewsets/bot/infraction.py Co-authored-by: Mark --- pydis_site/apps/api/viewsets/bot/infraction.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 1bf89203..9d5076d9 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -49,8 +49,7 @@ class InfractionViewSet( Invalid query parameters are ignored. #### Response format - - Response are paginated but the result is returned without any pagination metadata. - Below is the returned format. + Response is paginated but the result is returned without any pagination metadata. >>> [ ... { ... 'id': 5, -- cgit v1.2.3 From 0a7bffb0bb793517bb36136c847e13d439ce1ef9 Mon Sep 17 00:00:00 2001 From: Kunal Sharma Date: Sun, 7 Mar 2021 20:30:13 +0530 Subject: Update doc in pydis_site/apps/api/viewsets/bot/infraction.py Co-authored-by: Mark --- pydis_site/apps/api/viewsets/bot/infraction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 9d5076d9..bd512ddd 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -39,8 +39,8 @@ class InfractionViewSet( - **active** `bool`: whether the infraction is still active - **actor__id** `int`: snowflake of the user which applied the infraction - **hidden** `bool`: whether the infraction is a shadow infraction - - **limit** `int`: number of results return per page. default limit is 100 - - **offset** `int`: the initial index from which to return the results, default is 0 + - **limit** `int`: number of results return per page (default 100) + - **offset** `int`: the initial index from which to return the results (default 0) - **search** `str`: regular expression applied to the infraction's reason - **type** `str`: the type of the infraction - **user__id** `int`: snowflake of the user to which the infraction was applied -- cgit v1.2.3 From f4a67489b81f95978912582189cb23afb2169e8e Mon Sep 17 00:00:00 2001 From: Boris Muratov <8bee278@gmail.com> Date: Fri, 12 Mar 2021 15:35:47 +0200 Subject: Document endpoint in viewset docstring --- pydis_site/apps/api/viewsets/bot/user.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index 5e1f8775..25722f5a 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -119,6 +119,22 @@ class UserViewSet(ModelViewSet): - 200: returned on success - 404: if a user with the given `snowflake` could not be found + ### GET /bot/users//metricity_review_data + Gets metricity data for a single user's review by ID. + + #### Response format + >>> { + ... 'joined_at': '2020-08-26T08:09:43.507000', + ... 'top_channel_activity': [['off-topic', 15], + ... ['talent-pool', 4], + ... ['defcon', 2]], + ... 'total_messages': 22 + ... } + + #### Status codes + - 200: returned on success + - 404: if a user with the given `snowflake` could not be found + ### POST /bot/users Adds a single or multiple new users. The roles attached to the user(s) must be roles known by the site. -- cgit v1.2.3