diff --git a/Server01/static/img/post/35-search.png b/Server01/static/img/post/35-search.png new file mode 100644 index 0000000..1b7b14f Binary files /dev/null and b/Server01/static/img/post/35-search.png differ diff --git "a/Server01/static/img/post/36-\346\261\240\347\247\200\345\252\233.jpg" "b/Server01/static/img/post/36-\346\261\240\347\247\200\345\252\233.jpg" new file mode 100644 index 0000000..a77b691 Binary files /dev/null and "b/Server01/static/img/post/36-\346\261\240\347\247\200\345\252\233.jpg" differ diff --git a/Server01/static/img/post/37-02.jpg b/Server01/static/img/post/37-02.jpg new file mode 100644 index 0000000..b5e7bfd Binary files /dev/null and b/Server01/static/img/post/37-02.jpg differ diff --git a/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc b/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc index 0584259..a2fbc21 100644 Binary files a/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc and b/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc differ diff --git a/Server01/util/__pycache__/verifyJWT.cpython-39.pyc b/Server01/util/__pycache__/verifyJWT.cpython-39.pyc index 9c71a9e..6581339 100644 Binary files a/Server01/util/__pycache__/verifyJWT.cpython-39.pyc and b/Server01/util/__pycache__/verifyJWT.cpython-39.pyc differ diff --git a/Server01/util/auxiliaryFuction.py b/Server01/util/auxiliaryFuction.py index 7077847..0c65a68 100644 --- a/Server01/util/auxiliaryFuction.py +++ b/Server01/util/auxiliaryFuction.py @@ -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 [] diff --git a/Server01/util/verifyJWT.py b/Server01/util/verifyJWT.py index 931f895..7741532 100644 --- a/Server01/util/verifyJWT.py +++ b/Server01/util/verifyJWT.py @@ -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 diff --git a/Server01/views/__pycache__/post.cpython-39.pyc b/Server01/views/__pycache__/post.cpython-39.pyc index 33ea812..e9eeb19 100644 Binary files a/Server01/views/__pycache__/post.cpython-39.pyc and b/Server01/views/__pycache__/post.cpython-39.pyc differ diff --git a/Server01/views/__pycache__/user.cpython-39.pyc b/Server01/views/__pycache__/user.cpython-39.pyc index 61208dd..cdfc069 100644 Binary files a/Server01/views/__pycache__/user.cpython-39.pyc and b/Server01/views/__pycache__/user.cpython-39.pyc differ diff --git a/Server01/views/post.py b/Server01/views/post.py index 49a5117..bc0cb18 100644 --- a/Server01/views/post.py +++ b/Server01/views/post.py @@ -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 @@ -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) @@ -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) | @@ -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) diff --git a/Server01/views/user.py b/Server01/views/user.py index 06570d4..63a773a 100644 --- a/Server01/views/user.py +++ b/Server01/views/user.py @@ -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 @@ -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 = { @@ -57,15 +57,33 @@ 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): @@ -73,7 +91,13 @@ def get_user_focus(request, payload): 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) # 用户关注 diff --git a/webServer/__pycache__/settings.cpython-39.pyc b/webServer/__pycache__/settings.cpython-39.pyc index cb72c7c..bdb637f 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 2b67977..f3a0725 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 9c413fa..2803f9c 100644 --- a/webServer/urls.py +++ b/webServer/urls.py @@ -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) ]