示例#1
0
文件: views.py 项目: neeraj9/oioioi
def show_submission_source_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id, ProgramSubmission)
    raw_source = submission.source_file.read()
    raw_source, decode_error = decode_str(raw_source)
    filename = submission.source_file.file.name
    is_source_safe = False
    try:
        lexer = guess_lexer_for_filename(filename, raw_source)
        formatter = HtmlFormatter(linenos=True, line_number_chars=3, cssclass="syntax-highlight")
        formatted_source = highlight(raw_source, lexer, formatter)
        formatted_source_css = HtmlFormatter().get_style_defs(".syntax-highlight")
        is_source_safe = True
    except ClassNotFound:
        formatted_source = raw_source
        formatted_source_css = ""
    download_url = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission_id}
    )
    return TemplateResponse(
        request,
        "programs/source.html",
        {
            "source": formatted_source,
            "css": formatted_source_css,
            "is_source_safe": is_source_safe,
            "download_url": download_url,
            "decode_error": decode_error,
        },
    )
示例#2
0
文件: views.py 项目: matrach/oioioi
def show_submission_source_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       ProgramSubmission)
    raw_source = submission.source_file.read()
    raw_source, decode_error = decode_str(raw_source)
    filename = submission.source_file.file.name
    is_source_safe = False
    try:
        lexer = guess_lexer_for_filename(filename, raw_source)
        formatter = HtmlFormatter(linenos=True,
                                  line_number_chars=3,
                                  cssclass='syntax-highlight')
        formatted_source = highlight(raw_source, lexer, formatter)
        formatted_source_css = HtmlFormatter() \
                .get_style_defs('.syntax-highlight')
        is_source_safe = True
    except ClassNotFound:
        formatted_source = raw_source
        formatted_source_css = ''
    download_url = reverse('download_submission_source',
                           kwargs={
                               'contest_id': request.contest.id,
                               'submission_id': submission_id
                           })
    return TemplateResponse(
        request, 'programs/source.html', {
            'source': formatted_source,
            'css': formatted_source_css,
            'is_source_safe': is_source_safe,
            'download_url': download_url,
            'decode_error': decode_error
        })
示例#3
0
文件: views.py 项目: matrach/oioioi
def download_output_file_view(request,
                              contest_id,
                              submission_id,
                              testrun_report_id=None):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)
    result = get_testrun_report_or_404(request, submission, testrun_report_id)
    return stream_file(result.output_file, name='output.out')
示例#4
0
def score_reveal_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id)
    controller = request.contest.controller
    if not has_scores_reveal(submission.problem):
        raise Http404
    decision, reason = controller.can_reveal(request, submission)
    if not decision:
        messages.error(request, reason)
    else:
        controller.reveal_score(request, submission)
        messages.success(request, _("Submission score has been revealed."))

    return redirect('submission',
        contest_id=submission.problem_instance.contest_id,
        submission_id=submission.id)
示例#5
0
文件: views.py 项目: matrach/oioioi
def show_input_file_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)
    data = submission.input_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = submission.input_file.size
    download_url = reverse('download_testrun_input',
            kwargs={'contest_id': request.contest.id,
                    'submission_id': submission_id})
    return TemplateResponse(request, 'testrun/data.html', {
        'header': _("Input"),
        'data': data,
        'left': size - get_preview_size_limit(),
        'decode_error': decode_error,
        'download_url': download_url
    })
示例#6
0
文件: views.py 项目: matrach/oioioi
def show_input_file_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)
    data = submission.input_file.read(get_preview_size_limit())
    data, decode_error = decode_str(data)
    size = submission.input_file.size
    download_url = reverse('download_testrun_input',
                           kwargs={
                               'contest_id': request.contest.id,
                               'submission_id': submission_id
                           })
    return TemplateResponse(
        request, 'testrun/data.html', {
            'header': _("Input"),
            'data': data,
            'left': size - get_preview_size_limit(),
            'decode_error': decode_error,
            'download_url': download_url
        })
示例#7
0
文件: views.py 项目: matrach/oioioi
def download_input_file_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)

    return stream_file(submission.input_file, name='input.in')
示例#8
0
文件: views.py 项目: matrach/oioioi
def download_output_file_view(request, contest_id, submission_id,
                              testrun_report_id=None):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)
    result = get_testrun_report_or_404(request, submission, testrun_report_id)
    return stream_file(result.output_file, name='output.out')
