Skip to content

Commit

Permalink
更新用户更新个人信息功能
Browse files Browse the repository at this point in the history
  • Loading branch information
xishandong committed Jul 11, 2023
1 parent 24e4cc3 commit f1ecd35
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ pip insatll mysqlclient

用户评论

用户更新个人信息

未完待续
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/post/30-avatar.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/33-张娜英.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/33-桥本环奈.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__/verifyJWT.cpython-39.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions Server01/util/verifyJWT.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def wrapper(request, *args, **kwargs):
token = request.headers.get('Authorization').split(' ')[1]
verify_payload = jwt.decode(token, SECRET_KEY, ['HS256'], verify=True)
# 检查数据库是否存在该用户
if models.User.objects.filter(username=verify_payload.get('username'),
id=verify_payload.get('user_id')).exists():
if models.User.objects.filter(id=verify_payload.get('user_id')).exists():
return view_func(request, verify_payload, *args, **kwargs)
return JsonResponse({'error': '未授权访问'}, status=401)
except exceptions.ExpiredSignatureError:
Expand Down Expand Up @@ -45,7 +44,7 @@ def create_token(user):
payload = {
'user_id': user.id,
'username': user.username,
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=30) # 设置过期时间
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60) # 设置过期时间
}
result = jwt.encode(payload=payload, key=SECRET_KEY, algorithm='HS256', headers=headers)
return result
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.
11 changes: 5 additions & 6 deletions Server01/views/post.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import json

from django.http import JsonResponse
from webServer.settings import TIME_ZONE, SYSTEM_PATH

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




def upload_post(request):
@authenticate_request
def upload_post(request, payload):
file = request.FILES['file']
id = request.POST.get('id')
file_path = SYSTEM_PATH + '/webServer/Server01/static/img/post/' + str(id) + '-' + file.name
Expand Down Expand Up @@ -89,4 +89,3 @@ def query_post_index(request):
return JsonResponse({'info': list(combine_index_post(posts))}, status=200)

return JsonResponse({'info': []}, status=200)

33 changes: 32 additions & 1 deletion Server01/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from django.http import JsonResponse

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


# 用户登录
Expand Down Expand Up @@ -102,3 +103,33 @@ def unfollow(request, payload):
user.following.remove(unfollow_user)
return JsonResponse({'info': '成功取消关注'}, status=200)
return JsonResponse({'error': '非法的操作'}, status=401)


@authenticate_request
def update_user_info(request, payload):
data = json.loads(request.body)
user_id = payload['user_id']
user = models.User.objects.filter(id=user_id).first()
user.username = data['username']
user.signature = data['signature']
user.save()
return JsonResponse({'info': '修改成功'}, status=200)


@authenticate_request
def update_avatar(request, payload):
file = request.FILES['file']
id = payload['user_id']
file_path = SYSTEM_PATH + '/webServer/Server01/static/img/avatar/' + str(id) + '-' + file.name
check_and_delete(id, SYSTEM_PATH + '/webServer/Server01/static/img/avatar/')
with open(file_path, 'wb') as destination:
for chunk in file.chunks():
destination.write(chunk)
result = {
'filename': file.name,
'filepath': 'http://localhost:8000/static/img/avatar/' + str(id) + '-' + file.name,
}
user = models.User.objects.filter(id=id).first()
user.avatar = 'http://localhost:8000/static/img/avatar/' + str(id) + '-' + file.name
user.save()
return JsonResponse({'info': result}, status=200)
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 @@ -26,6 +26,8 @@
path('focus/', user.focusOn),
path('user/focus/', user.get_user_focus),
path('user/unfollow/', user.unfollow),
path('user/update/', user.update_user_info),
path('user/avatar/', user.update_avatar),
# 帖子相关
path('upload/', post.upload_post),
path('upload/info/', post.upload_post_info),
Expand Down

0 comments on commit f1ecd35

Please sign in to comment.