2017-10-06 09:46:14 +00:00
|
|
|
from utils.constants import ContestRuleType # noqa
|
2016-09-25 09:00:52 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.timezone import now
|
2017-10-11 13:43:29 +00:00
|
|
|
from utils.models import JSONField
|
2016-09-25 09:00:52 +00:00
|
|
|
|
2017-10-06 09:46:14 +00:00
|
|
|
from utils.constants import ContestStatus, ContestType
|
2017-11-10 11:40:54 +00:00
|
|
|
from account.models import User
|
2016-09-25 09:00:52 +00:00
|
|
|
from utils.models import RichTextField
|
|
|
|
|
|
|
|
|
|
|
|
class Contest(models.Model):
|
2017-01-26 04:33:00 +00:00
|
|
|
title = models.CharField(max_length=40)
|
2016-09-25 09:00:52 +00:00
|
|
|
description = RichTextField()
|
|
|
|
# show real time rank or cached rank
|
|
|
|
real_time_rank = models.BooleanField()
|
|
|
|
password = models.CharField(max_length=30, blank=True, null=True)
|
|
|
|
# enum of ContestRuleType
|
2017-01-24 05:22:40 +00:00
|
|
|
rule_type = models.CharField(max_length=36)
|
2016-09-25 09:00:52 +00:00
|
|
|
start_time = models.DateTimeField()
|
|
|
|
end_time = models.DateTimeField()
|
|
|
|
create_time = models.DateTimeField(auto_now_add=True)
|
2017-01-26 04:33:00 +00:00
|
|
|
last_update_time = models.DateTimeField(auto_now=True)
|
2016-09-25 09:00:52 +00:00
|
|
|
created_by = models.ForeignKey(User)
|
|
|
|
# 是否可见 false的话相当于删除
|
|
|
|
visible = models.BooleanField(default=True)
|
2017-11-10 11:40:54 +00:00
|
|
|
allowed_ip_ranges = JSONField(default=list)
|
2016-09-25 09:00:52 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self):
|
|
|
|
if self.start_time > now():
|
|
|
|
# 没有开始 返回1
|
|
|
|
return ContestStatus.CONTEST_NOT_START
|
|
|
|
elif self.end_time < now():
|
|
|
|
# 已经结束 返回-1
|
|
|
|
return ContestStatus.CONTEST_ENDED
|
|
|
|
else:
|
|
|
|
# 正在进行 返回0
|
|
|
|
return ContestStatus.CONTEST_UNDERWAY
|
|
|
|
|
2017-01-26 05:45:39 +00:00
|
|
|
@property
|
|
|
|
def contest_type(self):
|
|
|
|
if self.password:
|
|
|
|
return ContestType.PASSWORD_PROTECTED_CONTEST
|
|
|
|
return ContestType.PUBLIC_CONTEST
|
|
|
|
|
2017-10-27 10:36:29 +00:00
|
|
|
# 是否有权查看problem 的一些统计信息 诸如submission_number, accepted_number 等
|
|
|
|
def problem_details_permission(self, user):
|
|
|
|
return self.rule_type == ContestRuleType.ACM or \
|
|
|
|
self.status == ContestStatus.CONTEST_ENDED or \
|
2017-11-10 11:40:54 +00:00
|
|
|
user.is_authenticated() and user.is_contest_admin(self) or \
|
2017-10-27 10:36:29 +00:00
|
|
|
self.real_time_rank
|
2017-10-16 01:45:29 +00:00
|
|
|
|
2016-09-25 09:00:52 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = "contest"
|
2017-10-31 08:33:25 +00:00
|
|
|
ordering = ("-start_time",)
|
2016-09-25 09:00:52 +00:00
|
|
|
|
|
|
|
|
2017-10-01 20:33:43 +00:00
|
|
|
class AbstractContestRank(models.Model):
|
2016-09-25 09:00:52 +00:00
|
|
|
user = models.ForeignKey(User)
|
|
|
|
contest = models.ForeignKey(Contest)
|
2017-08-26 00:41:29 +00:00
|
|
|
submission_number = models.IntegerField(default=0)
|
2016-09-25 09:00:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
2017-10-01 20:33:43 +00:00
|
|
|
class ACMContestRank(AbstractContestRank):
|
2017-08-26 00:41:29 +00:00
|
|
|
accepted_number = models.IntegerField(default=0)
|
2017-12-03 07:36:31 +00:00
|
|
|
# total_time is only for ACM contest, total_time = ac time + none-ac times * 20 * 60
|
2016-09-25 09:00:52 +00:00
|
|
|
total_time = models.IntegerField(default=0)
|
2017-12-03 07:36:31 +00:00
|
|
|
# {"23": {"is_ac": True, "ac_time": 8999, "error_number": 2, "is_first_ac": True}}
|
2016-09-25 09:00:52 +00:00
|
|
|
# key is problem id
|
2017-10-11 13:43:29 +00:00
|
|
|
submission_info = JSONField(default=dict)
|
2016-09-25 09:00:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "acm_contest_rank"
|
|
|
|
|
|
|
|
|
2017-10-01 20:33:43 +00:00
|
|
|
class OIContestRank(AbstractContestRank):
|
2016-09-25 09:00:52 +00:00
|
|
|
total_score = models.IntegerField(default=0)
|
2017-12-03 07:36:31 +00:00
|
|
|
# {"23": 333}
|
2017-09-24 01:48:17 +00:00
|
|
|
# key is problem id, value is current score
|
2017-10-11 13:43:29 +00:00
|
|
|
submission_info = JSONField(default=dict)
|
2016-09-25 09:00:52 +00:00
|
|
|
|
|
|
|
class Meta:
|
2017-01-24 05:22:40 +00:00
|
|
|
db_table = "oi_contest_rank"
|
2017-01-25 04:50:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestAnnouncement(models.Model):
|
|
|
|
contest = models.ForeignKey(Contest)
|
|
|
|
title = models.CharField(max_length=128)
|
|
|
|
content = RichTextField()
|
2017-01-29 09:04:56 +00:00
|
|
|
created_by = models.ForeignKey(User)
|
2017-11-06 11:05:21 +00:00
|
|
|
visible = models.BooleanField(default=True)
|
2017-01-25 04:50:35 +00:00
|
|
|
create_time = models.DateTimeField(auto_now_add=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
db_table = "contest_announcement"
|
2017-08-01 08:52:48 +00:00
|
|
|
ordering = ("-create_time",)
|