示例#9
0
文件: views.py 项目: matrach/oioioi
def source_diff_view(request, contest_id, submission1_id, submission2_id):
    if request.session.get('saved_diff_id'):
        request.session.pop('saved_diff_id')
    submission1 = get_submission_or_404(request, contest_id, submission1_id,
                                        ProgramSubmission)
    submission2 = get_submission_or_404(request, contest_id, submission2_id,
                                        ProgramSubmission)
    source1 = submission1.source_file.read()
    source1, decode_error1 = decode_str(source1)
    source2 = submission2.source_file.read()
    source2, decode_error2 = decode_str(source2)
    source1 = source1.splitlines()
    source2 = source2.splitlines()

    numwidth = len(str(max(len(source1), len(source2))))
    ndiff = difflib.ndiff(source1, source2)

    class DiffLine(object):
        def __init__(self, css_class, text, number):
            self.css_class = css_class
            self.text = text
            self.number = number

    def diffstrip(line):
        return line[2:]

    def numformat(num):
        return str(num).rjust(numwidth)

    diff1, diff2 = [], []
    count1, count2 = 1, 1

    for diffline in ndiff:
        line = diffstrip(diffline)
        line = line.expandtabs(4)
        maxlen = getattr(settings, 'CHARACTERS_IN_LINE', 80)
        parts = (len(line) + maxlen) / maxlen
        line = line.ljust(parts * maxlen)
        for i in xrange(parts):
            f, t = i * maxlen, ((i + 1) * maxlen)
            c1, c2 = numformat(count1), numformat(count2)
            if diffline.startswith('- '):
                diff1.append(DiffLine('left', line[f:t], '' if i else c1))
                diff2.append(DiffLine('empty', '', ''))
            elif diffline.startswith('+ '):
                diff1.append(DiffLine('empty', '', ''))
                diff2.append(DiffLine('right', line[f:t], '' if i else c2))
            elif diffline.startswith('  '):
                diff1.append(DiffLine('both', line[f:t], '' if i else c1))
                diff2.append(DiffLine('both', line[f:t], '' if i else c2))
        if diffline.startswith('- ') or diffline.startswith('  '):
            count1 += 1
        if diffline.startswith('+ ') or diffline.startswith('  '):
            count2 += 1

    download_url1 = reverse('download_submission_source',
                            kwargs={
                                'contest_id': request.contest.id,
                                'submission_id': submission1_id
                            })
    download_url2 = reverse('download_submission_source',
                            kwargs={
                                'contest_id': request.contest.id,
                                'submission_id': submission2_id
                            })

    return TemplateResponse(
        request, 'programs/source_diff.html', {
            'source1':
            diff1,
            'decode_error1':
            decode_error1,
            'download_url1':
            download_url1,
            'source2':
            diff2,
            'decode_error2':
            decode_error2,
            'download_url2':
            download_url2,
            'reverse_diff_url':
            reverse('source_diff',
                    kwargs={
                        'contest_id': contest_id,
                        'submission1_id': submission2_id,
                        'submission2_id': submission1_id
                    })
        })
示例#10
0
文件: views.py 项目: matrach/oioioi
def save_diff_id_view(request, contest_id, submission_id):
    get_submission_or_404(request, contest_id, submission_id,
                          ProgramSubmission)
    request.session['saved_diff_id'] = submission_id
    return HttpResponse()
示例#11
0
文件: views.py 项目: matrach/oioioi
def download_submission_source_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       ProgramSubmission)
    return stream_file(submission.source_file)
示例#12
0
文件: views.py 项目: neeraj9/oioioi
def source_diff_view(request, contest_id, submission1_id, submission2_id):
    if request.session.get("saved_diff_id"):
        request.session.pop("saved_diff_id")
    submission1 = get_submission_or_404(request, contest_id, submission1_id, ProgramSubmission)
    submission2 = get_submission_or_404(request, contest_id, submission2_id, ProgramSubmission)
    source1 = submission1.source_file.read()
    source1, decode_error1 = decode_str(source1)
    source2 = submission2.source_file.read()
    source2, decode_error2 = decode_str(source2)
    source1 = source1.splitlines()
    source2 = source2.splitlines()

    numwidth = len(str(max(len(source1), len(source2))))
    ndiff = difflib.ndiff(source1, source2)

    class DiffLine(object):
        def __init__(self, css_class, text, number):
            self.css_class = css_class
            self.text = text
            self.number = number

    def diffstrip(line):
        return line[2:]

    def numformat(num):
        return str(num).rjust(numwidth)

    diff1, diff2 = [], []
    count1, count2 = 1, 1

    for diffline in ndiff:
        line = diffstrip(diffline)
        line = line.expandtabs(4)
        maxlen = getattr(settings, "CHARACTERS_IN_LINE", 80)
        parts = (len(line) + maxlen) / maxlen
        line = line.ljust(parts * maxlen)
        for i in xrange(parts):
            f, t = i * maxlen, ((i + 1) * maxlen)
            c1, c2 = numformat(count1), numformat(count2)
            if diffline.startswith("- "):
                diff1.append(DiffLine("left", line[f:t], "" if i else c1))
                diff2.append(DiffLine("empty", "", ""))
            elif diffline.startswith("+ "):
                diff1.append(DiffLine("empty", "", ""))
                diff2.append(DiffLine("right", line[f:t], "" if i else c2))
            elif diffline.startswith("  "):
                diff1.append(DiffLine("both", line[f:t], "" if i else c1))
                diff2.append(DiffLine("both", line[f:t], "" if i else c2))
        if diffline.startswith("- ") or diffline.startswith("  "):
            count1 += 1
        if diffline.startswith("+ ") or diffline.startswith("  "):
            count2 += 1

    download_url1 = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission1_id}
    )
    download_url2 = reverse(
        "download_submission_source", kwargs={"contest_id": request.contest.id, "submission_id": submission2_id}
    )

    return TemplateResponse(
        request,
        "programs/source_diff.html",
        {
            "source1": diff1,
            "decode_error1": decode_error1,
            "download_url1": download_url1,
            "source2": diff2,
            "decode_error2": decode_error2,
            "download_url2": download_url2,
            "reverse_diff_url": reverse(
                "source_diff",
                kwargs={"contest_id": contest_id, "submission1_id": submission2_id, "submission2_id": submission1_id},
            ),
        },
    )
示例#13
0
文件: views.py 项目: neeraj9/oioioi
def save_diff_id_view(request, contest_id, submission_id):
    get_submission_or_404(request, contest_id, submission_id, ProgramSubmission)
    request.session["saved_diff_id"] = submission_id
    return HttpResponse()
示例#14
0
文件: views.py 项目: neeraj9/oioioi
def download_submission_source_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id, ProgramSubmission)
    return stream_file(submission.source_file)
示例#15
0
文件: views.py 项目: matrach/oioioi
def download_input_file_view(request, contest_id, submission_id):
    submission = get_submission_or_404(request, contest_id, submission_id,
                                       TestRunProgramSubmission)

    return stream_file(submission.input_file, name='input.in')