From d5a9cabcc30d1abdcf54352cd9a59247287b7d99 Mon Sep 17 00:00:00 2001 From: moonlitgrace Date: Sat, 14 Dec 2024 16:37:05 +0530 Subject: [PATCH] refactor: add cleanup manager function --- backend/apps/comment/api/v1/viewsets.py | 2 ++ backend/apps/comment/managers.py | 6 ++++++ 2 files changed, 8 insertions(+) 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()