Skip to content

Commit

Permalink
更新评论回复
Browse files Browse the repository at this point in the history
  • Loading branch information
xishandong committed Jul 17, 2023
1 parent d0c91f7 commit 66d5446
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
Binary file modified Server01/views/__pycache__/comment.cpython-39.pyc
Binary file not shown.
31 changes: 28 additions & 3 deletions Server01/views/comment.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand All @@ -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)
Binary file modified webServer/__pycache__/settings.cpython-39.pyc
Binary file not shown.
Binary file modified webServer/__pycache__/urls.cpython-39.pyc
Binary file not shown.
3 changes: 2 additions & 1 deletion webServer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

0 comments on commit 66d5446

Please sign in to comment.