移除user time_zone, 统一使用submission_number和accepted_number字段
This commit is contained in:
30
contest/migrations/0005_auto_20170823_0918.py
Normal file
30
contest/migrations/0005_auto_20170823_0918.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.6 on 2017-08-23 09:18
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('contest', '0004_auto_20170717_1324'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='acmcontestrank',
|
||||
old_name='total_ac_number',
|
||||
new_name='accepted_number',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='acmcontestrank',
|
||||
old_name='total_submission_number',
|
||||
new_name='submission_number',
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='oicontestrank',
|
||||
old_name='total_submission_number',
|
||||
new_name='submission_number',
|
||||
),
|
||||
]
|
||||
@@ -64,14 +64,14 @@ class Contest(models.Model):
|
||||
class ContestRank(models.Model):
|
||||
user = models.ForeignKey(User)
|
||||
contest = models.ForeignKey(Contest)
|
||||
total_submission_number = models.IntegerField(default=0)
|
||||
submission_number = models.IntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class ACMContestRank(ContestRank):
|
||||
total_ac_number = models.IntegerField(default=0)
|
||||
accepted_number = models.IntegerField(default=0)
|
||||
# total_time is only for ACM contest total_time = ac time + none-ac times * 20 * 60
|
||||
total_time = models.IntegerField(default=0)
|
||||
# {23: {"is_ac": True, "ac_time": 8999, "error_number": 2, "is_first_ac": True}}
|
||||
@@ -92,7 +92,7 @@ class OIContestRank(ContestRank):
|
||||
db_table = "oi_contest_rank"
|
||||
|
||||
def update_rank(self, submission):
|
||||
self.total_submission_number += 1
|
||||
self.submission_number += 1
|
||||
|
||||
|
||||
class ContestAnnouncement(models.Model):
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import pickle
|
||||
from django.utils.timezone import now
|
||||
from django.db.models import Q
|
||||
from django.core.cache import cache
|
||||
from utils.api import APIView, validate_serializer
|
||||
from utils.cache import default_cache
|
||||
from utils.constants import CacheKey
|
||||
from account.decorators import login_required, check_contest_permission
|
||||
|
||||
from ..models import ContestAnnouncement, Contest, ContestStatus, ContestRuleType
|
||||
@@ -90,20 +92,25 @@ class ContestAccessAPI(APIView):
|
||||
class ContestRankAPI(APIView):
|
||||
def get_rank(self):
|
||||
if self.contest.rule_type == ContestRuleType.ACM:
|
||||
rank = ACMContestRank.objects.filter(contest=self.contest). \
|
||||
select_related("user").order_by("-total_ac_number", "total_time")
|
||||
print(rank)
|
||||
return ACMContestRankSerializer(rank, many=True).data
|
||||
return ACMContestRank.objects.filter(contest=self.contest). \
|
||||
select_related("user").order_by("-accepted_number", "total_time")
|
||||
else:
|
||||
rank = OIContestRank.objects.filter(contest=self.contest). \
|
||||
return OIContestRank.objects.filter(contest=self.contest). \
|
||||
select_related("user").order_by("-total_score")
|
||||
return OIContestRankSerializer(rank, many=True).data
|
||||
|
||||
@check_contest_permission
|
||||
def get(self, request):
|
||||
cache_key = str(self.contest.id) + "_rank_cache"
|
||||
rank = cache.get(cache_key)
|
||||
if not rank:
|
||||
rank = self.get_rank()
|
||||
cache.set(cache_key, rank)
|
||||
return self.success(rank)
|
||||
if self.contest.rule_type == ContestRuleType.ACM:
|
||||
model, serializer = ACMContestRank, ACMContestRankSerializer
|
||||
else:
|
||||
model, serializer = OIContestRank, OIContestRankSerializer
|
||||
|
||||
cache_key = CacheKey.contest_rank_cache + str(self.contest.id)
|
||||
qs = default_cache.get(cache_key)
|
||||
if not qs:
|
||||
ranks = self.get_rank()
|
||||
default_cache.set(cache_key, pickle.dumps(ranks))
|
||||
else:
|
||||
ranks = pickle.loads(qs)
|
||||
|
||||
return self.success(self.paginate_data(request, ranks, serializer))
|
||||
|
||||
Reference in New Issue
Block a user