Skip to content

Commit

Permalink
chore: create flag and custom manager
Browse files Browse the repository at this point in the history
  • Loading branch information
moonlitgrace committed Dec 14, 2024
1 parent 40a7e25 commit a5feb39
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 141 deletions.
18 changes: 18 additions & 0 deletions backend/apps/comment/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import models

from .models import CommentModel


class CommentManager(models.Manager):
def soft_delete(self, instance: CommentModel):
# if no children- hard delete
if instance.children_count == 0:
instance.delete()
# else- soft delete
else:
instance.deleted = True
instance.content = "[deleted]"
instance.save()

def clean_up_soft_deleted(self):
self.filter(deleted=True, children_count=0).delete()
6 changes: 4 additions & 2 deletions backend/apps/comment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class CommentModel(CreatedAtMixin, TreeModel):
quibbler = models.ForeignKey(
ProfileModel, on_delete=models.CASCADE, verbose_name=_('quibbler')
ProfileModel, on_delete=models.SET_NULL, null=True, verbose_name=_('quibbler')
)
content = models.TextField(_('content'))
upvotes = models.ManyToManyField(
Expand All @@ -20,13 +20,15 @@ class CommentModel(CreatedAtMixin, TreeModel):
downvotes = models.ManyToManyField(
ProfileModel, related_name='downvotes', blank=True, verbose_name=_('downvotes')
)
# flag
deleted = models.BooleanField(default=False)

@property
def children_count(self):
return self.children().count()

def __str__(self) -> str:
return f"Comment by {self.quibbler.username}"
return f"Comment by {self.quibbler}"

class Meta: # pyright: ignore
indexes = [idx.GistIndex(fields=['path'])]
Expand Down
Empty file added backend/locale/.gitkeep
Empty file.
139 changes: 0 additions & 139 deletions backend/locale/en/LC_MESSAGES/django.po

This file was deleted.

0 comments on commit a5feb39

Please sign in to comment.