From f59a39ee6d67bad00c4c6961ef56226470130bf7 Mon Sep 17 00:00:00 2001 From: lw2333 <2374046775@qq.com> Date: Fri, 10 Nov 2023 14:58:24 +0800 Subject: [PATCH] refactor: add unauthorized user exception --- hinghwa-dict-backend/article/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/hinghwa-dict-backend/article/views.py b/hinghwa-dict-backend/article/views.py index db037e24..b2df0065 100644 --- a/hinghwa-dict-backend/article/views.py +++ b/hinghwa-dict-backend/article/views.py @@ -304,23 +304,27 @@ def put(self, request) -> JsonResponse: class CommentDetail(View): # AT0405 获取评论详情 - def get(self, request, id) -> JsonResponse: # 注意没有使用request,位置也是需要保留着的 + def get(self, request, id) -> JsonResponse: # 初始化 me 的信息 me = {"is_liked": False, "is_author": False} + # 登录获取 user 信息,如果用户未登录则直接返回 401,不允许其查看评论 try: - comment = Comment.objects.get(id=id) - - # 登录获取 user 信息 token = token_pass(request.headers) user = token_user(token) + except UnauthorizedException: + return JsonResponse({"comment": comment_all(comment), "me": me}, status=401) + + # 获取评论信息 + try: + comment = Comment.objects.get(id=id) is_liked = comment.like_users.filter(id=user.id).exists() is_author = user == comment.user if user else False me = {"is_liked": is_liked, "is_author": is_author} - except: + except Comment.DoesNotExist: raise CommentNotFoundException(id) return JsonResponse({"comment": comment_all(comment), "me": me}, status=200)