From 13e3260fd3399e7751ba4cb4e75f23089a29c965 Mon Sep 17 00:00:00 2001 From: zema1 Date: Wed, 29 Nov 2017 14:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0UserSerailizer=20admin?= =?UTF-8?q?=E8=BF=94=E5=9B=9Ereal=5Fname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contest/serializers.py | 18 ++++++++++++++++-- contest/views/oj.py | 9 ++++++--- problem/tests.py | 3 --- problem/views/oj.py | 5 +++-- utils/api/_serializers.py | 8 ++++++++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/contest/serializers.py b/contest/serializers.py index abbdccca..f66cced4 100644 --- a/contest/serializers.py +++ b/contest/serializers.py @@ -75,16 +75,30 @@ class ContestPasswordVerifySerializer(serializers.Serializer): class ACMContestRankSerializer(serializers.ModelSerializer): - user = UsernameSerializer() + user = serializers.SerializerMethodField() submission_info = serializers.JSONField() class Meta: model = ACMContestRank + def __init__(self, *args, **kwargs): + self.is_admin_role = kwargs.pop("is_admin_role", False) + super().__init__(*args, **kwargs) + + def get_user(self, obj): + return UsernameSerializer(obj.user, is_admin_role=self.is_admin_role).data + class OIContestRankSerializer(serializers.ModelSerializer): - user = UsernameSerializer() + user = serializers.SerializerMethodField() submission_info = serializers.JSONField() class Meta: model = OIContestRank + + def __init__(self, *args, **kwargs): + self.is_admin_role = kwargs.pop("is_admin_role", False) + super().__init__(*args, **kwargs) + + def get_user(self, obj): + return UsernameSerializer(obj.user, is_admin_role=self.is_admin_role).data diff --git a/contest/views/oj.py b/contest/views/oj.py index 66889d5c..560f5679 100644 --- a/contest/views/oj.py +++ b/contest/views/oj.py @@ -57,8 +57,7 @@ class ContestListAPI(APIView): contests = contests.filter(end_time__lt=cur) else: contests = contests.filter(start_time__lte=cur, end_time__gte=cur) - data = self.paginate_data(request, contests, ContestSerializer) - return self.success(data) + return self.success(self.paginate_data(request, contests, ContestSerializer)) class ContestPasswordVerifyAPI(APIView): @@ -102,6 +101,7 @@ class ContestRankAPI(APIView): @check_contest_permission(check_type="ranks") def get(self, request): + user = request.user if self.contest.rule_type == ContestRuleType.OI: serializer = OIContestRankSerializer else: @@ -112,4 +112,7 @@ class ContestRankAPI(APIView): if not qs: qs = self.get_rank() cache.set(cache_key, qs) - return self.success(self.paginate_data(request, qs, serializer)) + page_qs = self.paginate_data(request, qs) + page_qs["results"] = serializer(page_qs["results"], many=True, + is_admin_role=user.is_authenticated() and user.is_admin_role()).data + return self.success(page_qs) diff --git a/problem/tests.py b/problem/tests.py index 93cd0585..46d9ac69 100644 --- a/problem/tests.py +++ b/problem/tests.py @@ -71,9 +71,6 @@ class ProblemTagListAPITest(APITestCase): ProblemTag.objects.create(name="name2") resp = self.client.get(self.reverse("problem_tag_list_api")) self.assertSuccess(resp) - resp_data = resp.data["data"] - self.assertEqual(resp_data[0]["name"], "name1") - self.assertEqual(resp_data[1]["name"], "name2") class TestCaseUploadAPITest(APITestCase): diff --git a/problem/views/oj.py b/problem/views/oj.py index 5d1efb40..57df2dd7 100644 --- a/problem/views/oj.py +++ b/problem/views/oj.py @@ -1,5 +1,5 @@ import random -from django.db.models import Q +from django.db.models import Q, Count from utils.api import APIView from account.decorators import check_contest_permission from ..models import ProblemTag, Problem, ProblemRuleType @@ -10,7 +10,8 @@ from contest.models import ContestRuleType class ProblemTagAPI(APIView): def get(self, request): - return self.success(TagSerializer(ProblemTag.objects.all(), many=True).data) + tags = ProblemTag.objects.annotate(problem_count=Count("problem")).filter(problem_count__gt=0) + return self.success(TagSerializer(tags, many=True).data) class PickOneAPI(APIView): diff --git a/utils/api/_serializers.py b/utils/api/_serializers.py index 737a9656..a975ec02 100644 --- a/utils/api/_serializers.py +++ b/utils/api/_serializers.py @@ -14,3 +14,11 @@ class IDOnlySerializer(serializers.Serializer): class UsernameSerializer(serializers.Serializer): id = serializers.IntegerField() username = serializers.CharField() + real_name = serializers.SerializerMethodField() + + def __init__(self, *args, **kwargs): + self.is_admin_role = kwargs.pop("is_admin_role", False) + super().__init__(*args, **kwargs) + + def get_real_name(self, obj): + return obj.userprofile.real_name if self.is_admin_role else None