Add output-only on UI
This commit is contained in:
parent
bdae79eeda
commit
195450ebc3
15 changed files with 708 additions and 566 deletions
|
@ -3,6 +3,7 @@ import json
|
|||
import logging
|
||||
import threading
|
||||
import time
|
||||
import os
|
||||
from collections import deque, namedtuple
|
||||
from operator import itemgetter
|
||||
|
||||
|
@ -10,6 +11,7 @@ from django import db
|
|||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.db.models import F
|
||||
from django.core.cache import cache
|
||||
|
||||
from judge import event_poster as event
|
||||
from judge.bridge.base_handler import ZlibPacketHandler, proxy_list
|
||||
|
@ -568,6 +570,13 @@ class JudgeHandler(ZlibPacketHandler):
|
|||
event.post("contest_%d" % participation.contest_id, {"type": "update"})
|
||||
self._post_update_submission(submission.id, "grading-end", done=True)
|
||||
|
||||
# Clean up submission source file (if any)
|
||||
source_file = cache.get(f"submission_source_file:{submission.id}")
|
||||
if source_file:
|
||||
filepath = os.path.join(settings.DMOJ_SUBMISSION_ROOT, source_file)
|
||||
if os.path.exists(filepath):
|
||||
os.remove(filepath)
|
||||
|
||||
def on_compile_error(self, packet):
|
||||
logger.info(
|
||||
"%s: Submission failed to compile: %s", self.name, packet["submission-id"]
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import os
|
||||
import secrets
|
||||
from operator import attrgetter
|
||||
|
||||
import pyotp
|
||||
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
|
@ -16,8 +18,9 @@ from django.forms import (
|
|||
ModelForm,
|
||||
formset_factory,
|
||||
BaseModelFormSet,
|
||||
FileField,
|
||||
)
|
||||
from django.urls import reverse_lazy
|
||||
from django.urls import reverse_lazy, reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils import timezone
|
||||
|
||||
|
@ -115,14 +118,23 @@ class ProfileForm(ModelForm):
|
|||
)
|
||||
|
||||
|
||||
def file_size_validator(file):
|
||||
limit = 10 * 1024 * 1024
|
||||
if file.size > limit:
|
||||
raise ValidationError("File too large. Size should not exceed 10MB.")
|
||||
|
||||
|
||||
class ProblemSubmitForm(ModelForm):
|
||||
source = CharField(
|
||||
max_length=65536, widget=AceWidget(theme="twilight", no_ace_media=True)
|
||||
)
|
||||
judge = ChoiceField(choices=(), widget=forms.HiddenInput(), required=False)
|
||||
source_file = FileField(required=False, validators=[file_size_validator])
|
||||
|
||||
def __init__(self, *args, judge_choices=(), **kwargs):
|
||||
def __init__(self, *args, judge_choices=(), request=None, **kwargs):
|
||||
super(ProblemSubmitForm, self).__init__(*args, **kwargs)
|
||||
self.source_file_name = None
|
||||
self.request = request
|
||||
self.fields["language"].empty_label = None
|
||||
self.fields["language"].label_from_instance = attrgetter("display_name")
|
||||
self.fields["language"].queryset = Language.objects.filter(
|
||||
|
@ -135,6 +147,24 @@ class ProblemSubmitForm(ModelForm):
|
|||
)
|
||||
self.fields["judge"].choices = judge_choices
|
||||
|
||||
def clean(self):
|
||||
if "source_file" in self.files:
|
||||
if self.cleaned_data["language"].key == "OUTPUT" and self.files[
|
||||
"source_file"
|
||||
].name.endswith(".zip"):
|
||||
self.source_file_name = secrets.token_hex(16) + ".zip"
|
||||
filepath = os.path.join(
|
||||
settings.DMOJ_SUBMISSION_ROOT, self.source_file_name
|
||||
)
|
||||
with open(filepath, "wb+") as destination:
|
||||
for chunk in self.files["source_file"].chunks():
|
||||
destination.write(chunk)
|
||||
self.cleaned_data["source"] = self.request.build_absolute_uri(
|
||||
reverse("submission_source_file", args=(self.source_file_name,))
|
||||
)
|
||||
del self.files["source_file"]
|
||||
return self.cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = Submission
|
||||
fields = ["language"]
|
||||
|
|
22
judge/migrations/0155_output_only.py
Normal file
22
judge/migrations/0155_output_only.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 3.2.18 on 2023-03-10 04:24
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("judge", "0154_add_submission_indexes"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="problemdata",
|
||||
name="output_only",
|
||||
field=models.BooleanField(
|
||||
help_text="Support output-only problem",
|
||||
null=True,
|
||||
verbose_name="is output only",
|
||||
),
|
||||
),
|
||||
]
|
|
@ -118,6 +118,11 @@ class ProblemData(models.Model):
|
|||
null=True,
|
||||
help_text=_("Leave empty for stdout"),
|
||||
)
|
||||
output_only = models.BooleanField(
|
||||
verbose_name=_("is output only"),
|
||||
help_text=_("Support output-only problem"),
|
||||
null=True,
|
||||
)
|
||||
|
||||
__original_zipfile = None
|
||||
|
||||
|
|
|
@ -254,6 +254,8 @@ class ProblemDataCompiler(object):
|
|||
if "file_io" not in init:
|
||||
init["file_io"] = {}
|
||||
init["file_io"]["output"] = self.data.fileio_output
|
||||
if self.data.output_only:
|
||||
init["output_only"] = True
|
||||
|
||||
return init
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ from random import randrange
|
|||
import random
|
||||
from copy import deepcopy
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
|
@ -486,7 +487,7 @@ class ProblemList(QueryStringSortMixin, TitleMixin, SolvedProblemMixin, ListView
|
|||
orphans=orphans,
|
||||
allow_empty_first_page=allow_empty_first_page,
|
||||
count=queryset.values("pk").count() if not self.in_contest else None,
|
||||
**kwargs
|
||||
**kwargs,
|
||||
)
|
||||
if not self.in_contest:
|
||||
queryset = queryset.add_i18n_name(self.request.LANGUAGE_CODE)
|
||||
|
@ -1039,8 +1040,10 @@ def problem_submit(request, problem, submission=None):
|
|||
if request.method == "POST":
|
||||
form = ProblemSubmitForm(
|
||||
request.POST,
|
||||
request.FILES,
|
||||
judge_choices=judge_choices,
|
||||
instance=Submission(user=profile, problem=problem),
|
||||
request=request,
|
||||
)
|
||||
if form.is_valid():
|
||||
if (
|
||||
|
@ -1114,6 +1117,7 @@ def problem_submit(request, problem, submission=None):
|
|||
|
||||
# Save a query
|
||||
model.source = source
|
||||
cache.set(f"submission_source_file:{model.id}", form.source_file_name, 3600)
|
||||
model.judge(rejudge=False, judge_id=form.cleaned_data["judge"])
|
||||
|
||||
return HttpResponseRedirect(
|
||||
|
|
|
@ -27,6 +27,7 @@ from django.forms import (
|
|||
formset_factory,
|
||||
FileInput,
|
||||
TextInput,
|
||||
CheckboxInput,
|
||||
)
|
||||
from django.http import Http404, HttpResponse, HttpResponseRedirect, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
|
@ -91,6 +92,7 @@ class ProblemDataForm(ModelForm):
|
|||
"interactive_judge",
|
||||
"fileio_input",
|
||||
"fileio_output",
|
||||
"output_only",
|
||||
]
|
||||
widgets = {
|
||||
"zipfile": FineUploadFileInput,
|
||||
|
@ -100,6 +102,7 @@ class ProblemDataForm(ModelForm):
|
|||
"output_prefix": HiddenInput,
|
||||
"fileio_input": TextInput,
|
||||
"fileio_output": TextInput,
|
||||
"output_only": CheckboxInput,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ from django.utils.translation import gettext_lazy
|
|||
from django.views.decorators.http import require_POST
|
||||
from django.views.generic import DetailView
|
||||
from django.views.generic import ListView
|
||||
from django.views import View
|
||||
|
||||
from judge import event_poster as event
|
||||
from judge.highlight_code import highlight_code
|
||||
|
@ -1049,3 +1050,16 @@ class UserContestSubmissionsAjax(UserContestSubmissions):
|
|||
return super(UserContestSubmissionsAjax, self).get(request, *args, **kwargs)
|
||||
except Http404:
|
||||
return HttpResponse(_("You don't have permission to access."))
|
||||
|
||||
|
||||
class SubmissionSourceFileView(View):
|
||||
def get(self, request, filename):
|
||||
filepath = os.path.join(settings.DMOJ_SUBMISSION_ROOT, filename)
|
||||
if not os.path.exists(filepath):
|
||||
raise Http404("File not found")
|
||||
response = HttpResponse()
|
||||
with open(filepath, "rb") as f:
|
||||
response.content = f.read()
|
||||
response["Content-Type"] = "application/octet-stream"
|
||||
response["Content-Disposition"] = "attachment; filename=%s" % (filename,)
|
||||
return response
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue