Skip to content

Commit b92aa76

Browse files
committed
Soft-delete reminders on DELETE instead of removing entirely
1 parent e1ae079 commit b92aa76

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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)