添加UserSerailizer admin返回real_name

This commit is contained in:
zema1 2017-11-29 14:06:32 +08:00
parent 7ce13911a7
commit 13e3260fd3
5 changed files with 33 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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