Esempio n. 1
0
    def post(self, request, cid, pid):
        try:
            if self.contest.status < 0 and not self.privileged:  # pending contest
                raise ValueError("Contest is not running.")
            if self.contest.run_tests_during_contest != 'all' and self.contest.status != 0 and \
                    not self.contest.system_tested and not self.privileged:  # pending result
                raise ValueError(
                    "Contest is still waiting for system tests. Try again later."
                )
            lang = request.POST.get('lang', '')
            if lang not in self.contest.supported_language_list:
                raise ValueError("Invalid language.")
            try:
                problem = self.contest.contestproblem_set.get(
                    identifier=pid).problem_id
            except ContestProblem.DoesNotExist:
                raise ValueError("Invalid problem.")

            code = request.POST.get('code', '')
            if self.contest.status < 0:
                submission = create_submission(problem,
                                               self.user,
                                               code,
                                               lang,
                                               ip=get_ip(request))
            else:
                submission = create_submission(problem,
                                               self.user,
                                               code,
                                               lang,
                                               contest=self.contest,
                                               ip=get_ip(request))
                contest_participant, created = self.contest.contestparticipant_set.get_or_create(
                    user=self.user)
                if created and (self.contest.status != 0
                                or self.contest.access_level == 30):
                    contest_participant.star = True
                    contest_participant.save(update_fields=['star'])
                if contest_participant.is_disabled:
                    raise ValueError("You have quitted the contest.")
            response = {
                "url":
                reverse('contest:submission_api',
                        kwargs={
                            'cid': self.contest.id,
                            'sid': submission.id
                        })
            }
            async (judge_submission_on_contest,
                   submission,
                   contest=self.contest)
            return JsonResponse(response)
        except Exception as e:
            return HttpResponseBadRequest(str(e).encode())
Esempio n. 2
0
 def post(self, request, cid):
     try:
         if self.contest.status != 0:
             raise ValueError("Contest is not running.")
         lang = request.POST.get('lang', '')
         if lang not in self.contest.supported_language_list:
             raise ValueError("Invalid language.")
         try:
             problem = self.contest.contestproblem_set.get(
                 identifier=request.POST.get('problem', '')).problem_id
         except ContestProblem.DoesNotExist:
             raise ValueError("Invalid problem.")
         submission = create_submission(problem,
                                        self.user,
                                        request.POST.get('code', ''),
                                        lang,
                                        contest=self.contest,
                                        ip=get_ip(request))
         contest_participant, _ = self.contest.contestparticipant_set.get_or_create(
             user=self.user)
         if contest_participant.is_disabled:
             raise ValueError("You have quitted the contest.")
         judge_submission_on_contest(submission)
         return JsonResponse({
             "url":
             reverse('contest:submission_api',
                     kwargs={
                         'cid': self.contest.id,
                         'sid': submission.id
                     })
         })
     except Exception as e:
         return HttpResponseBadRequest(str(e).encode())
Esempio n. 3
0
    def post(self, request, cid, pid):
        try:
            if self.participate_contest_status < 0 and not self.privileged:  # pending contest
                raise ValueError("比赛尚未开始。")
            if self.contest.run_tests_during_contest != 'all' and self.participate_contest_status != 0 and \
                    not self.contest.system_tested and not self.privileged:  # pending result
                raise ValueError("比赛仍在等待系统测试。")
            lang = request.POST.get('lang', '')
            if lang not in self.contest.supported_language_list:
                raise ValueError("语言无效。")
            try:
                problem = self.contest.contestproblem_set.get(identifier=pid).problem_id
            except ContestProblem.DoesNotExist:
                raise ValueError("题目无效。")

            code = request.POST.get('code', '')
            if self.contest.status < 0:
                submission = create_submission(problem, self.user, code, lang, ip=get_ip(request))
            elif self.contest.status != 0 and self.contest.contest_type == 1:
                raise ValueError("你已经错过了作业提交时间。")
            else:
                submission = create_submission(problem, self.user, code, lang,
                                               contest=self.contest, ip=get_ip(request))
                contest_participant, created = self.contest.contestparticipant_set.get_or_create(user=self.user)

                if self.contest.contest_type == 0:
                    start_time = contest_participant.start_time(self.contest)
                    end_time = start_time + self.contest.length
                    if start_time <= submission.create_time <= end_time:
                        submission.contest_time = submission.create_time - start_time
                        submission.save(update_fields=["contest_time"])

                if created and (self.contest.status != 0 or self.contest.access_level == 30):
                    contest_participant.star = True
                    contest_participant.save(update_fields=['star'])
                if contest_participant.is_disabled:
                    raise ValueError("你已退出比赛。")
            response = {"url": reverse('contest:submission_api',
                                       kwargs={'cid': self.contest.id, 'sid': submission.id})}
            async_task(judge_submission_on_contest, submission, contest=self.contest)
            return JsonResponse(response)
        except Exception as e:
            return HttpResponseBadRequest(str(e).encode())