Skip to content

Commit

Permalink
changed comments list to descending order
Browse files Browse the repository at this point in the history
  • Loading branch information
genonfire committed May 1, 2024
1 parent f83e1de commit e8c14e2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
13 changes: 10 additions & 3 deletions contents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import models
from django.db.models import (
Case,
F,
IntegerField,
Q,
When,
Expand Down Expand Up @@ -154,6 +155,9 @@ def like(self):
else:
return 0

def comment_count(self):
return Comment.objects.active_count(self)


class CommentManager(models.Manager):
def my(self, user):
Expand All @@ -165,6 +169,9 @@ def my(self, user):
def active(self):
return self.filter(is_deleted=False)

def active_count(self, blog):
return self.active().filter(blog=blog).count()

def deleted(self):
return self.filter(is_deleted=True)

Expand All @@ -179,11 +186,11 @@ def blog(self, blog, user):

comments = self.filter(blog_comments).annotate(
custom_order=Case(
When(comment_id=0, then='id'),
default='comment_id',
When(comment_id=0, then=F('id')),
default=F('comment_id'),
output_field=IntegerField(),
)
).order_by('custom_order', 'id')
).order_by('-custom_order', 'id')
return comments

def admin_query(self, q):
Expand Down
2 changes: 2 additions & 0 deletions contents/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Meta:
'image',
'tags',
'like',
'comment_count',
'is_published',
'created_at',
'modified_at',
Expand All @@ -155,6 +156,7 @@ class Meta:
'image',
'tags',
'like',
'comment_count',
'is_published',
'created_at',
'modified_at',
Expand Down
37 changes: 28 additions & 9 deletions tests/contents/test_blog_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,25 @@ def test_blog_comment_check_fields(self):
self.check_not(self.data.get('is_deleted'))
self.check(self.data.get('editable'))

def test_comment_check_count(self):
self.get(
'/api/contents/blogs/%d/' % self.blog.id,
auth=True
)
self.check(self.data.get('comment_count'), 1)

self.delete(
'/api/contents/comment/%d/' % self.comment.id,
auth=True
)
self.status(200)

self.get(
'/api/contents/blogs/%d/' % self.blog.id,
auth=True
)
self.check(self.data.get('comment_count'), 0)

def test_comment_update_permission(self):
self.patch(
'/api/contents/comment/%d/' % self.comment.id,
Expand Down Expand Up @@ -488,16 +507,16 @@ def test_comment_list(self):
auth=True
)
self.check(len(self.data), 5)
self.check(self.data[0].get('content'), 'A')
self.check(self.data[0].get('content'), 'E')
self.check(self.data[0].get('comment_id'), 0)
self.check(self.data[1].get('content'), 'B')
self.check(self.data[1].get('comment_id'), comment.id)
self.check(self.data[2].get('content'), 'C')
self.check(self.data[2].get('comment_id'), comment.id)
self.check(self.data[3].get('content'), 'D')
self.check(self.data[3].get('comment_id'), 0)
self.check(self.data[4].get('content'), 'E')
self.check(self.data[4].get('comment_id'), 0)
self.check(self.data[1].get('content'), 'D')
self.check(self.data[1].get('comment_id'), 0)
self.check(self.data[2].get('content'), 'A')
self.check(self.data[2].get('comment_id'), 0)
self.check(self.data[3].get('content'), 'B')
self.check(self.data[3].get('comment_id'), comment.id)
self.check(self.data[4].get('content'), 'C')
self.check(self.data[4].get('comment_id'), comment.id)

self.delete(
'/api/contents/comment/%d/' % comment.id,
Expand Down

0 comments on commit e8c14e2

Please sign in to comment.