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/user-task-points
Browse files Browse the repository at this point in the history
  • Loading branch information
bitterteriyaki committed Dec 5, 2023
2 parents 9a17702 + 1b19eef commit e145ffc
Show file tree
Hide file tree
Showing 15 changed files with 407 additions and 15 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ on:

env:
CLOUDAMQP_URL: "amqp://virtualjudge:virtualjudge@localhost:5672//"
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_USER: virtualjudge
POSTGRES_PASSWORD: virtualjudge
POSTGRES_DB: virtualjudge

jobs:
upload:
Expand All @@ -20,13 +25,21 @@ jobs:
RABBITMQ_DEFAULT_PASS: virtualjudge
ports:
- 5672:5672
postgresql:
image: postgres:16.0-alpine
ports:
- 5432:5432
env:
POSTGRES_USER: virtualjudge
POSTGRES_PASSWORD: virtualjudge
POSTGRES_DB: virtualjudge
strategy:
max-parallel: 4
matrix:
python-version: ["3.11"]
poetry-version: ["1.6.1"]
env:
DATABASE_URL: "sqlite:///db.sqlite3"
DATABASE_URL: "postgres://virtualjudge:virtualjudge@localhost:5432/virtualjudge"
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}

steps:
Expand Down
15 changes: 14 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ on:

env:
CLOUDAMQP_URL: "amqp://virtualjudge:virtualjudge@localhost:5672//"
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_USER: virtualjudge
POSTGRES_PASSWORD: virtualjudge
POSTGRES_DB: virtualjudge

jobs:
build:
Expand All @@ -20,13 +25,21 @@ jobs:
RABBITMQ_DEFAULT_PASS: virtualjudge
ports:
- 5672:5672
postgresql:
image: postgres:16.0-alpine
ports:
- 5432:5432
env:
POSTGRES_USER: virtualjudge
POSTGRES_PASSWORD: virtualjudge
POSTGRES_DB: virtualjudge
strategy:
max-parallel: 4
matrix:
python-version: ["3.11"]
poetry-version: ["1.6.1"]
env:
DATABASE_URL: "sqlite:///db.sqlite3"
DATABASE_URL: "postgres://virtualjudge:virtualjudge@localhost:5432/virtualjudge"
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
40 changes: 40 additions & 0 deletions apps/submissions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.admin.sites import AdminSite
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -94,3 +95,42 @@ def test_fieldsets(self) -> None:
(_("Details"), {"fields": ("author", "task", "code", "status")})
]
self.assertEqual(self.submission_admin.fieldsets, expected)


class SubmissionListViewtest(TestCase):
def setUp(self) -> None:
self.user = User.objects.create_user(
username="testuser",
email="testuser@example",
password="testpassword",
)

self.contest = Contest._default_manager.create(
title="Test Contest",
description="This is a test contest",
start_time=timezone.now(),
end_time=timezone.now() + timedelta(hours=1),
cancelled=False,
)

self.task = Task._default_manager.create(
title="Test Task",
description="This is a test task",
contest=self.contest,
)

self.submission = Submission._default_manager.create(
author=self.user,
task=self.task,
code="test code",
)

def test_submission_list_view(self) -> None:
self.client.login(email="testuser@example", password="testpassword")

url = reverse("submissions:list")
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
self.assertIn("submissions", response.context)
self.assertIn(self.submission, response.context["submissions"])
7 changes: 7 additions & 0 deletions apps/submissions/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from apps.submissions.views import SubmissionListView

app_name = "submissions"

urlpatterns = [path("", SubmissionListView.as_view(), name="list")]
21 changes: 21 additions & 0 deletions apps/submissions/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import TYPE_CHECKING

from django.db.models import QuerySet
from django.views.generic import ListView

from apps.submissions.models import Submission

if TYPE_CHECKING:
SubmissionViewBase = ListView[Submission]
else:
SubmissionViewBase = ListView


class SubmissionListView(SubmissionViewBase):
model = Submission
template_name = "submissions/list.html"
context_object_name = "submissions"
paginate_by = 10

def get_queryset(self) -> QuerySet[Submission]:
return Submission._default_manager.all().order_by("-created_at")
31 changes: 24 additions & 7 deletions apps/tasks/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from typing import TYPE_CHECKING, cast

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

Expand All @@ -19,6 +25,7 @@

class TaskModelForm(TaskModelFormBase):
description = CharField(widget=Textarea(attrs={"rows": 14, "cols": 80}))
constraints = SimpleArrayField(CharField(max_length=256), required=False)
score = IntegerField(min_value=0, required=False)

memory_limit = IntegerField(
Expand Down Expand Up @@ -48,7 +55,7 @@ class TaskAdmin(TaskAdminBase):
list_filter = ("contest", "score")

fieldsets = [
(_("General"), {"fields": ("title", "description")}),
(_("General"), {"fields": ("title", "description", "constraints")}),
(_("Meta"), {"fields": ("contest", "score")}),
(_("Limits"), {"fields": ("memory_limit", "time_limit")}),
(_("Test case"), {"fields": ("input_file", "output_file")}),
Expand All @@ -61,10 +68,20 @@ def save_model(
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"])
if change and len(request.FILES) == 0:
return super().save_model(request, obj, form, change)

try:
# 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"]
)
except KeyError:
return super().save_model(request, obj, form, change)

obj.input_file = input_file.read().decode("utf-8")
obj.output_file = output_file.read().decode("utf-8")
Expand Down
22 changes: 22 additions & 0 deletions apps/tasks/migrations/0005_task_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.7 on 2023-12-01 18:09

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tasks", "0004_task_input_file_task_output_file"),
]

operations = [
migrations.AddField(
model_name="task",
name="constraints",
field=django.contrib.postgres.fields.ArrayField(
base_field=models.CharField(max_length=256),
default=list,
size=None,
),
),
]
2 changes: 2 additions & 0 deletions apps/tasks/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.postgres.fields import ArrayField
from django.db.models import (
CASCADE,
CharField,
Expand All @@ -16,6 +17,7 @@ class Task(TimestampedModel):

title = CharField(max_length=256)
description = CharField(max_length=4096)
constraints = ArrayField(CharField(max_length=256), default=list)

contest = ForeignKey(Contest, related_name="tasks", on_delete=CASCADE)
score = IntegerField(null=True)
Expand Down
Loading

0 comments on commit e145ffc

Please sign in to comment.