diff --git a/Server01/__pycache__/models.cpython-39.pyc b/Server01/__pycache__/models.cpython-39.pyc index ba86457..76e354a 100644 Binary files a/Server01/__pycache__/models.cpython-39.pyc and b/Server01/__pycache__/models.cpython-39.pyc differ diff --git a/Server01/migrations/0011_remove_user_followed.py b/Server01/migrations/0011_remove_user_followed.py new file mode 100644 index 0000000..7a72e33 --- /dev/null +++ b/Server01/migrations/0011_remove_user_followed.py @@ -0,0 +1,17 @@ +# Generated by Django 4.1 on 2023-07-11 08:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("Server01", "0010_remove_post_images_alter_image_post"), + ] + + operations = [ + migrations.RemoveField( + model_name="user", + name="followed", + ), + ] diff --git a/Server01/migrations/__pycache__/0011_remove_user_followed.cpython-39.pyc b/Server01/migrations/__pycache__/0011_remove_user_followed.cpython-39.pyc new file mode 100644 index 0000000..b774b69 Binary files /dev/null and b/Server01/migrations/__pycache__/0011_remove_user_followed.cpython-39.pyc differ diff --git a/Server01/models.py b/Server01/models.py index 89295fe..e2e954f 100644 --- a/Server01/models.py +++ b/Server01/models.py @@ -1,4 +1,6 @@ from django.db import models +from webServer.settings import SYSTEM_PATH +from Server01.util.auxiliaryFuction import check_and_delete class User(models.Model): @@ -9,14 +11,17 @@ class User(models.Model): avatar = models.CharField(max_length=256, verbose_name='头像', null=False, default='http://localhost:8000/static/img/avatar/defaultAvatar.jpg') signature = models.CharField(max_length=64, verbose_name='个性签名', default='暂时没有个性签名~', null=True) + # 用户关注,related_name获取用户的粉丝 following = models.ManyToManyField('self', symmetrical=False, blank=True, related_name='beFocusOn') - followed = models.ManyToManyField('self', symmetrical=False, blank=True, related_name='focusOn') + # 用户喜爱的帖子 favorites = models.ManyToManyField('Post', blank=True, related_name='favoritePosts') + # 用户收藏的帖子 collected = models.ManyToManyField('Post', blank=True, related_name='collectedPosts') class Post(models.Model): """ 帖子表 """ + # 用户通过related_name获取ta发的帖子 user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts') title = models.CharField(max_length=64, verbose_name='标题', null=False) content = models.TextField(max_length=3000, verbose_name='内容', null=True) @@ -25,7 +30,9 @@ class Post(models.Model): def delete(self, *args, **kwargs): # 删除关联的帖子图片 self.imgs.all().delete() - + # 删除帖子的图片的存储 + path = SYSTEM_PATH + '/webServer/Server01/static/img/post/' + check_and_delete(path, self.id) # 删除帖子本身 super().delete(*args, **kwargs) diff --git a/Server01/static/img/post/29-avatar.jpg b/Server01/static/img/post/29-avatar.jpg new file mode 100644 index 0000000..eb44714 Binary files /dev/null and b/Server01/static/img/post/29-avatar.jpg differ diff --git a/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc b/Server01/util/__pycache__/auxiliaryFuction.cpython-39.pyc index ffbd4b3..0584259 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/auxiliaryFuction.py b/Server01/util/auxiliaryFuction.py index 962c088..7077847 100644 --- a/Server01/util/auxiliaryFuction.py +++ b/Server01/util/auxiliaryFuction.py @@ -1,7 +1,45 @@ +import os + import pytz +import Server01.models as models + +# 更换时区 def convert_to_timezone(datetime_obj, timezone_str): target_timezone = pytz.timezone(timezone_str) converted_datetime = datetime_obj.astimezone(target_timezone) return converted_datetime.strftime('%Y-%m-%d %H:%M') + + +# 检查邮箱 +def check_email(email): + return models.User.objects.filter(email=email).exists() + + +# 整合主页帖子的信息 +def combine_index_post(posts): + for post in posts: + imgs = post.imgs.all() + info = { + 'title': post.title, + 'id': post.id, + 'img': imgs[0].imagePath, + 'user': { + 'id': post.user.id, + 'username': post.user.username, + 'avatar': post.user.avatar + } + } + yield info + + +# 检查和删除图片,用于删除帖子时删除文件,以及删除用户上一次上传的头像 +def check_and_delete(id, mainPath): + # 获取目录下的文件 + file_list = os.listdir(mainPath) + # 遍历文件列表,检查是否有对应的文件,如果有就删除 + for file_name in file_list: + if file_name.startswith(f'{id}-'): + file_path = os.path.join(mainPath, file_name) + os.remove(file_path) diff --git a/Server01/views/__pycache__/post.cpython-39.pyc b/Server01/views/__pycache__/post.cpython-39.pyc index 175692b..0ea910e 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 10ea309..7ecd431 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 4dcd9f9..62c85c7 100644 --- a/Server01/views/post.py +++ b/Server01/views/post.py @@ -1,18 +1,18 @@ import json from django.http import JsonResponse -from webServer.settings import TIME_ZONE +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 +from Server01.util.auxiliaryFuction import convert_to_timezone, combine_index_post + -system = 'D:/vue' def upload_post(request): file = request.FILES['file'] id = request.POST.get('id') - file_path = system + '/webServer/Server01/static/img/post/' + str(id) + '-' + file.name + file_path = SYSTEM_PATH + '/webServer/Server01/static/img/post/' + str(id) + '-' + file.name with open(file_path, 'wb') as destination: for chunk in file.chunks(): destination.write(chunk) @@ -27,6 +27,7 @@ def upload_post(request): return JsonResponse({'error': '错误的操作'}, status=401) +# 用户上传帖子 @authenticate_request def upload_post_info(request, payload): data = json.loads(request.body) @@ -38,6 +39,7 @@ def upload_post_info(request, payload): return JsonResponse({'data': 'success', 'info': post.id}, status=200) +# 获取帖子详情,整合信息 def get_post_detail(request): data = json.loads(request.body) id = data.get('id') @@ -72,6 +74,7 @@ def get_post_detail(request): return JsonResponse({'error': '错误的访问'}, status=401) +# 主页推送帖子 def query_post_index(request): data = json.loads(request.body) offset = data['offset'] @@ -87,18 +90,3 @@ def query_post_index(request): return JsonResponse({'info': []}, status=200) - -def combine_index_post(posts): - for post in posts: - imgs = post.imgs.all() - info = { - 'title': post.title, - 'id': post.id, - 'img': imgs[0].imagePath, - 'user': { - 'id': post.user.id, - 'username': post.user.username, - 'avatar': post.user.avatar - } - } - yield info diff --git a/Server01/views/user.py b/Server01/views/user.py index c53f640..257e929 100644 --- a/Server01/views/user.py +++ b/Server01/views/user.py @@ -3,7 +3,8 @@ from django.http import JsonResponse import Server01.models as models -from Server01.util.verifyJWT import create_token +from Server01.util.auxiliaryFuction import check_email, combine_index_post +from Server01.util.verifyJWT import create_token, authenticate_request # 用户登录 @@ -38,6 +39,7 @@ def register(request): return JsonResponse({'error': '创建用户失败'}, status=401) +# 获取用户主页信息 def query_user_index(request): data = json.loads(request.body) if data.get('id'): @@ -49,7 +51,7 @@ def query_user_index(request): 'avatar': user.avatar, 'signature': user.signature, 'fans': user.beFocusOn.count(), - 'focusOn': user.focusOn.count(), + 'focusOn': user.following.count(), 'postsCount': user.posts.count(), } info = { @@ -63,21 +65,40 @@ def query_user_index(request): return JsonResponse({'error': '非法访问'}, status=401) -def combine_index_post(posts): - for post in posts: - imgs = post.imgs.all() - info = { - 'title': post.title, - 'id': post.id, - 'img': imgs[0].imagePath, - 'user': { - 'id': post.user.id, - 'username': post.user.username, - 'avatar': post.user.avatar - } - } - yield info +# 获取用户关注用户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) + + +# 用户关注 +@authenticate_request +def focusOn(request, payload): + # 做关注操作的用户id + id1 = payload['user_id'] + user1 = models.User.objects.filter(id=id1).first() + # 被关注的用户id + id2 = json.loads(request.body)['id'] + user2 = models.User.objects.filter(id=id2).first() + if user1 and user2: + user1.following.add(user2) + return JsonResponse({'info': '成功关注'}, status=200) + return JsonResponse({'error': '非法的操作'}, status=401) -def check_email(email): - return models.User.objects.filter(email=email).exists() +@authenticate_request +def unfollow(request, payload): + # 取消关注操作的用户id + user_id = payload['user_id'] + user = models.User.objects.filter(id=user_id).first() + # 被取消关注的用户id + unfollow_id = json.loads(request.body)['id'] + unfollow_user = models.User.objects.filter(id=unfollow_id).first() + if user and unfollow_user: + user.following.remove(unfollow_user) + return JsonResponse({'info': '成功取消关注'}, status=200) + return JsonResponse({'error': '非法的操作'}, status=401) diff --git a/webServer/__pycache__/settings.cpython-39.pyc b/webServer/__pycache__/settings.cpython-39.pyc index a20d88d..2e5ec24 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 d24306f..3025d33 100644 Binary files a/webServer/__pycache__/urls.cpython-39.pyc and b/webServer/__pycache__/urls.cpython-39.pyc differ diff --git a/webServer/settings.py b/webServer/settings.py index 0ccd204..8ba416f 100644 --- a/webServer/settings.py +++ b/webServer/settings.py @@ -141,7 +141,8 @@ # 启用 CSRF 保护 CSRF_COOKIE_HTTPONLY = True - +# 配置保存文件路径 +SYSTEM_PATH = 'D:/vue' diff --git a/webServer/urls.py b/webServer/urls.py index fa736d5..8819ce1 100644 --- a/webServer/urls.py +++ b/webServer/urls.py @@ -19,12 +19,18 @@ urlpatterns = [ path("admin/", admin.site.urls), + # 用户相关 path("login/", user.login), path('register/', user.register), path('index/', user.query_user_index), + path('focus/', user.focusOn), + path('user/focus/', user.get_user_focus), + path('user/unfollow/', user.unfollow), + # 帖子相关 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('comment/', comment.do_comment) ]