Skip to content

Commit

Permalink
评论加载优化,变为无限滚动
Browse files Browse the repository at this point in the history
  • Loading branch information
xishandong committed Jul 14, 2023
1 parent 7dbbf17 commit f83c0b0
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 15 deletions.
Binary file removed Server01/static/img/avatar/10-lisa.jpg
Binary file not shown.
Binary file added Server01/static/img/avatar/10-金珍妮.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Server01/static/img/avatar/9-金智秀.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Server01/static/img/post/38-01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Server01/static/img/post/39-03.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Server01/util/__pycache__/verifyJWT.cpython-39.pyc
Binary file not shown.
Binary file modified Server01/views/__pycache__/comment.cpython-39.pyc
Binary file not shown.
Binary file modified Server01/views/__pycache__/post.cpython-39.pyc
Binary file not shown.
29 changes: 28 additions & 1 deletion Server01/views/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from django.http import JsonResponse

from Server01.models import Comment
from Server01.models import Comment, Post
from Server01.util.auxiliaryFuction import convert_to_timezone, filter_querySet
from Server01.util.verifyJWT import authenticate_request
from webServer.settings import TIME_ZONE


@authenticate_request
Expand All @@ -13,3 +15,28 @@ def do_comment(request, verify_payload):
data['user_id'] = user_id
comment = Comment.objects.create(**data)
return JsonResponse({'info': '评论已发送!', 'id': comment.id}, status=200)


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()
filter_comments = filter_querySet(comments, offset, limit=5)
if filter_comments:
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_comments if comment
]
return JsonResponse({'info': data}, status=200)
return JsonResponse({'info': []}, status=200)


14 changes: 1 addition & 13 deletions Server01/views/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@ def get_post_detail(request):
'title': post.title,
'id': post.id,
'imgs': [img.imagePath for img in imgs],
'comment': [
{
'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 post.comments.all()
],
'user': {
'id': post.user.id,
'username': post.user.username,
Expand All @@ -72,6 +59,7 @@ def get_post_detail(request):
'createTime': convert_to_timezone(post.created_at, TIME_ZONE),
'likeCount': post.favoritePosts.all().count(),
'collectCount': post.collectedPosts.all().count(),
'commentCount': post.comments.all().count(),
}
return JsonResponse({'info': info}, status=200)
return JsonResponse({'error': '错误的访问'}, status=404)
Expand Down
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 @@ -36,5 +36,6 @@
path('post/', post.query_post_index),
path('post/control/', post.control_like_collect),
# 评论相关
path('comment/', comment.do_comment)
path('comment/', comment.do_comment),
path('comment/main/', comment.get_comment)
]

0 comments on commit f83c0b0

Please sign in to comment.