From dc9a7f6c51bf5910382b3a8683f2f940c8b088a0 Mon Sep 17 00:00:00 2001 From: KJ Kim Date: Sun, 21 Apr 2024 14:25:27 +0900 Subject: [PATCH] changed replies list to descending order --- communities/models.py | 7 ++++--- tests/communities/test_reply.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/communities/models.py b/communities/models.py index 5faec4e..e244267 100644 --- a/communities/models.py +++ b/communities/models.py @@ -1,6 +1,7 @@ from django.db import models from django.db.models import ( Case, + F, IntegerField, Q, When, @@ -220,11 +221,11 @@ def thread(self, thread, user): replies = self.filter(thread_replies).annotate( custom_order=Case( - When(reply_id=0, then='id'), - default='reply_id', + When(reply_id=0, then=F('id')), + default=F('reply_id'), output_field=IntegerField(), ) - ).order_by('custom_order', 'id') + ).order_by('-custom_order', 'id') return replies def my(self, user): diff --git a/tests/communities/test_reply.py b/tests/communities/test_reply.py index 73d9580..da3b393 100644 --- a/tests/communities/test_reply.py +++ b/tests/communities/test_reply.py @@ -480,16 +480,16 @@ def test_reply_list(self): auth=True ) self.check(len(self.data), 5) - self.check(self.data[0].get('content'), '1') + self.check(self.data[0].get('content'), '5') self.check(self.data[0].get('reply_id'), 0) - self.check(self.data[1].get('content'), '2') - self.check(self.data[1].get('reply_id'), reply_id) - self.check(self.data[2].get('content'), '3') - self.check(self.data[2].get('reply_id'), reply_id) - self.check(self.data[3].get('content'), '4') - self.check(self.data[3].get('reply_id'), 0) - self.check(self.data[4].get('content'), '5') - self.check(self.data[4].get('reply_id'), 0) + self.check(self.data[1].get('content'), '4') + self.check(self.data[1].get('reply_id'), 0) + self.check(self.data[2].get('content'), '1') + self.check(self.data[2].get('reply_id'), 0) + self.check(self.data[3].get('content'), '2') + self.check(self.data[3].get('reply_id'), reply_id) + self.check(self.data[4].get('content'), '3') + self.check(self.data[4].get('reply_id'), reply_id) self.delete( '/api/communities/r/%d/' % self.data[4].get('id'),