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
|
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.request import Request
from rest_framework.filters import SearchFilter
from rest_framework.mixins import (
CreateModelMixin,
DestroyModelMixin,
ListModelMixin,
RetrieveModelMixin,
UpdateModelMixin
)
from rest_framework.viewsets import GenericViewSet
from pydis_site.apps.api.models.bot.reminder import Reminder
from pydis_site.apps.api.serializers import ReminderSerializer
class ReminderViewSet(
CreateModelMixin,
RetrieveModelMixin,
ListModelMixin,
DestroyModelMixin,
UpdateModelMixin,
GenericViewSet,
):
"""
View providing CRUD access to reminders.
## Routes
### GET /bot/reminders
Returns all reminders in the database.
#### Response format
>>> [
... {
... 'active': True,
... 'author': 1020103901030,
... 'mentions': [
... 336843820513755157,
... 165023948638126080,
... 267628507062992896
... ],
... 'content': "Make dinner",
... 'expiration': '5018-11-20T15:52:00Z',
... 'id': 11,
... 'channel_id': 634547009956872193,
... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>",
... 'failures': 3
... },
... ...
... ]
#### Status codes
- 200: returned on success
### GET /bot/reminders/<id:int>
Fetches the reminder with the given id.
#### Response format
>>>
... {
... 'active': True,
... 'author': 1020103901030,
... 'mentions': [
... 336843820513755157,
... 165023948638126080,
... 267628507062992896
... ],
... 'content': "Make dinner",
... 'expiration': '5018-11-20T15:52:00Z',
... 'id': 11,
... 'channel_id': 634547009956872193,
... 'jump_url': "https://discord.com/channels/<guild_id>/<channel_id>/<message_id>",
... 'failures': 3
... }
#### Status codes
- 200: returned on success
- 404: returned when the reminder doesn't exist
### POST /bot/reminders
Create a new reminder.
#### Request body
>>> {
... 'author': int,
... 'mentions': list[int],
... 'content': str,
... 'expiration': str, # ISO-formatted datetime
... 'channel_id': int,
... 'jump_url': str
... }
#### Status codes
- 201: returned on success
- 400: if the body format is invalid
- 404: if no user with the given ID could be found
### PATCH /bot/reminders/<id:int>
Update the user with the given `id`.
All fields in the request body are optional.
#### Request body
>>> {
... 'mentions': list[int],
... 'content': str,
... 'expiration': str, # ISO-formatted datetime
... 'failures': int
... }
#### Status codes
- 200: returned on success
- 400: if the body format is invalid
- 404: if no user with the given ID could be found
### DELETE /bot/reminders/<id:int>
Delete the reminder with the given `id`.
This is a soft-delete by setting `active` to False.
#### Status codes
- 204: returned on success
- 404: if a reminder with the given `id` does not exist
## Authentication
Requires an API token.
"""
serializer_class = ReminderSerializer
queryset = Reminder.objects.prefetch_related('author')
filter_backends = (DjangoFilterBackend, SearchFilter)
filterset_fields = ('active', 'author__id')
def perform_destroy(self, instance: Reminder) -> None:
"""Soft-delete reminders when DELETE is called."""
instance.active = False
instance.save()
def get_queryset(self) -> list[Reminder]:
"""Filter out soft-deleted reminders by default."""
queryset = Reminder.objects.prefetch_related('author')
request: Request = self.request
include_inactive = request.query_params.get("include_inactive", "false")
include_inactive = include_inactive.lower() == "true"
if not include_inactive:
queryset = queryset.filter(active=True)
return queryset
|