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 904c806 commit d0c91f7
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 17 deletions.
Binary file added Server01/static/img/post/64-img1.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/65-img4.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/65-project.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 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.
31 changes: 31 additions & 0 deletions Server01/util/auxiliaryFuction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytz

import Server01.models as models
from webServer.settings import TIME_ZONE


# 更换时区
Expand Down Expand Up @@ -54,3 +55,33 @@ def filter_querySet(querySet, offset, limit=20):
filterQuerySet = querySet.order_by('-id')[start:end]
return filterQuerySet
return []


def get_user_post_info(posts, offset):
clear_posts = filter_querySet(posts, offset, 10)
info = [{
'date': convert_to_timezone(post.created_at, TIME_ZONE),
'title': post.title,
'likeCount': post.favoritePosts.count(),
'collectCount': post.collectedPosts.count(),
'commentCount': post.comments.count(),
'content': post.content,
'id': post.id,
'username': post.user.username,
} for post in clear_posts if post]
return info


def get_user_info(users, offset):
clear_users = filter_querySet(users, offset, 10)
info = [
{
'username': user.username,
'avatar': user.avatar,
'id': user.id,
'fans': user.beFocusOn.count(),
'follow': user.following.count(),
'note': user.posts.count()
} for user in clear_users
]
return info
1 change: 1 addition & 0 deletions Server01/util/verifyJWT.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def wrapper(request, *args, **kwargs):
error_message = {'error': '非法的token'}
return JsonResponse(error_message, status=401)
except AttributeError as e:
print(e)
error_message = {'error': '未查询到登录信息,请重新登录'}
return JsonResponse(error_message, status=403)

Expand Down
Binary file modified Server01/views/__pycache__/user.cpython-39.pyc
Binary file not shown.
59 changes: 43 additions & 16 deletions Server01/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import Server01.models as models
from Server01.util.auxiliaryFuction import check_email, combine_index_post, check_and_delete, filter_querySet, \
convert_to_timezone
convert_to_timezone, get_user_post_info, get_user_info
from Server01.util.verifyJWT import create_token, authenticate_request
from webServer.settings import SYSTEM_PATH, TIME_ZONE

Expand Down Expand Up @@ -130,6 +130,20 @@ def unfollow(request, payload):
return JsonResponse({'error': '非法的操作'}, status=401)


@authenticate_request
def remove_fans(request, payload):
# 移除操作的用户id
user_id = payload['user_id']
user = models.User.objects.filter(id=user_id).first()
# 移除的粉丝id
fans_id = json.loads(request.body)['id']
fan = models.User.objects.filter(id=fans_id).first()
if user and fan:
fan.following.remove(user)
return JsonResponse({'info': '成功移除粉丝'}, status=200)
return JsonResponse({'error': '非法的操作'}, status=401)


@authenticate_request
def update_user_info(request, payload):
data = json.loads(request.body)
Expand Down Expand Up @@ -161,21 +175,34 @@ def update_avatar(request, payload):


@authenticate_request
def user_post_control_index(request, payload):
def user_control_index(request, payload):
user_id = payload['user_id']
offset = json.loads(request.body)['offset']
data = json.loads(request.body)
offset = data['offset']
types = data['types']
user = models.User.objects.filter(id=user_id).first()
if user:
user_post = user.posts.all()
clear_user_post = filter_querySet(user_post, offset, 10)
info = [{
'date': convert_to_timezone(post.created_at, TIME_ZONE),
'title': post.title,
'likeCount': post.favoritePosts.all().count(),
'collectCount': post.collectedPosts.all().count(),
'commentCount': post.comments.all().count(),
'content': post.content,
'id': post.id
} for post in clear_user_post if post]
return JsonResponse({'info': info, 'total': user_post.count()}, status=200)
return JsonResponse({'error': '错误的操作'}, status=404)
if types == 'posts':
user_data = user.posts.all()
info = get_user_post_info(user_data, offset)
elif types == 'collected':
user_data = user.collected.all()
info = get_user_post_info(user_data, offset)
elif types == 'favorites':
user_data = user.favorites.all()
info = get_user_post_info(user_data, offset)
elif types == 'fans':
user_data = user.beFocusOn.all()
info = get_user_info(user_data, offset)
elif types == 'follow':
user_data = user.following.all()
info = get_user_info(user_data, offset)
else:
return JsonResponse({'error': '错误的操作'}, status=404)
total = user_data.count()
return JsonResponse({'info': info, 'total': total}, 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.
4 changes: 3 additions & 1 deletion webServer/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""
from django.contrib import admin
from django.urls import path

from Server01.views import user, post, comment

urlpatterns = [
Expand All @@ -29,7 +30,8 @@
path('user/update/', user.update_user_info),
path('user/avatar/', user.update_avatar),
path('user/post/', user.query_user_index_post),
path('user/post/control/', user.user_post_control_index),
path('user/post/control/', user.user_control_index),
path('user/remove/fan/', user.remove_fans),
# 帖子相关
path('upload/', post.upload_post),
path('upload/info/', post.upload_post_info),
Expand Down

0 comments on commit d0c91f7

Please sign in to comment.