From a0fee499ddc3f0a12282bd5522cb2d69a25e99b7 Mon Sep 17 00:00:00 2001 From: Harry-zklcdc <1269158832@qq.com> Date: Fri, 7 Aug 2020 13:12:21 +0800 Subject: [PATCH] [Opt] Optimize query performance --- account/views/oj.py | 4 ++-- contest/views/oj.py | 4 ++-- forum/views/oj.py | 8 ++++---- submission/views/oj.py | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/account/views/oj.py b/account/views/oj.py index 1ca94caf..977e2270 100644 --- a/account/views/oj.py +++ b/account/views/oj.py @@ -392,11 +392,11 @@ class UserRankAPI(APIView): data = self.paginate_data(request, profiles, RankInfoSerializer) for i in range(0, len(data["results"])): try: - user = User.objects.get(username=data["results"][i]["user"]["username"], is_disabled=False) + user = User.objects.get(id=data["results"][i]["user"]["id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=False).data data["results"][i].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + data["results"][i].update({"grade": 0, "title": None, "title_color": None}) return self.success(data) diff --git a/contest/views/oj.py b/contest/views/oj.py index 50e673f1..e8acb6bc 100644 --- a/contest/views/oj.py +++ b/contest/views/oj.py @@ -192,9 +192,9 @@ class ContestRankAPI(APIView): page_qs["results"] = serializer(page_qs["results"], many=True, is_contest_admin=is_contest_admin).data for i in range(0, len(page_qs["results"])): try: - user = User.objects.get(username=page_qs["results"][i]["user"]["username"], is_disabled=False) + user = User.objects.get(id=page_qs["results"][i]["user"]["id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=True).data page_qs["results"][i].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + page_qs["results"][i].update({"grade": 0, "title": None, "title_color": None}) return self.success(page_qs) diff --git a/forum/views/oj.py b/forum/views/oj.py index a768245d..dee3d4c1 100644 --- a/forum/views/oj.py +++ b/forum/views/oj.py @@ -182,11 +182,11 @@ class ForumReplyAPI(APIView): data = ForumReplySerializer(forumreply).data try: - user = User.objects.get(username=data["author"]["username"], is_disabled=False) + user = User.objects.get(id=data["author"]["id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=False).data data["author"].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + data["author"].update({"grade": 0, "title": None, "title_color": None}) forumpost = ForumPostSerializer(ForumPost.objects.select_related("author").get(id=data["fa_id"])).data author = User.objects.get(username=forumpost["author"]["username"], is_disabled=False) @@ -221,11 +221,11 @@ class ForumReplyAPI(APIView): data = self.paginate_data(request, forumreplys, ForumReplySerializer) for i in range(0, len(data["results"])): try: - user = User.objects.get(username=data["results"][i]["author"]["username"], is_disabled=False) + user = User.objects.get(id=data["results"][i]["author"]["id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=False).data data["results"][i]["author"].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + data["results"][i]["author"].update({"grade": 0, "title": None, "title_color": None}) return self.success(data) @login_required diff --git a/submission/views/oj.py b/submission/views/oj.py index 4a361ed1..2674cf91 100644 --- a/submission/views/oj.py +++ b/submission/views/oj.py @@ -156,11 +156,11 @@ class SubmissionListAPI(APIView): data["results"] = SubmissionListSerializer(data["results"], many=True, user=request.user).data for i in range(0, len(data["results"])): try: - user = User.objects.get(username=data["results"][i]["username"], is_disabled=False) + user = User.objects.get(id=data["results"][i]["user_id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=False).data data["results"][i].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + data["results"][i].update({"grade": 0, "title": None, "title_color": None}) return self.success(data) @@ -203,11 +203,11 @@ class ContestSubmissionListAPI(APIView): data["results"] = SubmissionListSerializer(data["results"], many=True, user=request.user).data for i in range(0, len(data["results"])): try: - user = User.objects.get(username=data["results"][i]["username"], is_disabled=False) + user = User.objects.get(id=data["results"][i]["user_id"], is_disabled=False) userprofile = UserProfileSerializer(user.userprofile, show_real_name=True).data data["results"][i].update({"grade": userprofile["grade"], "title": userprofile["user"]["title"], "title_color": userprofile["user"]["title_color"]}) except Exception: - pass + data["results"][i].update({"grade": 0, "title": None, "title_color": None}) return self.success(data)