From 99bb70212606bd9b3408bf21c4fb52cb135a7527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97?= <2053217188@qq.com> Date: Thu, 9 May 2024 13:57:55 +0800 Subject: [PATCH] refactor(article): highlight "me" field --- hinghwa-dict-backend/article/views.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/hinghwa-dict-backend/article/views.py b/hinghwa-dict-backend/article/views.py index 1dc72b9c..25c0eb3e 100644 --- a/hinghwa-dict-backend/article/views.py +++ b/hinghwa-dict-backend/article/views.py @@ -131,9 +131,9 @@ def get(self, request, id) -> JsonResponse: me = {"liked": False, "is_author": False} return JsonResponse({"article": article, "me": me}, status=200) if ( - not article.visibility - and not user.is_superuser - and not user == article.author + not article.visibility + and not user.is_superuser + and not user == article.author ): raise ArticleNotFoundException() article.views += 1 @@ -254,11 +254,19 @@ def get(self, request, id) -> JsonResponse: article = Article.objects.filter(id=id) if not article.exists() or not ( - article[0].visibility or user.is_superuser or user == article[0].author + article[0].visibility or user.is_superuser or user == article[0].author ): raise ArticleNotFoundException() article = article[0] - comments = [comment_normal(comment) for comment in article.comments.all()] + comments = [] + for comment in article.comments.all(): + me = {"like": comment.like_users.filter(id=user.id).exists()} + comments.append( + { + "comment": comment_all(comment), + "me": me, + } + ) return JsonResponse({"comments": comments}, status=200) # AT0401 发表文章评论 @@ -267,7 +275,7 @@ def post(self, request, id) -> JsonResponse: user = token_user(token) article = Article.objects.filter(id=id) if not article.exists() or not ( - article[0].visibility or user.is_superuser or user == article[0].author + article[0].visibility or user.is_superuser or user == article[0].author ): raise ArticleNotFoundException() article = article[0] @@ -289,13 +297,13 @@ def delete(self, request, id) -> JsonResponse: user = token_user(token) article = Article.objects.filter(id=id) if not article.exists() or not ( - article[0].visibility or user.is_superuser or user == article[0].author + article[0].visibility or user.is_superuser or user == article[0].author ): raise ArticleNotFoundException() body = demjson3.decode(request.body) comment = Comment.objects.get(id=body["id"]) if token_pass(request.headers, comment.user.id) or token_pass( - request.headers, -1 + request.headers, -1 ): # 应原注释要求,超级管理员也能删 comment.delete()