Exemplo n.º 1
0
def problem_submit(request, problem=None, submission=None):
    try:
        if submission is not None and Submission.objects.get(id=int(submission)).user.user != request.user:
            raise PermissionDenied()
    except Submission.DoesNotExist:
        raise Http404()

    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST, instance=Submission(user=request.user.profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission') and
                        Submission.objects.filter(user=request.user.profile).exclude(
                                status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse('<h1>You submitted too many submissions.</h1>', status=503)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            model = form.save()

            cp = request.user.profile.contest
            if cp.current is not None:
                try:
                    contest_problem = model.problem.contests.get(contest=cp.current.contest)
                except ContestProblem.DoesNotExist:
                    pass
                else:
                    contest = ContestSubmission(submission=model, problem=contest_problem,
                                                participation=cp.current)
                    contest.save()

            model.judge()
            return HttpResponseRedirect(reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
    else:
        initial = {'language': request.user.profile.language}
        if problem is not None:
            try:
                initial['problem'] = Problem.objects.get(code=problem)
            except ObjectDoesNotExist:
                raise Http404()
        if submission is not None:
            try:
                sub = Submission.objects.get(id=int(submission))
                initial['source'] = sub.source
                initial['language'] = sub.language
            except (ObjectDoesNotExist, ValueError):
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = form_data['problem'].allowed_languages
    form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = request.user.profile.ace_theme
    return render_to_response('problem/submit.jade', {
        'form': form,
        'title': 'Submit',
        'langs': Language.objects.all(),
    }, context_instance=RequestContext(request))
Exemplo n.º 2
0
def problem_submit(request, problem=None, submission=None):
    if submission is not None and not request.user.has_perm('judge.resubmit_other') and \
                    get_object_or_404(Submission, id=int(submission)).user.user != request.user:
        raise PermissionDenied()

    profile = request.user.profile
    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST,
                                 instance=Submission(user=profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission')
                    and Submission.objects.filter(
                        user=profile, was_rejudged=False).exclude(
                            status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse(
                    '<h1>You submitted too many submissions.</h1>', status=503)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            if not form.cleaned_data['problem'].is_accessible_by(request.user):
                user_logger.info(
                    'Naughty user %s wants to submit to %s without permission',
                    request.user.username, form.cleaned_data['problem'].code)
                return HttpResponseForbidden(
                    '<h1>Do you want me to ban you?</h1>')
            if not request.user.is_superuser and form.cleaned_data[
                    'problem'].banned_users.filter(id=profile.id).exists():
                return generic_message(
                    request, _('Banned from submitting'),
                    _('You have been declared persona non grata for this problem. '
                      'You are permanently barred from submitting this problem.'
                      ))

            if profile.current_contest is not None:
                try:
                    contest_problem = form.cleaned_data[
                        'problem'].contests.get(
                            contest=profile.current_contest.contest)
                except ContestProblem.DoesNotExist:
                    model = form.save()
                else:
                    max_subs = contest_problem.max_submissions
                    if max_subs and get_contest_submission_count(
                            problem, profile) >= max_subs:
                        return generic_message(
                            request, _('Too many submissions'),
                            _('You have exceeded the submission limit for this problem.'
                              ))
                    model = form.save()
                    contest = ContestSubmission(
                        submission=model,
                        problem=contest_problem,
                        participation=profile.current_contest)
                    contest.save()
            else:
                model = form.save()

            profile.update_contest()
            model.judge(rejudge=False)
            return HttpResponseRedirect(
                reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
            if submission is not None:
                sub = get_object_or_404(Submission, id=int(submission))
    else:
        initial = {'language': profile.language}
        if problem is not None:
            initial['problem'] = get_object_or_404(Problem, code=problem)
            problem_object = initial['problem']
            if not problem_object.is_accessible_by(request.user):
                raise Http404()
        if submission is not None:
            try:
                sub = get_object_or_404(Submission, id=int(submission))
                initial['source'] = sub.source
                initial['language'] = sub.language
            except ValueError:
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = (
            form_data['problem'].usable_languages.order_by(
                'name', 'key').prefetch_related(
                    Prefetch('runtimeversion_set',
                             RuntimeVersion.objects.order_by('priority'))))
        problem_object = form_data['problem']
    if 'language' in form_data:
        form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = profile.ace_theme

    if submission is not None:
        default_lang = sub.language
    else:
        default_lang = request.user.profile.language

    submission_limit = submissions_left = None
    if profile.current_contest is not None:
        try:
            submission_limit = problem_object.contests.get(
                contest=profile.current_contest.contest).max_submissions
        except ContestProblem.DoesNotExist:
            pass
        else:
            if submission_limit:
                submissions_left = submission_limit - get_contest_submission_count(
                    problem, profile)
    return render(
        request, 'problem/submit.html', {
            'form':
            form,
            'title':
            _('Submit to %(problem)s') % {
                'problem': problem_object.translated_name(
                    request.LANGUAGE_CODE),
            },
            'content_title':
            mark_safe(
                escape(_('Submit to %(problem)s')) % {
                    'problem':
                    format_html(
                        u'<a href="{0}">{1}</a>',
                        reverse('problem_detail', args=[problem_object.code]),
                        problem_object.translated_name(request.LANGUAGE_CODE))
                }),
            'langs':
            Language.objects.all(),
            'no_judges':
            not form.fields['language'].queryset,
            'submission_limit':
            submission_limit,
            'submissions_left':
            submissions_left,
            'ACE_URL':
            ACE_URL,
            'default_lang':
            default_lang,
        })
Exemplo n.º 3
0
def problem_submit(request, problem=None, submission=None):
    if submission is not None and not request.user.has_perm('judge.resubmit_other') and \
                    get_object_or_404(Submission, id=int(submission)).user.user != request.user:
        raise PermissionDenied()

    profile = request.user.profile
    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST, instance=Submission(user=profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission') and
                        Submission.objects.filter(user=profile, is_being_rejudged=False).exclude(
                            status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse('<h1>You submitted too many submissions.</h1>', status=503)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            if not form.cleaned_data['problem'].is_accessible_by(request.user):
                user_logger.info('Naughty user %s wants to submit to %s without permission',
                                 request.user.username, form.cleaned_data['problem'].code)
                return HttpResponseForbidden('<h1>Do you want me to ban you?</h1>')
            if not request.user.is_superuser and form.cleaned_data['problem'].banned_users.filter(
                    id=profile.id).exists():
                return generic_message(request, _('Banned from Submitting'),
                                       _('You have been declared persona non grata for this problem. '
                                         'You are permanently barred from submitting this problem.'))
            model = form.save()

            profile.update_contest()
            if profile.current_contest is not None:
                try:
                    contest_problem = model.problem.contests.get(contest=profile.current_contest.contest)
                except ContestProblem.DoesNotExist:
                    pass
                else:
                    contest = ContestSubmission(submission=model, problem=contest_problem,
                                                participation=profile.current_contest)
                    contest.save()

            model.judge()
            return HttpResponseRedirect(reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
    else:
        initial = {'language': profile.language}
        if problem is not None:
            initial['problem'] = get_object_or_404(Problem, code=problem)
            if not initial['problem'].is_accessible_by(request.user):
                raise Http404()
        if submission is not None:
            try:
                sub = get_object_or_404(Submission, id=int(submission))
                initial['source'] = sub.source
                initial['language'] = sub.language
            except ValueError:
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = (form_data['problem'].usable_languages.order_by('name', 'key')
                                            .prefetch_related(
            Prefetch('runtimeversion_set', RuntimeVersion.objects.order_by('priority'))))
    if 'language' in form_data:
        form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = profile.ace_theme
    return render(request, 'problem/submit.jade', {
        'form': form,
        'title': _('Submit'),
        'langs': Language.objects.all(),
        'no_judges': not form.fields['language'].queryset,
        'ACE_URL': ACE_URL
    })
Exemplo n.º 4
0
Arquivo: problem.py Projeto: DMOJ/site
def problem_submit(request, problem=None, submission=None):
    if submission is not None and not request.user.has_perm('judge.resubmit_other') and \
                    get_object_or_404(Submission, id=int(submission)).user.user != request.user:
        raise PermissionDenied()

    profile = request.user.profile
    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST, instance=Submission(user=profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission') and
                        Submission.objects.filter(user=profile, was_rejudged=False).exclude(
                            status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse('<h1>You submitted too many submissions.</h1>', status=429)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            if not form.cleaned_data['problem'].is_accessible_by(request.user):
                user_logger.info('Naughty user %s wants to submit to %s without permission',
                                 request.user.username, form.cleaned_data['problem'].code)
                return HttpResponseForbidden('<h1>Do you want me to ban you?</h1>')
            if not request.user.is_superuser and form.cleaned_data['problem'].banned_users.filter(
                    id=profile.id).exists():
                return generic_message(request, _('Banned from submitting'),
                                       _('You have been declared persona non grata for this problem. '
                                         'You are permanently barred from submitting this problem.'))

            with transaction.atomic():
                if profile.current_contest is not None:
                    try:
                        contest_problem = form.cleaned_data['problem'].contests.get(contest=profile.current_contest.contest)
                    except ContestProblem.DoesNotExist:
                        model = form.save()
                    else:
                        max_subs = contest_problem.max_submissions
                        if max_subs and get_contest_submission_count(problem, profile) >= max_subs:
                            return generic_message(request, _('Too many submissions'),
                                                    _('You have exceeded the submission limit for this problem.'))
                        model = form.save()
                        contest = ContestSubmission(submission=model, problem=contest_problem,
                                                    participation=profile.current_contest)
                        contest.save()
                else:
                    model = form.save()

                # Create the SubmissionSource object
                source = SubmissionSource(submission=model, source=form.cleaned_data['source'])
                source.save()
                profile.update_contest()

            # Save a query
            model.source = source
            model.judge(rejudge=False)

            return HttpResponseRedirect(reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
            if submission is not None:
                sub = get_object_or_404(Submission, id=int(submission))
    else:
        initial = {'language': profile.language}
        if problem is not None:
            initial['problem'] = get_object_or_404(Problem, code=problem)
            problem_object = initial['problem']
            if not problem_object.is_accessible_by(request.user):
                raise Http404()
        if submission is not None:
            try:
                sub = get_object_or_404(Submission.objects.select_related('source', 'language'), id=int(submission))
                initial['source'] = sub.source.source
                initial['language'] = sub.language
            except ValueError:
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = (form_data['problem'].usable_languages.order_by('name', 'key')
            .prefetch_related(Prefetch('runtimeversion_set', RuntimeVersion.objects.order_by('priority'))))
        problem_object = form_data['problem']
    if 'language' in form_data:
        form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = profile.ace_theme

    if submission is not None:
        default_lang = sub.language
    else:
        default_lang = request.user.profile.language

    submission_limit = submissions_left = None
    if profile.current_contest is not None:
        try:
            submission_limit = problem_object.contests.get(contest=profile.current_contest.contest).max_submissions
        except ContestProblem.DoesNotExist:
            pass
        else:
            if submission_limit:
                submissions_left = submission_limit - get_contest_submission_count(problem, profile)
    return render(request, 'problem/submit.html', {
        'form': form,
        'title': _('Submit to %(problem)s') % {
            'problem': problem_object.translated_name(request.LANGUAGE_CODE),
        },
        'content_title': mark_safe(escape(_('Submit to %(problem)s')) % {
            'problem': format_html('<a href="{0}">{1}</a>',
                                   reverse('problem_detail', args=[problem_object.code]),
                                   problem_object.translated_name(request.LANGUAGE_CODE))
        }),
        'langs': Language.objects.all(),
        'no_judges': not form.fields['language'].queryset,
        'submission_limit': submission_limit,
        'submissions_left': submissions_left,
        'ACE_URL': ACE_URL,

        'default_lang': default_lang,
    })
Exemplo n.º 5
0
def problem_submit(request, problem=None, submission=None):
    try:
        if submission is not None and not request.user.has_perm('judge.resubmit_other') and \
                Submission.objects.get(id=int(submission)).user.user != request.user:
            raise PermissionDenied()
    except Submission.DoesNotExist:
        raise Http404()

    profile = request.user.profile
    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST,
                                 instance=Submission(user=profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission')
                    and Submission.objects.filter(user=profile).exclude(
                        status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse(
                    '<h1>You submitted too many submissions.</h1>', status=503)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            if not request.user.is_superuser and form.cleaned_data[
                    'problem'].banned_users.filter(id=profile.id).exists():
                return generic_message(
                    request, 'Banned from Submitting',
                    'You have been declared persona non grata for this problem. '
                    'You are permanently barred from submitting this problem.')
            model = form.save()

            cp = profile.contest
            if cp.current is not None:
                try:
                    contest_problem = model.problem.contests.get(
                        contest=cp.current.contest)
                except ContestProblem.DoesNotExist:
                    pass
                else:
                    contest = ContestSubmission(submission=model,
                                                problem=contest_problem,
                                                participation=cp.current)
                    contest.save()

            model.judge()
            return HttpResponseRedirect(
                reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
    else:
        initial = {'language': profile.language}
        if problem is not None:
            try:
                initial['problem'] = Problem.objects.get(code=problem)
            except ObjectDoesNotExist:
                raise Http404()
        if submission is not None:
            try:
                sub = Submission.objects.get(id=int(submission))
                initial['source'] = sub.source
                initial['language'] = sub.language
            except (ObjectDoesNotExist, ValueError):
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = form_data[
            'problem'].usable_languages
    if 'language' in form_data:
        form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = profile.ace_theme
    return render(
        request, 'problem/submit.jade', {
            'form': form,
            'title': 'Submit',
            'langs': Language.objects.all(),
            'no_judges': not form.fields['language'].queryset
        })
Exemplo n.º 6
0
def problem_submit(request, problem=None, submission=None):
    try:
        if (
            submission is not None
            and not request.user.has_perm("judge.resubmit_other")
            and Submission.objects.get(id=int(submission)).user.user != request.user
        ):
            raise PermissionDenied()
    except Submission.DoesNotExist:
        raise Http404()

    profile = request.user.profile
    if request.method == "POST":
        form = ProblemSubmitForm(request.POST, instance=Submission(user=profile))
        if form.is_valid():
            if (
                not request.user.has_perm("judge.spam_submission")
                and Submission.objects.filter(user=profile).exclude(status__in=["D", "IE", "CE", "AB"]).count() > 2
            ):
                return HttpResponse("<h1>You submitted too many submissions.</h1>", status=503)
            if not form.cleaned_data["problem"].allowed_languages.filter(id=form.cleaned_data["language"].id).exists():
                raise PermissionDenied()
            if (
                not request.user.is_superuser
                and form.cleaned_data["problem"].banned_users.filter(id=profile.id).exists()
            ):
                return generic_message(
                    request,
                    "Banned from Submitting",
                    "You have been declared persona non grata for this problem. "
                    "You are permanently barred from submitting this problem.",
                )
            model = form.save()

            cp = profile.contest
            if cp.current is not None:
                try:
                    contest_problem = model.problem.contests.get(contest=cp.current.contest)
                except ContestProblem.DoesNotExist:
                    pass
                else:
                    contest = ContestSubmission(submission=model, problem=contest_problem, participation=cp.current)
                    contest.save()

            model.judge()
            return HttpResponseRedirect(reverse("submission_status", args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
    else:
        initial = {"language": profile.language}
        if problem is not None:
            try:
                initial["problem"] = Problem.objects.get(code=problem)
            except ObjectDoesNotExist:
                raise Http404()
        if submission is not None:
            try:
                sub = Submission.objects.get(id=int(submission))
                initial["source"] = sub.source
                initial["language"] = sub.language
            except (ObjectDoesNotExist, ValueError):
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if "problem" in form_data:
        form.fields["language"].queryset = form_data["problem"].usable_languages
    if "language" in form_data:
        form.fields["source"].widget.mode = form_data["language"].ace
    form.fields["source"].widget.theme = profile.ace_theme
    return render(
        request,
        "problem/submit.jade",
        {
            "form": form,
            "title": "Submit",
            "langs": Language.objects.all(),
            "no_judges": not form.fields["language"].queryset,
        },
    )
Exemplo n.º 7
0
def problem_submit(request, problem=None, submission=None):
    try:
        if submission is not None and not request.user.has_perm('judge.resubmit_other') and \
                Submission.objects.get(id=int(submission)).user.user != request.user:
            raise PermissionDenied()
    except Submission.DoesNotExist:
        raise Http404()

    profile = request.user.profile
    if request.method == 'POST':
        form = ProblemSubmitForm(request.POST, instance=Submission(user=profile))
        if form.is_valid():
            if (not request.user.has_perm('judge.spam_submission') and
                        Submission.objects.filter(user=profile).exclude(
                                status__in=['D', 'IE', 'CE', 'AB']).count() > 2):
                return HttpResponse('<h1>You submitted too many submissions.</h1>', status=503)
            if not form.cleaned_data['problem'].allowed_languages.filter(
                    id=form.cleaned_data['language'].id).exists():
                raise PermissionDenied()
            if not request.user.is_superuser and form.cleaned_data['problem'].banned_users.filter(id=profile.id).exists():
                return generic_message(request, _('Banned from Submitting'),
                                       _('You have been declared persona non grata for this problem. '
                                         'You are permanently barred from submitting this problem.'))
            model = form.save()

            cp = profile.contest
            if cp.current is not None:
                try:
                    contest_problem = model.problem.contests.get(contest=cp.current.contest)
                except ContestProblem.DoesNotExist:
                    pass
                else:
                    contest = ContestSubmission(submission=model, problem=contest_problem,
                                                participation=cp.current)
                    contest.save()

            model.judge()
            return HttpResponseRedirect(reverse('submission_status', args=[str(model.id)]))
        else:
            form_data = form.cleaned_data
    else:
        initial = {'language': profile.language}
        if problem is not None:
            try:
                initial['problem'] = Problem.objects.get(code=problem)
            except ObjectDoesNotExist:
                raise Http404()
        if submission is not None:
            try:
                sub = Submission.objects.get(id=int(submission))
                initial['source'] = sub.source
                initial['language'] = sub.language
            except (ObjectDoesNotExist, ValueError):
                raise Http404()
        form = ProblemSubmitForm(initial=initial)
        form_data = initial
    if 'problem' in form_data:
        form.fields['language'].queryset = form_data['problem'].usable_languages
    if 'language' in form_data:
        form.fields['source'].widget.mode = form_data['language'].ace
    form.fields['source'].widget.theme = profile.ace_theme
    return render(request, 'problem/submit.jade', {
        'form': form,
        'title': _('Submit'),
        'langs': Language.objects.all(),
        'no_judges': not form.fields['language'].queryset
    })