Skip to content

Commit

Permalink
refactor: add cleanup manager function
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 14, 2024
1 parent a3cb20c commit d5a9cab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/apps/comment/api/v1/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ class CommentModelViewSet(viewsets.ModelViewSet):

def perform_destroy(self, instance):
CommentModel.objects.soft_delete(instance) # pyright: ignore
# perform cleanup
CommentModel.objects.clean_up_soft_deleted() # pyright: ignore
6 changes: 6 additions & 0 deletions backend/apps/comment/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ def soft_delete(self, instance):
instance.content = "[deleted]"
instance.quibbler = None
instance.save()

def clean_up_soft_deleted(self):
# cleanup all comment instances with no children
for comment_instance in self.filter(deleted=True):
if comment_instance.children_count == 0:
comment_instance.delete()

0 comments on commit d5a9cab

Please sign in to comment.