[Opt] Optimize query performance

This commit is contained in:
Harry-zklcdc 2020-08-07 13:12:21 +08:00
parent 7864fd4b59
commit a0fee499dd
4 changed files with 12 additions and 12 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)