Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file submission to problem page #1933

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions judge/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.db.models import Q
from django.forms import BooleanField, CharField, ChoiceField, Form, ModelForm, MultipleChoiceField
from django.forms import BooleanField, CharField, ChoiceField, Form, ModelForm, MultipleChoiceField, FileField
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -124,7 +124,8 @@ def clean_submission_result(self):


class ProblemSubmitForm(ModelForm):
source = CharField(max_length=65536, widget=AceWidget(theme='twilight', no_ace_media=True))
source = CharField(max_length=65536, widget=AceWidget(theme='twilight', no_ace_media=True), required=False)
file = FileField(required=False, widget=forms.FileInput)
judge = ChoiceField(choices=(), widget=forms.HiddenInput(), required=False)

def __init__(self, *args, judge_choices=(), **kwargs):
Expand All @@ -139,11 +140,21 @@ def __init__(self, *args, judge_choices=(), **kwargs):
)
self.fields['judge'].choices = judge_choices

def clean(self):
cleaned_data = super(ProblemSubmitForm, self).clean()
file = cleaned_data['file']
if file is not None:
with file.open(mode='rb') as source_file:
source = ''.join(map(lambda b: b.decode('utf-8'), source_file.readlines()))
if len(source) >= 65536: raise ValidationError(_('Source file is too long.'))
cleaned_data['source'] = source
elif cleaned_data['source'] is None: raise ValidationError(_('Source and file are both empty.'))
return cleaned_data

class Meta:
model = Submission
fields = ['language']


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: don't delete newline.

class EditOrganizationForm(ModelForm):
class Meta:
model = Organization
Expand Down
6 changes: 6 additions & 0 deletions judge/views/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class ProblemDetail(ProblemMixin, SolvedProblemMixin, CommentedDetailView):
def get_comment_page(self):
return 'p:%s' % self.object.code

@cached_property
def default_language(self):
return self.request.profile.language

def get_context_data(self, **kwargs):
context = super(ProblemDetail, self).get_context_data(**kwargs)
user = self.request.user
Expand All @@ -168,11 +172,13 @@ def get_context_data(self, **kwargs):
context['has_pdf_render'] = HAS_PDF
context['completed_problem_ids'] = self.get_completed_problems()
context['attempted_problems'] = self.get_attempted_problems()
context['form'] = ProblemSubmitForm()

can_edit = self.object.is_editable_by(user)
context['can_edit_problem'] = can_edit
if user.is_authenticated:
tickets = self.object.tickets
context['default_lang'] = self.default_language
if not can_edit:
tickets = tickets.filter(own_ticket_filter(user.profile.id))
context['has_tickets'] = tickets.exists()
Expand Down
42 changes: 38 additions & 4 deletions templates/problem/problem.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{% extends "common-content.html" %}
{% block js_media %}
{{ form.media.js }}
{% endblock %}
{% block content_media %}
{% include "comments/media-css.html" %}
{{ form.media.css }}
<style>
.title-state {
font-size: 2em;
Expand Down Expand Up @@ -34,13 +38,17 @@
min-width: 12.5em;
}

#problem-types, #allowed-langs, #available-judges {
#problem-types, #allowed-langs, #available-judges, #file-upload {
padding-top: 1em;
}

.problem-info-entry {
padding-top: 0.5em;
}

#file-upload-form > div {
margin-top: 5px;
}
</style>
{% endblock %}

Expand Down Expand Up @@ -98,7 +106,7 @@ <h2 style="color:#393630; display: inline-block">{{ title }}</h2>
{% if request.user.is_authenticated and request.in_contest and submission_limit %}
{% if submissions_left > 0 %}
<a href="{{ url('problem_submit', problem.code) }}" class="unselectable button full">
{{ _('Submit solution') }}
{{ _('Go to editor') }}
</a>
<div class="submissions-left">
{% trans trimmed counter=submissions_left %}
Expand All @@ -108,14 +116,40 @@ <h2 style="color:#393630; display: inline-block">{{ title }}</h2>
{% endtrans %}
</div>
{% else %}
<a class="unselectable button full disabled">{{ _('Submit solution') }}</a>
<a class="unselectable button full disabled">{{ _('Go to editor') }}</a>
<div class="no-submissions-left submissions-left">{{ _('0 submissions left') }}</div>
{% endif %}
{% else %}
<a href="{{ url('problem_submit', problem.code) }}" class="unselectable button full">
{{ _('Submit solution') }}
{{ _('Go to editor') }}
</a>
{% endif %}
{% if request.user.is_authenticated and (not request.in_contest or submission_limit) %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please use spaces instead of tabs.

<form id="file-upload" method="post" action="{{ url('problem_submit', problem.code) }}" enctype="multipart/form-data">
{% csrf_token %}
{{ form.non_field_errors() }}

<div class="toggle closed unselectable">
<i class="fa fa-chevron-right fa-fw"></i>Upload a file
</div>
<div style="display:none" class="toggled" id="file-upload-form">
<div>
{{ form.file }}
</div>
<div id="language-select">
<select id="id_language" name="language" class="full">
{% for lang in form.fields.language.queryset %}
<option value="{{ lang.id }}" {% if request.user.is_authenticated and lang.id == default_lang.id %}selected{% endif %}>{{ lang.name }}</option>
{% endfor %}
</select>
</div>
<div>
<input value="{{ _('Submit!') }}" type="submit" id="submit-file-button" class="unselectable button full"
{% if request.in_contest and submission_limit and not submissions_left %}disabled{% endif %} />
</div>
</div>
</form>
{% endif %}

<hr style="padding-bottom: 0.3em">

Expand Down