Skip to content

Commit

Permalink
完成收藏点赞逻辑以及主页优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xishandong committed Jul 12, 2023
1 parent 1879c7e commit 7dbbf17
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 19 deletions.
Binary file added Server01/static/img/post/35-search.png
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/36-池秀媛.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/37-02.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 modified Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc
Binary file not shown.
Binary file modified Server01/util/__pycache__/verifyJWT.cpython-39.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions Server01/util/auxiliaryFuction.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ def check_and_delete(id, mainPath):
if file_name.startswith(f'{id}-'):
file_path = os.path.join(mainPath, file_name)
os.remove(file_path)


def filter_querySet(querySet, offset, limit=20):
limit = limit # 每页显示的帖子数量
count = querySet.count()
if 0 <= offset < count:
start = offset
end = offset + limit
filterQuerySet = querySet.order_by('-id')[start:end]
return filterQuerySet
return []
4 changes: 2 additions & 2 deletions Server01/util/verifyJWT.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def wrapper(request, *args, **kwargs):
error_message = {'error': '非法的token'}
return JsonResponse(error_message, status=401)
except AttributeError as e:
error_message = {'error': str(e)}
return JsonResponse(error_message, status=401)
error_message = {'error': '未查询到登录信息,请重新登录'}
return JsonResponse(error_message, status=403)

return wrapper

Expand Down
Binary file modified Server01/views/__pycache__/post.cpython-39.pyc
Binary file not shown.
Binary file modified Server01/views/__pycache__/user.cpython-39.pyc
Binary file not shown.
42 changes: 32 additions & 10 deletions Server01/views/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.http import JsonResponse

import Server01.models as models
from Server01.util.auxiliaryFuction import convert_to_timezone, combine_index_post
from Server01.util.auxiliaryFuction import convert_to_timezone, combine_index_post, filter_querySet
from Server01.util.verifyJWT import authenticate_request
from webServer.settings import TIME_ZONE, SYSTEM_PATH

Expand Down Expand Up @@ -69,7 +69,9 @@ def get_post_detail(request):
'username': post.user.username,
'avatar': post.user.avatar
},
'createTime': convert_to_timezone(post.created_at, TIME_ZONE)
'createTime': convert_to_timezone(post.created_at, TIME_ZONE),
'likeCount': post.favoritePosts.all().count(),
'collectCount': post.collectedPosts.all().count(),
}
return JsonResponse({'info': info}, status=200)
return JsonResponse({'error': '错误的访问'}, status=404)
Expand All @@ -80,7 +82,6 @@ def query_post_index(request):
data = json.loads(request.body)
offset = data['offset']
query = data.get('query')
limit = 20 # 每页显示的帖子数量
if query:
posts = models.Post.objects.filter(
Q(title__icontains=query) |
Expand All @@ -89,12 +90,33 @@ def query_post_index(request):
)
else:
posts = models.Post.objects.all()
count = posts.count()

if 0 <= offset < count:
start = offset
end = offset + limit
posts = posts.order_by('-id')[start:end]
posts = filter_querySet(posts, offset, limit=10)
if posts:
return JsonResponse({'info': list(combine_index_post(posts))}, status=200)

# 没有内容了
return JsonResponse({'info': []}, status=200)


@authenticate_request
def control_like_collect(request, payload):
user_id = payload['user_id']
data = json.loads(request.body)
operation = data['operator']
post_id = data['post_id']
types = data['type']
user = models.User.objects.filter(id=user_id).first()
post = models.Post.objects.filter(id=post_id).first()
if user and post:
if types == 'like':
if not operation:
user.favorites.add(post)
return JsonResponse({'info': '成功添加喜欢'}, status=200)
user.favorites.remove(post)
return JsonResponse({'info': '成功取消喜欢'}, status=200)
elif types == 'collect':
if not operation:
user.collected.add(post)
return JsonResponse({'info': '成功添加收藏'}, status=200)
user.collected.remove(post)
return JsonResponse({'info': '成功取消收藏'}, status=200)
return JsonResponse({'error': '错误的操作'}, status=404)
38 changes: 31 additions & 7 deletions Server01/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.http import JsonResponse

import Server01.models as models
from Server01.util.auxiliaryFuction import check_email, combine_index_post, check_and_delete
from Server01.util.auxiliaryFuction import check_email, combine_index_post, check_and_delete, filter_querySet
from Server01.util.verifyJWT import create_token, authenticate_request
from webServer.settings import SYSTEM_PATH

Expand Down Expand Up @@ -40,10 +40,10 @@ def register(request):
return JsonResponse({'error': '创建用户失败'}, status=401)


# 获取用户主页信息
# 获取用户主页的个人信息
def query_user_index(request):
data = json.loads(request.body)
if data.get('id'):
if data.get('id') and data.get('id') != 'undefined':
user = models.User.objects.filter(id=data.get('id')).first()
if user:
author = {
Expand All @@ -57,23 +57,47 @@ def query_user_index(request):
}
info = {
'user': author,
'posts': list(combine_index_post(user.posts.all())),
'collected': list(combine_index_post(user.collected.all())),
'favorites': list(combine_index_post(user.favorites.all()))
}
return JsonResponse({'data': info}, status=200)
return JsonResponse({'error': '错误的访问'}, status=404)
return JsonResponse({'error': '非法访问'}, status=404)


def query_user_index_post(request):
type_mapping = {
'帖子': 'posts',
'点赞': 'favorites',
'收藏': 'collected',
}
data = json.loads(request.body)
user_id = data['user_id']
types = data['types']
offset = data['offset']
user = models.User.objects.filter(id=user_id).first()
if user and types in type_mapping:
field_name = type_mapping[types]
postObj = getattr(user, field_name).all()
posts = filter_querySet(postObj, offset, limit=10)
if posts:
return JsonResponse({'info': list(combine_index_post(posts))}, status=200)
return JsonResponse({'info': []}, status=200)
return JsonResponse({'error': '错误访问'}, status=404)


# 获取用户关注用户id
@authenticate_request
def get_user_focus(request, payload):
user_id = payload['user_id']
user = models.User.objects.filter(id=user_id).first()
following = user.following.all()
ids = [u.id for u in following]
return JsonResponse({'info': ids}, status=200)
collected = user.collected.all()
c_ids = [u.id for u in collected]
favorites = user.favorites.all()
f_ids = [u.id for u in favorites]
return JsonResponse({'info': {
'follow': ids, 'collected': c_ids, 'favorites': f_ids
}}, status=200)


# 用户关注
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.
2 changes: 2 additions & 0 deletions webServer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
path('user/unfollow/', user.unfollow),
path('user/update/', user.update_user_info),
path('user/avatar/', user.update_avatar),
path('user/post/', user.query_user_index_post),
# 帖子相关
path('upload/', post.upload_post),
path('upload/info/', post.upload_post_info),
path('post/detail/', post.get_post_detail),
path('post/', post.query_post_index),
path('post/control/', post.control_like_collect),
# 评论相关
path('comment/', comment.do_comment)
]

0 comments on commit 7dbbf17

Please sign in to comment.