2017-08-01 08:52:48 +00:00
|
|
|
from django.utils.timezone import now
|
2017-10-01 20:33:43 +00:00
|
|
|
from django.core.cache import cache
|
2017-07-17 13:28:06 +00:00
|
|
|
from utils.api import APIView, validate_serializer
|
2017-08-26 00:41:29 +00:00
|
|
|
from utils.constants import CacheKey
|
2017-08-01 08:52:48 +00:00
|
|
|
from account.decorators import login_required, check_contest_permission
|
2017-01-29 09:04:56 +00:00
|
|
|
|
2017-10-06 09:46:14 +00:00
|
|
|
from utils.constants import ContestRuleType, ContestStatus
|
2017-10-01 21:16:14 +00:00
|
|
|
from ..models import ContestAnnouncement, Contest, OIContestRank, ACMContestRank
|
2017-01-29 09:04:56 +00:00
|
|
|
from ..serializers import ContestAnnouncementSerializer
|
2017-07-17 13:28:06 +00:00
|
|
|
from ..serializers import ContestSerializer, ContestPasswordVerifySerializer
|
2017-08-01 08:52:48 +00:00
|
|
|
from ..serializers import OIContestRankSerializer, ACMContestRankSerializer
|
2017-01-29 09:04:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestAnnouncementListAPI(APIView):
|
2017-10-27 10:36:29 +00:00
|
|
|
@check_contest_permission(check_type="announcements")
|
2017-01-29 09:04:56 +00:00
|
|
|
def get(self, request):
|
|
|
|
contest_id = request.GET.get("contest_id")
|
|
|
|
if not contest_id:
|
|
|
|
return self.error("Invalid parameter")
|
2017-08-29 11:26:38 +00:00
|
|
|
data = ContestAnnouncement.objects.select_related("created_by").filter(contest_id=contest_id)
|
2017-01-29 09:04:56 +00:00
|
|
|
max_id = request.GET.get("max_id")
|
|
|
|
if max_id:
|
|
|
|
data = data.filter(id__gt=max_id)
|
|
|
|
return self.success(ContestAnnouncementSerializer(data, many=True).data)
|
2017-06-22 06:10:32 +00:00
|
|
|
|
|
|
|
|
2017-07-17 13:28:06 +00:00
|
|
|
class ContestAPI(APIView):
|
2017-10-27 10:36:29 +00:00
|
|
|
@check_contest_permission(check_type="details")
|
2017-06-22 06:10:32 +00:00
|
|
|
def get(self, request):
|
2017-10-27 10:36:29 +00:00
|
|
|
return self.success(ContestSerializer(self.contest).data)
|
2017-06-22 06:10:32 +00:00
|
|
|
|
2017-10-27 10:36:29 +00:00
|
|
|
|
|
|
|
class ContestListAPI(APIView):
|
|
|
|
def get(self, request):
|
2017-08-29 11:26:38 +00:00
|
|
|
contests = Contest.objects.select_related("created_by").filter(visible=True)
|
2017-06-22 06:10:32 +00:00
|
|
|
keyword = request.GET.get("keyword")
|
2017-08-01 08:52:48 +00:00
|
|
|
rule_type = request.GET.get("rule_type")
|
|
|
|
status = request.GET.get("status")
|
2017-06-22 06:10:32 +00:00
|
|
|
if keyword:
|
|
|
|
contests = contests.filter(title__contains=keyword)
|
2017-08-01 08:52:48 +00:00
|
|
|
if rule_type:
|
|
|
|
contests = contests.filter(rule_type=rule_type)
|
|
|
|
if status:
|
|
|
|
cur = now()
|
|
|
|
if status == ContestStatus.CONTEST_NOT_START:
|
|
|
|
contests = contests.filter(start_time__gt=cur)
|
|
|
|
elif status == ContestStatus.CONTEST_ENDED:
|
|
|
|
contests = contests.filter(end_time__lt=cur)
|
|
|
|
else:
|
2017-10-01 20:33:43 +00:00
|
|
|
contests = contests.filter(start_time__lte=cur, end_time__gte=cur)
|
2017-10-27 10:36:29 +00:00
|
|
|
data = self.paginate_data(request, contests, ContestSerializer)
|
|
|
|
return self.success(data)
|
2017-07-17 13:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestPasswordVerifyAPI(APIView):
|
|
|
|
@validate_serializer(ContestPasswordVerifySerializer)
|
|
|
|
@login_required
|
2017-07-18 03:18:18 +00:00
|
|
|
def post(self, request):
|
2017-07-17 13:28:06 +00:00
|
|
|
data = request.data
|
|
|
|
try:
|
|
|
|
contest = Contest.objects.get(id=data["contest_id"], visible=True, password__isnull=False)
|
|
|
|
except Contest.DoesNotExist:
|
2017-10-01 20:33:43 +00:00
|
|
|
return self.error("Contest does not exist")
|
2017-07-17 13:28:06 +00:00
|
|
|
if contest.password != data["password"]:
|
2017-10-01 20:33:43 +00:00
|
|
|
return self.error("Wrong password")
|
2017-07-17 13:28:06 +00:00
|
|
|
|
|
|
|
# password verify OK.
|
2017-10-01 20:33:43 +00:00
|
|
|
if "accessible_contests" not in request.session:
|
|
|
|
request.session["accessible_contests"] = []
|
2017-10-16 01:45:29 +00:00
|
|
|
request.session["accessible_contests"].append(contest.id)
|
2017-07-17 13:28:06 +00:00
|
|
|
# https://docs.djangoproject.com/en/dev/topics/http/sessions/#when-sessions-are-saved
|
|
|
|
request.session.modified = True
|
|
|
|
return self.success(True)
|
2017-07-20 07:52:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestAccessAPI(APIView):
|
|
|
|
@login_required
|
|
|
|
def get(self, request):
|
|
|
|
contest_id = request.GET.get("contest_id")
|
|
|
|
if not contest_id:
|
2017-10-01 20:33:43 +00:00
|
|
|
return self.error()
|
|
|
|
return self.success({"access": int(contest_id) in request.session.get("accessible_contests", [])})
|
2017-08-01 08:52:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContestRankAPI(APIView):
|
|
|
|
def get_rank(self):
|
2017-08-23 09:01:55 +00:00
|
|
|
if self.contest.rule_type == ContestRuleType.ACM:
|
2017-08-26 00:41:29 +00:00
|
|
|
return ACMContestRank.objects.filter(contest=self.contest). \
|
|
|
|
select_related("user").order_by("-accepted_number", "total_time")
|
2017-08-01 08:52:48 +00:00
|
|
|
else:
|
2017-08-26 00:41:29 +00:00
|
|
|
return OIContestRank.objects.filter(contest=self.contest). \
|
2017-08-01 08:52:48 +00:00
|
|
|
select_related("user").order_by("-total_score")
|
|
|
|
|
2017-10-27 10:36:29 +00:00
|
|
|
@check_contest_permission(check_type="ranks")
|
2017-08-01 08:52:48 +00:00
|
|
|
def get(self, request):
|
2017-10-16 01:45:29 +00:00
|
|
|
if self.contest.rule_type == ContestRuleType.OI:
|
2017-08-29 11:26:38 +00:00
|
|
|
serializer = OIContestRankSerializer
|
2017-10-16 01:45:29 +00:00
|
|
|
else:
|
|
|
|
serializer = ACMContestRankSerializer
|
2017-08-26 00:41:29 +00:00
|
|
|
|
2017-10-01 20:33:43 +00:00
|
|
|
cache_key = f"{CacheKey.contest_rank_cache}:{self.contest.id}"
|
|
|
|
qs = cache.get(cache_key)
|
2017-08-26 00:41:29 +00:00
|
|
|
if not qs:
|
2017-10-01 20:33:43 +00:00
|
|
|
qs = self.get_rank()
|
|
|
|
cache.set(cache_key, qs)
|
|
|
|
return self.success(self.paginate_data(request, qs, serializer))
|