diff --git a/Server01/views/__pycache__/comment.cpython-39.pyc b/Server01/views/__pycache__/comment.cpython-39.pyc index 06a67c4..636f3b8 100644 Binary files a/Server01/views/__pycache__/comment.cpython-39.pyc and b/Server01/views/__pycache__/comment.cpython-39.pyc differ diff --git a/Server01/views/comment.py b/Server01/views/comment.py index 6f368a2..9decc82 100644 --- a/Server01/views/comment.py +++ b/Server01/views/comment.py @@ -1,8 +1,9 @@ import json +from django.db.models import Count from django.http import JsonResponse -from Server01.models import Comment, Post +from Server01.models import Comment from Server01.util.auxiliaryFuction import convert_to_timezone, filter_querySet from Server01.util.verifyJWT import authenticate_request from webServer.settings import TIME_ZONE @@ -21,7 +22,7 @@ def get_comment(request): data = json.loads(request.body) post_id = data['id'] offset = data['offset'] - comments = Post.objects.filter(id=post_id).first().comments.all() + comments = Comment.objects.filter(post_id=post_id, parent_comment=None).annotate(reply_count=Count('replies')) filter_comments = filter_querySet(comments, offset, limit=5) if filter_comments: data = [ @@ -33,10 +34,34 @@ def get_comment(request): 'id': comment.user.id, 'username': comment.user.username, 'avatar': comment.user.avatar - } + }, + 'replyCount': comment.reply_count, + 'replies': [] } for comment in filter_comments if comment ] return JsonResponse({'info': data}, status=200) return JsonResponse({'info': []}, status=200) +def load_reply(request): + data = json.loads(request.body) + id = data['id'] + offset = data['offset'] + comment = Comment.objects.filter(id=id).first() + if comment: + replies = comment.replies.all() + filter_replies = filter_querySet(replies, offset, limit=5) + data = [ + { + 'id': comment.id, + 'content': comment.content, + 'createTime': convert_to_timezone(comment.created_at, TIME_ZONE), + 'user': { + 'id': comment.user.id, + 'username': comment.user.username, + 'avatar': comment.user.avatar + }, + } for comment in filter_replies if comment + ] + return JsonResponse({'info': data, 'count': len(data)}, status=200) + return JsonResponse({'error': '错误的操作'}, status=404) \ No newline at end of file diff --git a/webServer/__pycache__/settings.cpython-39.pyc b/webServer/__pycache__/settings.cpython-39.pyc index 9cda3c6..fb35fda 100644 Binary files a/webServer/__pycache__/settings.cpython-39.pyc and b/webServer/__pycache__/settings.cpython-39.pyc differ diff --git a/webServer/__pycache__/urls.cpython-39.pyc b/webServer/__pycache__/urls.cpython-39.pyc index 0c9e907..f96b341 100644 Binary files a/webServer/__pycache__/urls.cpython-39.pyc and b/webServer/__pycache__/urls.cpython-39.pyc differ diff --git a/webServer/urls.py b/webServer/urls.py index 75324b5..e5e5f30 100644 --- a/webServer/urls.py +++ b/webServer/urls.py @@ -41,5 +41,6 @@ path('post/delete/', post.post_delete), # 评论相关 path('comment/', comment.do_comment), - path('comment/main/', comment.get_comment) + path('comment/main/', comment.get_comment), + path('comment/reply/', comment.load_reply) ]