Skip to content

Commit f0b3a9b

Browse files
authored
Merge pull request #1315 from python-discord/soft-delete-reminders
Soft-delete reminders on DELETE instead of removing entirely
2 parents b41b582 + 3c1c958 commit f0b3a9b

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pydis_site/apps/api/tests/test_reminders.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def test_delete_known_reminder_returns_204(self):
114114
response = self.client.delete(url)
115115

116116
self.assertEqual(response.status_code, 204)
117-
self.assertRaises(Reminder.DoesNotExist, Reminder.objects.get, id=self.reminder.id)
118117

119118

120119
class ReminderListTests(AuthenticatedAPITestCase):
@@ -154,18 +153,26 @@ def test_reminders_in_full_list(self):
154153
response = self.client.get(url)
155154

156155
self.assertEqual(response.status_code, 200)
156+
self.assertNotIn(self.rem_dict_two, response.json())
157+
158+
response = self.client.get(f"{url}?include_inactive=true")
157159
self.assertCountEqual(response.json(), [self.rem_dict_one, self.rem_dict_two])
158160

159161
def test_filter_search(self):
160162
url = reverse('api:bot:reminder-list')
161163
response = self.client.get(f'{url}?search={self.author.name}')
164+
self.assertEqual(response.status_code, 200)
165+
self.assertCountEqual(response.json(), [self.rem_dict_one])
162166

167+
def test_filter_search_with_inactive(self):
168+
url = reverse('api:bot:reminder-list')
169+
response = self.client.get(f'{url}?search={self.author.name}&include_inactive=true')
163170
self.assertEqual(response.status_code, 200)
164171
self.assertCountEqual(response.json(), [self.rem_dict_one, self.rem_dict_two])
165172

166-
def test_filter_field(self):
173+
def test_only_active_by_default(self):
167174
url = reverse('api:bot:reminder-list')
168-
response = self.client.get(f'{url}?active=true')
175+
response = self.client.get(url)
169176

170177
self.assertEqual(response.status_code, 200)
171178
self.assertEqual(response.json(), [self.rem_dict_one])

pydis_site/apps/api/viewsets/bot/reminder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django_filters.rest_framework import DjangoFilterBackend
2+
from rest_framework.request import Request
23
from rest_framework.filters import SearchFilter
34
from rest_framework.mixins import (
45
CreateModelMixin,
@@ -113,6 +114,7 @@ class ReminderViewSet(
113114
114115
### DELETE /bot/reminders/<id:int>
115116
Delete the reminder with the given `id`.
117+
This is a soft-delete by setting `active` to False.
116118
117119
#### Status codes
118120
- 204: returned on success
@@ -126,3 +128,19 @@ class ReminderViewSet(
126128
queryset = Reminder.objects.prefetch_related('author')
127129
filter_backends = (DjangoFilterBackend, SearchFilter)
128130
filterset_fields = ('active', 'author__id')
131+
132+
def perform_destroy(self, instance: Reminder) -> None:
133+
"""Soft-delete reminders when DELETE is called."""
134+
instance.active = False
135+
instance.save()
136+
137+
def get_queryset(self) -> list[Reminder]:
138+
"""Filter out soft-deleted reminders by default."""
139+
queryset = Reminder.objects.prefetch_related('author')
140+
request: Request = self.request
141+
142+
include_inactive = request.query_params.get("include_inactive", "false")
143+
include_inactive = include_inactive.lower() == "true"
144+
if not include_inactive:
145+
queryset = queryset.filter(active=True)
146+
return queryset

0 commit comments

Comments
 (0)