diff --git a/backend/apps/comment/api/v1/viewsets.py b/backend/apps/comment/api/v1/viewsets.py index 792641a..f51bd9b 100644 --- a/backend/apps/comment/api/v1/viewsets.py +++ b/backend/apps/comment/api/v1/viewsets.py @@ -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 diff --git a/backend/apps/comment/managers.py b/backend/apps/comment/managers.py index 128f357..d7e354d 100644 --- a/backend/apps/comment/managers.py +++ b/backend/apps/comment/managers.py @@ -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()