Skip to content

Commit

Permalink
Soft-delete reminders on DELETE instead of removing entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed May 9, 2024
1 parent 3d59587 commit 4d7376a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pydis_site/apps/api/viewsets/bot/reminder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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,
Expand Down Expand Up @@ -113,6 +114,7 @@ class ReminderViewSet(
### 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
Expand All @@ -123,6 +125,20 @@ class ReminderViewSet(
"""

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_query_set(self) -> list[Reminder]:
"""Filter out soft-deleted reminders by default."""
queryset = Reminder.objects.prefetch_related('author')

request: Request = self.request
only_active = bool(request.query_params.get("active", True))
if only_active:
queryset = queryset.filter(active=True)
return queryset

0 comments on commit 4d7376a

Please sign in to comment.