aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/api/viewsets/bot/nomination.py
blob: 10325c3d05b7d7eeeb010edff9f22eb6b440a4d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from collections import ChainMap

from django.http.request import HttpRequest
from django.utils import timezone
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import status
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 GenericViewSet

from pydis_site.apps.api.models.bot import Nomination, NominationEntry
from pydis_site.apps.api.serializers import NominationEntrySerializer, NominationSerializer


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
    - **reviewed** `bool`: whether the nomination has been voted on/is being voted on
    - **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,
    ...         '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',
    ...         'entries': [
    ...             {
    ...                'actor': 336843820513755157,
    ...                'reason': 'They know how to explain difficult concepts',
    ...                'inserted_at': '2019-04-25T14:02:37.775587Z'
    ...             }
    ...         ],
    ...         'reviewed': true
    ...     }
    ... ]

    #### Status codes
    - 200: returned on success

    ### GET /bot/nominations/<id:int>
    Retrieve a single nomination by ID.

    ### Response format
    >>> {
    ...     'id': 1,
    ...     'active': true,
    ...     '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',
    ...     'entries': [
    ...         {
    ...             'actor': 336843820513755157,
    ...             'reason': 'They know how to explain difficult concepts',
    ...             'inserted_at': '2019-04-25T14:02:37.775587Z'
    ...         }
    ...     ],
    ...     'reviewed': false
    ... }

    ### Status codes
    - 200: returned on success
    - 404: returned if a 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. If `user` already has an
    active nomination, a new nomination entry will be created and assigned to the
    active nomination.

    #### Request body
    >>> {
    ...     'actor': 409107086526644234
    ...     'reason': 'He would make a great helper',
    ...     'user': 409107086526644234
    ... }

    #### Response format
    See `GET /bot/nominations/<id:int>`

    #### Status codes
    - 201: returned on success
    - 400: returned on failure for one of the following reasons:
        - The `user` or `actor` are unknown to the site;
        - The request contained a field that cannot be set at creation.

    ### PATCH /bot/nominations/<id:int>
    Update or end the nomination with the given `id` and return the updated nomination.

    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.
    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
    that providing other valid fields is not allowed and invalid fields are ignored.

    ### 1. Updating the `reason` of `active` nomination. The `actor` field is required.

    #### Request body
    >>> {
    ...     'reason': 'He would make a great helper',
    ...     'actor': 409107086526644234
    ... }

    #### Response format
    See `GET /bot/nominations/<id:int>`

    #### 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

    ### 2. Ending an `active` nomination

    #### Request body
    >>> {
    ...     'active': False
    ...     'end_reason': 'They've been added to the Helpers team',
    ... }

    See operation 1 for the response format and status codes.

    ### 3. 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',
    ... }

    Note: The request body may contain either or both fields.

    See operation 1 for the response format and status codes.

    ### 4. Setting nomination `reviewed`

    #### Request body
    >>> {
    ...     'reviewed': True
    ... }

    See operation 1 for the response format and status codes.
    """

    serializer_class = NominationSerializer
    queryset = Nomination.objects.all().prefetch_related('entries')
    filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter)
    filterset_fields = ('user__id', 'active', 'reviewed')
    frozen_on_create = ('ended_at', 'end_reason', 'active', 'inserted_at', 'reviewed')

    def create(self, request: HttpRequest, *args, **kwargs) -> Response:
        """
        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.']})

        user_id = request.data.get("user")
        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)

            # The serializer will truncate and get rid of excessive data
            entry_serializer = NominationEntrySerializer(
                data=ChainMap(request.data, {"nomination": nomination.id})
            )
            entry_serializer.is_valid(raise_exception=True)
            NominationEntry.objects.create(**entry_serializer.validated_data)

            data = NominationSerializer(nomination).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})
        )
        entry_serializer.is_valid(raise_exception=True)

        # 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
        ).exists():
            raise ValidationError(
                {'actor': ['This actor has already endorsed this nomination.']}
            )

        NominationEntry.objects.create(**entry_serializer.validated_data)

        data = NominationSerializer(nomination_filter[0]).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:
        """
        DRF method for updating a Nomination.

        Called by the Django Rest Framework in response to the corresponding HTTP request.
        """
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)

        data = serializer.validated_data

        # There are three distinct PATCH scenarios we need to validate.
        if instance.active and 'active' not in data:
            # 1. We're updating an active nomination without ending it.
            if 'end_reason' in data:
                raise ValidationError(
                    {'end_reason': ["An active nomination can't have an end reason."]}
                )

        elif instance.active and not data['active']:
            # 2. We're ending an active nomination.
            if 'reason' in request.data:
                raise ValidationError(
                    {'reason': ['This field cannot be set when ending a nomination.']}
                )

            if 'end_reason' not in request.data:
                raise ValidationError(
                    {'end_reason': ['This field is required when ending a nomination.']}
                )

            if 'reviewed' in request.data:
                raise ValidationError(
                    {'reviewed': ['This field cannot be set while you are ending a nomination.']}
                )

            if 'thread_id' in request.data:
                raise ValidationError(
                    {'thread_id': ['This field cannot be set when ending a nomination.']}
                )

            instance.ended_at = timezone.now()

        elif 'active' in data:
            # 3. The `active` field is only allowed when ending a nomination.
            raise ValidationError(
                {'active': ['This field can only be used to end a nomination']}
            )

        elif not instance.active and 'reviewed' in request.data:
            raise ValidationError(
                {'reviewed': ['This field cannot be set if the nomination is inactive.']}
            )

        elif not instance.active and 'thread_id' in request.data:
            raise ValidationError(
                {'thread_id': ['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 the reason.']}
                )

            entry_filter = NominationEntry.objects.filter(
                nomination_id=instance.id,
                actor__id=request.data['actor']
            )

            if not entry_filter.exists():
                raise ValidationError(
                    {'actor': ["The actor doesn't exist or has not nominated the user."]}
                )

            entry = entry_filter[0]
            entry.reason = request.data['reason']
            entry.save()

        serializer.save()

        return Response(serializer.data)