aboutsummaryrefslogtreecommitdiffstats
path: root/api/viewsets.py
blob: de5ddaf667dc5e27d3b44b818555d8205853edbf (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
from django.shortcuts import get_object_or_404
from rest_framework.exceptions import ParseError
from rest_framework.mixins import (
    CreateModelMixin, DestroyModelMixin,
    ListModelMixin, RetrieveModelMixin
)
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED
from rest_framework.viewsets import GenericViewSet, ModelViewSet, ViewSet
from rest_framework_bulk import BulkCreateModelMixin

from .models import (
    DocumentationLink,
    OffTopicChannelName,
    SnakeFact, SnakeIdiom,
    SnakeName, SpecialSnake,
    Tag, User
)
from .serializers import (
    DocumentationLinkSerializer,
    OffTopicChannelNameSerializer,
    SnakeFactSerializer, SnakeIdiomSerializer,
    SnakeNameSerializer, SpecialSnakeSerializer,
    TagSerializer, UserSerializer
)


class DocumentationLinkViewSet(
    CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin, GenericViewSet
):
    """
    View providing management of documentation links used in the bot's `Doc` cog.

    ## Routes
    ### GET /bot/documentation-links
    Retrieve all currently stored entries from the database.

    #### Response format
    >>> [
    ...     {
    ...         'package': 'flask',
    ...         'base_url': 'https://flask.pocoo.org/docs/dev',
    ...         'inventory_url': 'https://flask.pocoo.org/docs/objects.inv'
    ...     },
    ...     # ...
    ... ]

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

    ### GET /bot/documentation-links/<package:str>
    Look up the documentation object for the given `package`.

    #### Response format
    >>> {
    ...     'package': 'flask',
    ...     'base_url': 'https://flask.pocoo.org/docs/dev',
    ...     'inventory_url': 'https://flask.pocoo.org/docs/objects.inv'
    ... }

    #### Status codes
    - 200: returned on success
    - 404: if no entry for the given `package` exists

    ### POST /bot/documentation-links
    Create a new documentation link object.

    #### Body schema
    >>> {
    ...     'package': str,
    ...     'base_url': URL,
    ...     'inventory_url': URL
    ... }

    #### Status codes
    - 201: returned on success
    - 400: if the request body has invalid fields, see the response for details

    ### DELETE /bot/documentation-links/<package:str>
    Delete the entry for the given `package`.

    #### Status codes
    - 204: returned on success
    - 404: if the given `package` could not be found
    """

    queryset = DocumentationLink.objects.all()
    serializer_class = DocumentationLinkSerializer
    lookup_field = 'package'


class OffTopicChannelNameViewSet(DestroyModelMixin, ViewSet):
    """
    View of off-topic channel names used by the bot
    to rotate our off-topic names on a daily basis.

    ## Routes
    ### GET /bot/off-topic-channel-names
    Return all known off-topic channel names from the database.
    If the `random_items` query parameter is given, for example using...
        $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?random_items=5
    ... then the API will return `5` random items from the database.

    #### Response format
    Return a list of off-topic-channel names:
    >>> [
    ...     "lemons-lemonade-stand",
    ...     "bbq-with-bisk"
    ... ]

    #### Status codes
    - 200: returned on success
    - 400: returned when `random_items` is not a positive integer

    ### POST /bot/off-topic-channel-names
    Create a new off-topic-channel name in the database.
    The name must be given as a query parameter, for example:
        $ curl api.pythondiscord.local:8000/bot/off-topic-channel-names?name=lemons-lemonade-shop

    #### Status codes
    - 201: returned on success
    - 400: if the request body has invalid fields, see the response for details

    ### DELETE /bot/off-topic-channel-names/<name:str>
    Delete the off-topic-channel name with the given `name`.

    #### Status codes
    - 204: returned on success
    - 404: returned when the given `name` was not found

    ## Authentication
    Requires a API token.
    """

    lookup_field = 'name'
    serializer_class = OffTopicChannelNameSerializer

    def get_object(self):
        queryset = self.get_queryset()
        name = self.kwargs[self.lookup_field]
        return get_object_or_404(queryset, name=name)

    def get_queryset(self):
        return OffTopicChannelName.objects.all()

    def create(self, request):
        if 'name' in request.query_params:
            create_data = {'name': request.query_params['name']}
            serializer = OffTopicChannelNameSerializer(data=create_data)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(create_data, status=HTTP_201_CREATED)

        else:
            raise ParseError(detail={
                'name': ["This query parameter is required."]
            })

    def list(self, request):  # noqa
        if 'random_items' in request.query_params:
            param = request.query_params['random_items']
            try:
                random_count = int(param)
            except ValueError:
                raise ParseError(detail={'random_items': ["Must be a valid integer."]})

            if random_count <= 0:
                raise ParseError(detail={
                    'random_items': ["Must be a positive integer."]
                })

            queryset = self.get_queryset().order_by('?')[:random_count]
            serialized = self.serializer_class(queryset, many=True)
            return Response(serialized.data)

        queryset = self.get_queryset()
        serialized = self.serializer_class(queryset, many=True)
        return Response(serialized.data)


class SnakeFactViewSet(ListModelMixin, GenericViewSet):
    """
    View providing snake facts created by the Pydis community in the first code jam.

    ## Routes
    ### GET /bot/snake-facts
    Returns snake facts from the database.

    #### Response format
    >>> [
    ...     {'fact': 'Snakes are dangerous'},
    ...     {'fact': 'Except for Python, we all love it'}
    ... ]

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

    ## Authentication
    Requires an API token.
    """

    serializer_class = SnakeFactSerializer
    queryset = SnakeFact.objects.all()


class SnakeIdiomViewSet(ListModelMixin, GenericViewSet):
    """
    View providing snake idioms for the snake cog.

    ## Routes
    ### GET /bot/snake-idioms
    Returns snake idioms from the database.

    #### Response format
    >>> [
    ...    {'idiom': 'Sneky snek'},
    ...    {'idiom': 'Snooky Snake'}
    ... ]

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

    ## Authentication
    Requires an API token
    """

    serializer_class = SnakeIdiomSerializer
    queryset = SnakeIdiom.objects.all()


class SnakeNameViewSet(ViewSet):
    """
    View providing snake names for the bot's snake cog from our first code jam's winners.

    ## Routes
    ### GET /bot/snake-names
    By default, return a single random snake name along with its name and scientific name.
    If the `get_all` query parameter is given, for example using...
        $ curl api.pythondiscord.local:8000/bot/snake-names?get_all=yes
    ... then the API will return all snake names and scientific names in the database.

    #### Response format
    Without `get_all` query parameter:
    >>> {
    ...     'name': "Python",
    ...     'scientific': "Langus greatus"
    ... }

    If the database is empty for whatever reason, this will return an empty dictionary.

    With `get_all` query parameter:
    >>> [
    ...     {'name': "Python 3", 'scientific': "Langus greatus"},
    ...     {'name': "Python 2", 'scientific': "Langus decentus"}
    ... ]

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

    ## Authentication
    Requires a API token.
    """

    serializer_class = SnakeNameSerializer

    def get_queryset(self):
        return SnakeName.objects.all()

    def list(self, request):  # noqa
        if request.query_params.get('get_all'):
            queryset = self.get_queryset()
            serialized = self.serializer_class(queryset, many=True)
            return Response(serialized.data)

        single_snake = SnakeName.objects.order_by('?').first()
        if single_snake is not None:
            body = {
                'name': single_snake.name,
                'scientific': single_snake.scientific
            }

            return Response(body)

        return Response({})


class SpecialSnakeViewSet(ListModelMixin, GenericViewSet):
    """
    View providing special snake names for our bot's snake cog.

    ## Routes
    ### GET /bot/special-snakes
    Returns a list of special snake names.

    #### Response Format
    >>> [
    ...   {
    ...     'name': 'Snakky sneakatus',
    ...     'info': 'Scary snek',
    ...     'image': 'https://discordapp.com/assets/53ef346458017da2062aca5c7955946b.svg'
    ...   }
    ... ]

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

    ## Authentication
    Requires an API token.
    """

    serializer_class = SpecialSnakeSerializer
    queryset = SpecialSnake.objects.all()


class TagViewSet(ModelViewSet):
    """
    View providing CRUD operations on tags shown by our bot.

    ## Routes
    ### GET /bot/tags
    Returns all tags in the database.

    #### Response format
    >>> [
    ...     {
    ...         'title': "resources",
    ...         'embed': {
    ...             'content': "Did you really think I'd put something useful here?"
    ...         }
    ...     }
    ... ]

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

    ### GET /bot/tags/<title:str>
    Gets a single tag by its title.

    #### Response format
    >>> {
    ...     'title': "My awesome tag",
    ...     'embed': {
    ...         'content': "totally not filler words"
    ...     }
    ... }

    #### Status codes
    - 200: returned on success
    - 404: if a tag with the given `title` could not be found

    ### POST /bot/tags
    Adds a single tag to the database.

    #### Request body
    >>> {
    ...     'title': str,
    ...     'embed': dict
    ... }

    The embed structure is the same as the embed structure that the Discord API
    expects. You can view the documentation for it here:
        https://discordapp.com/developers/docs/resources/channel#embed-object

    #### Status codes
    - 201: returned on success
    - 400: if one of the given fields is invalid

    ### PUT /bot/members/<title:str>
    Update the tag with the given `title`.

    #### Request body
    >>> {
    ...     'title': str,
    ...     'embed': dict
    ... }

    The embed structure is the same as the embed structure that the Discord API
    expects. You can view the documentation for it here:
        https://discordapp.com/developers/docs/resources/channel#embed-object

    #### Status codes
    - 200: returned on success
    - 400: if the request body was invalid, see response body for details
    - 404: if the tag with the given `title` could not be found

    ### PATCH /bot/members/<title:str>
    Update the tag with the given `title`.

    #### Request body
    >>> {
    ...     'title': str,
    ...     'embed': dict
    ... }

    The embed structure is the same as the embed structure that the Discord API
    expects. You can view the documentation for it here:
        https://discordapp.com/developers/docs/resources/channel#embed-object

    #### Status codes
    - 200: returned on success
    - 400: if the request body was invalid, see response body for details
    - 404: if the tag with the given `title` could not be found

    ### DELETE /bot/members/<title:str>
    Deletes the tag with the given `title`.

    #### Status codes
    - 204: returned on success
    - 404: if a tag with the given `title` does not exist
    """

    serializer_class = TagSerializer
    queryset = Tag.objects.all()


class UserViewSet(BulkCreateModelMixin, ModelViewSet):
    """
    View providing CRUD operations on Discord users through the bot.

    ## Routes
    ### GET /bot/users
    Returns all users currently known.

    #### Response format
    >>> [
    ...     {
    ...         'id': 409107086526644234,
    ...         'avatar': "3ba3c1acce584c20b1e96fc04bbe80eb",
    ...         'name': "Python",
    ...         'discriminator': 4329,
    ...         'roles': [
    ...             352427296948486144,
    ...             270988689419665409,
    ...             277546923144249364,
    ...             458226699344019457
    ...         ],
    ...         'in_guild': True
    ...     }
    ... ]

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

    ### GET /bot/users/<snowflake:int>
    Gets a single user by ID.

    #### Response format
    >>> {
    ...     'id': 409107086526644234,
    ...     'avatar': "3ba3c1acce584c20b1e96fc04bbe80eb",
    ...     'name': "Python",
    ...     'discriminator': 4329,
    ...     'roles': [
    ...         352427296948486144,
    ...         270988689419665409,
    ...         277546923144249364,
    ...         458226699344019457
    ...     ],
    ...     'in_guild': True
    ... }

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

    #### Request body
    >>> {
    ...     'id': int,
    ...     'avatar': str,
    ...     'name': str,
    ...     'discriminator': int,
    ...     'roles': List[int],
    ...     'in_guild': bool
    ... }

    Alternatively, request users can be POSTed as a list of above objects,
    in which case multiple users will be created at once.

    #### Status codes
    - 201: returned on success
    - 400: if one of the given roles does not exist, or one of the given fields is invalid

    ### PUT /bot/users/<snowflake:int>
    Update the user with the given `snowflake`.
    All fields in the request body are required.

    #### Request body
    >>> {
    ...     'id': int,
    ...     'avatar': str,
    ...     'name': str,
    ...     'discriminator': int,
    ...     'roles': List[int],
    ...     'in_guild': bool
    ... }

    #### Status codes
    - 200: returned on success
    - 400: if the request body was invalid, see response body for details
    - 404: if the user with the given `snowflake` could not be found

    ### PATCH /bot/users/<snowflake:int>
    Update the user with the given `snowflake`.
    All fields in the request body are optional.

    #### Request body
    >>> {
    ...     'id': int,
    ...     'avatar': str,
    ...     'name': str,
    ...     'discriminator': int,
    ...     'roles': List[int],
    ...     'in_guild': bool
    ... }

    #### Status codes
    - 200: returned on success
    - 400: if the request body was invalid, see response body for details
    - 404: if the user with the given `snowflake` could not be found

    ### DELETE /bot/users/<snowflake:int>
    Deletes the user with the given `snowflake`.

    #### Status codes
    - 204: returned on success
    - 404: if a user with the given `snowflake` does not exist
    """

    serializer_class = UserSerializer
    queryset = User.objects.all()