Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into feature/submission-page
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizaMaluf committed Nov 29, 2023
2 parents 08b44f9 + a5cd539 commit 24e1e63
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 25 deletions.
4 changes: 3 additions & 1 deletion apps/submissions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ class SubmissionAdmin(SubmissionAdminBase):
list_display = ("__str__", "author", "task")
list_filter = ("author", "task", "created_at")

fieldsets = [(_("Details"), {"fields": ("author", "task", "code")})]
fieldsets = [
(_("Details"), {"fields": ("author", "task", "code", "status")})
]
34 changes: 34 additions & 0 deletions apps/submissions/migrations/0003_submission_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2.7 on 2023-11-26 22:26

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("submissions", "0002_alter_submission_code"),
]

operations = [
migrations.AddField(
model_name="submission",
name="status",
field=models.TextField(
choices=[
("WJ", "Waiting judge"),
("JG", "Judging"),
("AC", "Accepted"),
("WA", "Wrong answer"),
("RE", "Runtime error"),
("TLE", "Time limit exceeded"),
("MLE", "Memory limit exceeded"),
("CE", "Compilation error"),
("IE", "Internal error"),
("UE", "Unknown error"),
("SE", "Submission error"),
("PE", "Presentation error"),
],
default="WJ",
max_length=3,
),
),
]
25 changes: 24 additions & 1 deletion apps/submissions/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
from django.core.validators import MinLengthValidator
from django.db.models import CASCADE, ForeignKey, TextField
from django.db.models import CASCADE, ForeignKey, TextChoices, TextField
from django.utils.translation import gettext_lazy as _

from apps.tasks.models import Task
from apps.users.models import User
from core.models import TimestampedModel


class SubmissionStatus(TextChoices):
"""Represents the status of a submission."""

WAITING_JUDGE = ("WJ", _("Waiting judge"))
JUDGING = ("JG", _("Judging"))
ACCEPTED = ("AC", _("Accepted"))
WRONG_ANSWER = ("WA", _("Wrong answer"))
RUNTIME_ERROR = ("RE", _("Runtime error"))
TIME_LIMIT_EXCEEDED = ("TLE", _("Time limit exceeded"))
MEMORY_LIMIT_EXCEEDED = ("MLE", _("Memory limit exceeded"))
COMPILATION_ERROR = ("CE", _("Compilation error"))
INTERNAL_ERROR = ("IE", _("Internal error"))
UNKNOWN_ERROR = ("UE", _("Unknown error"))
SUBMISSION_ERROR = ("SE", _("Submission error"))
PRESENTATION_ERROR = ("PE", _("Presentation error"))


class Submission(TimestampedModel):
"""
Represents a submission to a task by an user. The code field is
Expand All @@ -16,6 +34,11 @@ class Submission(TimestampedModel):
author = ForeignKey(User, related_name="submissions", on_delete=CASCADE)
task = ForeignKey(Task, related_name="submissions", on_delete=CASCADE)
code = TextField(validators=[MinLengthValidator(15)])
status = TextField(
max_length=3,
choices=SubmissionStatus.choices,
default=SubmissionStatus.WAITING_JUDGE,
)

class Meta:
db_table = "submissions"
Expand Down
4 changes: 3 additions & 1 deletion apps/submissions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,7 @@ def test_list_filter(self) -> None:
self.assertEqual(self.submission_admin.list_filter, expected)

def test_fieldsets(self) -> None:
expected = [(_("Details"), {"fields": ("author", "task", "code")})]
expected = [
(_("Details"), {"fields": ("author", "task", "code", "status")})
]
self.assertEqual(self.submission_admin.fieldsets, expected)
34 changes: 31 additions & 3 deletions apps/tasks/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

from django.contrib.admin import ModelAdmin, register
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.forms import CharField, IntegerField, ModelForm, Textarea
from django.forms.fields import FileField
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _

from apps.tasks.models import Task
Expand All @@ -19,12 +22,19 @@ class TaskModelForm(TaskModelFormBase):
score = IntegerField(min_value=0, required=False)

memory_limit = IntegerField(
min_value=0, required=False, help_text=_("In bytes.")
min_value=0,
required=False,
help_text=_("In bytes."),
)
time_limit = IntegerField(
min_value=0, required=False, help_text=_("In seconds.")
min_value=0,
required=False,
help_text=_("In seconds."),
)

input_file = FileField()
output_file = FileField()

class Meta:
model = Task
fields = "__all__"
Expand All @@ -41,4 +51,22 @@ class TaskAdmin(TaskAdminBase):
(_("General"), {"fields": ("title", "description")}),
(_("Meta"), {"fields": ("contest", "score")}),
(_("Limits"), {"fields": ("memory_limit", "time_limit")}),
(_("Test case"), {"fields": ("input_file", "output_file")}),
]

def save_model(
self,
request: HttpRequest,
obj: Task,
form: TaskModelForm,
change: bool,
) -> None:
# request.FILES does not cast to the correct type so we need to
# cast it manually, otherwise Mypy will complain.
input_file = cast(InMemoryUploadedFile, request.FILES["input_file"])
output_file = cast(InMemoryUploadedFile, request.FILES["output_file"])

obj.input_file = input_file.read().decode("utf-8")
obj.output_file = output_file.read().decode("utf-8")

return super().save_model(request, obj, form, change)
22 changes: 22 additions & 0 deletions apps/tasks/migrations/0004_task_input_file_task_output_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.7 on 2023-11-26 21:58

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tasks", "0003_task_memory_limit_task_score_and_more"),
]

operations = [
migrations.AddField(
model_name="task",
name="input_file",
field=models.TextField(),
),
migrations.AddField(
model_name="task",
name="output_file",
field=models.TextField(),
),
]
11 changes: 10 additions & 1 deletion apps/tasks/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from django.db.models import CASCADE, CharField, ForeignKey, IntegerField
from django.db.models import (
CASCADE,
CharField,
ForeignKey,
IntegerField,
TextField,
)

from apps.contests.enums import ContestStatus
from apps.contests.models import Contest
Expand All @@ -17,6 +23,9 @@ class Task(TimestampedModel):
memory_limit = IntegerField(null=True)
time_limit = IntegerField(null=True)

input_file = TextField()
output_file = TextField()

class Meta:
db_table = "tasks"

Expand Down
Loading

0 comments on commit 24e1e63

Please sign in to comment.