def get_context_data(self, **kwargs): data = super(ProblemSubmissionView, self).get_context_data(**kwargs) data['submission'] = submission = get_object_or_404( Submission, pk=self.kwargs.get('sid'), problem_id=self.kwargs.get('pk'), contest__isnull=True) if submission.author == self.request.user and \ submission.is_judged and is_case_download_available(self.request.user, self.kwargs.get('pk')): submission.allow_case_download = True if self.request.user.is_authenticated and ( submission.author == self.request.user or is_problem_manager(self.request.user, submission.problem) or self.request.user.submission_set.filter( problem_id=self.kwargs.get('pk'), status=SubmissionStatus.ACCEPTED).exists()): permission = get_permission_for_submission(self.request.user, submission, special_permission=True) data['submission_block'] = render_submission(submission, permission=permission) if permission == 2 or (self.request.user == submission.author and submission.report_paid): data['report_block'] = render_submission_report(submission.pk) else: data['report_block'] = '' else: raise PermissionDenied( "Code is not public for users who have not got accepted yet.") data['problem'] = submission.problem return data
def post(self, request, pk): try: lang = request.POST.get('lang', '') if lang not in dict(LANG_CHOICE).keys(): raise ValueError("Invalid language.") submission = create_submission(self.problem, self.user, request.POST.get('code', ''), lang, ip=get_ip(request)) running_complete = bool( is_problem_manager(self.user, self.problem) and request.POST.get('complete')) async_task(judge_submission_on_problem, submission, run_until_complete=running_complete) return JsonResponse({ "url": reverse('problem:submission_api', kwargs={ 'pk': self.problem.id, 'sid': submission.id }) }) except Exception as e: return HttpResponseBadRequest(str(e).encode())
def dispatch(self, request, *args, **kwargs): self.problem = get_object_or_404(Problem, pk=kwargs.get('pk')) self.user = request.user self.privileged = is_problem_manager(self.user, self.problem) self.request = request return super(ProblemDetailMixin, self).dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs): try: n = request.POST['answer'] if '-' in n: contest_id, identifier = n.split('-') contest = Contest.objects.get(pk=contest_id) if not is_contest_manager(request.user, contest): raise PermissionError problem = contest.contestproblem_set.get( identifier=identifier).problem else: problem = Problem.objects.get(pk=n) if not is_problem_manager(request.user, problem): raise PermissionError if 'force' in request.GET: new_prob = None else: new_prob = ProblemCreate.get_unused_problem() if not new_prob: new_prob = Problem.objects.create() new_prob.managers.add(request.user) saved_id = new_prob.id problem.clone_parent = problem.id problem.id = saved_id problem.alias = 'p%d' % problem.id problem.visible = False problem.save() except: messages.error(request, "Problem does not exist or not available.") return redirect(reverse('polygon:problem_list_2')) return redirect( reverse('polygon:problem_list_2') + "?exact=%d" % saved_id)
def dispatch(self, request, *args, **kwargs): self.request = request self.problem = get_object_or_404(Problem, pk=kwargs.get('pk')) self.latest_revisions = self.problem.revisions.all().order_by("-revision")[:5] if is_problem_manager(self.request.user, self.problem): self.permission = 2 else: self.permission = 0 self.init_revision(*args, **kwargs) return super().dispatch(request, *args, **kwargs)
def get_queryset(self): try: queryset = self.get_selected_from().select_related('problem', 'author'). \ only('pk', 'contest_id', 'create_time', 'author_id', 'author__username', 'author__magic', 'problem_id', 'problem__title', 'lang', 'status', 'status_time', 'status_percent', 'code_length', 'ip', 'status_test', 'status_memory', 'visible', 'judge_server', 'contest_time') if 'user' in self.request.GET: queryset = queryset.filter(author_id=self.request.GET['user']) if self.allow_problem_query and 'problem' in self.request.GET: problem_id = self.reinterpret_problem_identifier( self.request.GET['problem']) try: if is_problem_manager(self.request.user, Problem.objects.get(pk=problem_id)): self.privileged = True queryset = queryset.filter(problem_id=problem_id) except: pass if 'lang' in self.request.GET: queryset = queryset.filter(lang=self.request.GET['lang']) if self.allow_verdict_query and 'verdict' in self.request.GET: queryset = queryset.filter( status=int(self.request.GET['verdict'][1:])) if is_admin_or_root(self.request.user): self.privileged = True if not self.privileged: queryset = queryset.filter(visible=True) if not self.privileged and not self.contest_submission_visible: queryset = queryset.filter(contest__isnull=True) ordering = self.get_ordering() if ordering is not None: if isinstance(ordering, str): ordering = (ordering, ) queryset = queryset.order_by(*ordering) if self.distinct_by_author: author_set = set() res = [] for submission in queryset.all(): if submission.author_id not in author_set: author_set.add(submission.author_id) res.append(submission) if self.query_number and len(res) >= self.query_number: break return res else: return queryset.all()[:self.query_number] except Exception as e: raise Http404(e)
def create_submission(problem, author: User, code, lang, contest=None, status=SubmissionStatus.WAITING, ip='', visible=True): if not 6 <= len(code) <= 65536: raise ValueError("代码不得小于 6 字节,不得超过 65536 字节。") if lang == "java": matching_regex = r"public\s+(final\s+)?class\s+Main" if re.search(matching_regex, code) is None: raise ValueError("Java 语言应匹配正则:" + matching_regex) if author.submission_set.exists() and ( datetime.now() - author.submission_set.first().create_time ).total_seconds() < settings.SUBMISSION_INTERVAL_LIMIT: raise ValueError("5 秒内只能提交一次。") if contest: if contest.submission_set.filter(author=author, problem_id=problem, code__exact=code, lang=lang).exists(): raise ValueError("你之前交过完全一样的代码。") if isinstance(problem, (int, str)): problem = Problem.objects.get(pk=problem) if contest is not None: if is_contest_manager(author, contest): visible = False elif is_problem_manager(author, problem): visible = False return Submission.objects.create(lang=lang, code=code, author=author, problem=problem, contest=contest, status=status, ip=ip, visible=visible)
def is_problem_accepted(user, problem): return is_problem_manager(user, problem) or (user.is_authenticated and user.submission_set.filter(problem=problem, status=SubmissionStatus.ACCEPTED).exists())
def test_func(self): if is_problem_manager(self.request.user, self.submission.problem) or \ (self.submission.contest and is_contest_manager(self.request.user, self.submission.contest)): return super(RejudgeSubmission, self).test_func() return False
def test_func(self): if not is_problem_manager(self.request.user, self.problem): return False return super(PolygonProblemMixin, self).test_func()