From fc35e5ed79c99742c1d7d5c408ff7261f1358478 Mon Sep 17 00:00:00 2001 From: zema1 Date: Wed, 6 Dec 2017 14:48:46 +0800 Subject: [PATCH] filter admin users in all rankings --- account/tests.py | 16 ++++++++++++++++ account/views/oj.py | 6 +++--- contest/views/oj.py | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/account/tests.py b/account/tests.py index 97880aac..7ca7e810 100644 --- a/account/tests.py +++ b/account/tests.py @@ -474,6 +474,22 @@ class UserRankAPITest(APITestCase): self.assertEqual(data[0]["user"]["username"], "test2") self.assertEqual(data[1]["user"]["username"], "test1") + def test_admin_role_filted(self): + self.create_admin("admin", "admin123") + admin = User.objects.get(username="admin") + profile1 = admin.userprofile + profile1.submission_number = 20 + profile1.accepted_number = 5 + profile1.total_score = 300 + profile1.save() + resp = self.client.get(self.url, data={"rule": ContestRuleType.ACM}) + self.assertSuccess(resp) + self.assertEqual(len(resp.data["data"]), 2) + + resp = self.client.get(self.url, data={"rule": ContestRuleType.OI}) + self.assertSuccess(resp) + self.assertEqual(len(resp.data["data"]), 2) + class ProfileProblemDisplayIDRefreshAPITest(APITestCase): def setUp(self): diff --git a/account/views/oj.py b/account/views/oj.py index 0128fa99..321e93f3 100644 --- a/account/views/oj.py +++ b/account/views/oj.py @@ -18,7 +18,7 @@ from utils.api import APIView, validate_serializer from utils.captcha import Captcha from utils.shortcuts import rand_str, img2base64, datetime2str from ..decorators import login_required -from ..models import User, UserProfile +from ..models import User, UserProfile, AdminType from ..serializers import (ApplyResetPasswordSerializer, ResetPasswordSerializer, UserChangePasswordSerializer, UserLoginSerializer, UserRegisterSerializer, UsernameOrEmailCheckSerializer, @@ -375,8 +375,8 @@ class UserRankAPI(APIView): rule_type = request.GET.get("rule") if rule_type not in ContestRuleType.choices(): rule_type = ContestRuleType.ACM - profiles = UserProfile.objects.select_related("user")\ - .exclude(user__is_disabled=True) + profiles = UserProfile.objects.filter(user__admin_type=AdminType.REGULAR_USER, user__is_disabled=False) \ + .select_related("user") if rule_type == ContestRuleType.ACM: profiles = profiles.filter(submission_number__gt=0).order_by("-accepted_number", "submission_number") else: diff --git a/contest/views/oj.py b/contest/views/oj.py index 4f78f826..38afe44a 100644 --- a/contest/views/oj.py +++ b/contest/views/oj.py @@ -3,6 +3,7 @@ from django.core.cache import cache from utils.api import APIView, validate_serializer from utils.constants import CacheKey from utils.shortcuts import datetime2str +from account.models import AdminType from account.decorators import login_required, check_contest_permission from utils.constants import ContestRuleType, ContestStatus @@ -93,10 +94,10 @@ class ContestAccessAPI(APIView): class ContestRankAPI(APIView): def get_rank(self): if self.contest.rule_type == ContestRuleType.ACM: - return ACMContestRank.objects.filter(contest=self.contest). \ + return ACMContestRank.objects.filter(contest=self.contest, user__admin_type=AdminType.REGULAR_USER). \ select_related("user").order_by("-accepted_number", "total_time") else: - return OIContestRank.objects.filter(contest=self.contest). \ + return OIContestRank.objects.filter(contest=self.contest, user__admin_type=AdminType.REGULAR_USER). \ select_related("user").order_by("-total_score") @check_contest_permission(check_type="ranks")