Skip to content

Commit

Permalink
refactor: pass exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Topology2333 committed Nov 13, 2023
1 parent 48fbebf commit d95b4ba
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions hinghwa-dict-backend/article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,14 @@ def get(self, request, id) -> JsonResponse:
# 初始化 me 的信息
me = {"is_liked": False, "is_author": False}

# 登录获取 user 信息,如果用户未登录则直接返回 401,不允许其查看评论
try:
token = token_pass(request.headers)
user = token_user(token)

# 获取评论信息
token = token_pass(request.headers)
user = token_user(token)

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
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 UnauthorizedException:
pass
except Comment.DoesNotExist:
raise CommentNotFoundException(id)
me = {"is_liked": is_liked, "is_author": is_author}

return JsonResponse({"comment": comment_all(comment), "me": me}, status=200)

Expand Down

0 comments on commit d95b4ba

Please sign in to comment.