Skip to content

Commit 447011d

Browse files
committed
fix: comment model manager inherit treemanager and comments get action
1 parent 49a5b26 commit 447011d

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

backend/apps/comment/managers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from django.db import models
1+
from django_ltree.managers import TreeManager
22

33

4-
class CommentManager(models.Manager):
4+
class CommentModelManager(TreeManager):
55
def soft_delete(self, instance):
66
# if no children- hard delete
77
if instance.children_count == 0:
@@ -15,6 +15,6 @@ def soft_delete(self, instance):
1515

1616
def clean_up_soft_deleted(self):
1717
# cleanup all comment instances with no children
18-
for comment_instance in self.filter(deleted=True):
19-
if comment_instance.children_count == 0:
20-
comment_instance.delete()
18+
for comment in self.filter(deleted=True):
19+
if comment.children_count == 0:
20+
comment.delete()

backend/apps/comment/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from apps.user.models import ProfileModel
77
from common.mixins.model_mixins import CreatedAtMixin
88

9-
from .managers import CommentManager
9+
from .managers import CommentModelManager
1010

1111
# Create your models here.
1212

@@ -28,7 +28,7 @@ class CommentModel(CreatedAtMixin, TreeModel):
2828
# flag
2929
deleted = models.BooleanField(default=False)
3030
# custom manager for soft-deletions handling
31-
objects = CommentManager()
31+
objects = CommentModelManager()
3232

3333
@property
3434
def children_count(self):

backend/apps/quib/api/v1/viewsets.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ def get_serializer_class(self): # pyright: ignore
1717
if self.action == 'list':
1818
return QuibSlimSerializer
1919
# if custom action: 'comment'
20-
if self.action == 'comment':
20+
if self.action == 'comments':
2121
return CommentSerializer
2222
return QuibSerializer
2323

24-
@action(detail=True, methods=[HTTPMethod.POST])
25-
def comment(self, request, pk=None):
24+
@action(detail=True, methods=[HTTPMethod.GET, HTTPMethod.POST])
25+
def comments(self, request, pk=None):
2626
quib_instance = get_object_or_404(QuibModel, pk=pk)
2727

28+
if request.method == HTTPMethod.GET:
29+
comments = quib_instance.comments.all()
30+
serializer = CommentSerializer(comments, many=True)
31+
32+
return response.Response(serializer.data, status=status.HTTP_200_OK)
33+
2834
serializer = CommentSerializer(data=request.data)
2935
serializer.is_valid(raise_exception=True)
3036

3137
comment_instance = serializer.save()
3238
quib_instance.comments.add(comment_instance)
3339

34-
return response.Response(status=status.HTTP_201_CREATED, data=serializer.data)
40+
return response.Response(serializer.data, status=status.HTTP_201_CREATED)

0 commit comments

Comments
 (0)