This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from unb-mds/feature/contests
[feature request] Adicionar página de contest
- Loading branch information
Showing
15 changed files
with
291 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.apps import AppConfig | ||
|
||
default_app_config = "apps.contests.ContestsConfig" | ||
|
||
|
||
class ContestsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "appscontests" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from django.contrib.admin import ModelAdmin, register | ||
from django.forms import ( | ||
CharField, | ||
ModelForm, | ||
ModelMultipleChoiceField, | ||
Textarea, | ||
) | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from apps.contests.models import Contest | ||
from apps.users.models import User | ||
|
||
if TYPE_CHECKING: | ||
ContestAdminBase = ModelAdmin[Contest] | ||
ContestModelFormBase = ModelForm[Contest] | ||
else: | ||
ContestAdminBase = ModelAdmin | ||
ContestModelFormBase = ModelForm | ||
|
||
|
||
class ContestModelForm(ContestModelFormBase): | ||
description = CharField(widget=Textarea(attrs={"rows": 14, "cols": 80})) | ||
users = ModelMultipleChoiceField( | ||
queryset=User.objects.all(), required=False | ||
) | ||
|
||
class Meta: | ||
model = Contest | ||
fields = "__all__" | ||
|
||
|
||
@register(Contest) | ||
class ContestAdmin(ContestAdminBase): | ||
form = ContestModelForm | ||
|
||
list_display = ("title", "start_time", "end_time", "status") | ||
list_filter = ("start_time", "end_time") | ||
|
||
fieldsets = [ | ||
(_("General"), {"fields": ("title", "description")}), | ||
( | ||
_("Other"), | ||
{"fields": ("start_time", "end_time", "users", "cancelled")}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from enum import StrEnum | ||
|
||
|
||
class ContestStatus(StrEnum): | ||
PENDING = "Pending" | ||
RUNNING = "Running" | ||
FINISHED = "Finished" | ||
CANCELLED = "Cancelled" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 4.2.5 on 2023-09-30 05:03 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Contest", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("title", models.CharField(max_length=256)), | ||
("description", models.CharField(max_length=1024)), | ||
("start_time", models.DateTimeField()), | ||
("end_time", models.DateTimeField()), | ||
( | ||
"users", | ||
models.ManyToManyField( | ||
related_name="contests", to=settings.AUTH_USER_MODEL | ||
), | ||
), | ||
], | ||
options={ | ||
"db_table": "contests", | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.5 on 2023-09-30 05:21 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("contests", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="contest", | ||
name="cancelled", | ||
field=models.BooleanField(default=False), | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.db.models import ( | ||
BooleanField, | ||
CharField, | ||
DateTimeField, | ||
ManyToManyField, | ||
) | ||
from django.utils.timezone import now | ||
|
||
from apps.contests.enums import ContestStatus | ||
from apps.users.models import User | ||
from core.models import TimestampedModel | ||
|
||
|
||
class Contest(TimestampedModel): | ||
"""Represents a contest.""" | ||
|
||
id: int | ||
|
||
title = CharField(max_length=256) | ||
description = CharField(max_length=1024) | ||
|
||
start_time = DateTimeField() | ||
end_time = DateTimeField() | ||
cancelled = BooleanField(default=False) | ||
|
||
users = ManyToManyField(User, related_name="contests") | ||
|
||
class Meta: | ||
db_table = "contests" | ||
|
||
def __str__(self) -> str: | ||
return f"{self.title} #{self.id}" | ||
|
||
@property | ||
def status(self) -> ContestStatus: | ||
if self.cancelled: | ||
return ContestStatus.CANCELLED | ||
|
||
if self.start_time > now(): | ||
return ContestStatus.PENDING | ||
elif self.end_time < now(): | ||
return ContestStatus.FINISHED | ||
else: | ||
return ContestStatus.RUNNING |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import path | ||
|
||
from apps.contests.views import DetailView, IndexView, send | ||
|
||
app_name = "contests" | ||
|
||
urlpatterns = [ | ||
path("", IndexView.as_view(), name="index"), | ||
path("<int:pk>/", DetailView.as_view(), name="detail"), | ||
path("<int:contest_id>/send/", send, name="send"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
from io import StringIO | ||
from typing import TYPE_CHECKING | ||
|
||
from django.db.models.query import QuerySet | ||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import get_object_or_404 | ||
from django.views import generic | ||
|
||
from apps.contests.models import Contest | ||
|
||
if TYPE_CHECKING: | ||
IndexViewBase = generic.ListView[Contest] | ||
DetailViewBase = generic.DetailView[Contest] | ||
else: | ||
IndexViewBase = generic.ListView | ||
DetailViewBase = generic.DetailView | ||
|
||
|
||
class IndexView(IndexViewBase): | ||
template_name = "contests/index.html" | ||
context_object_name = "contests" | ||
|
||
def get_queryset(self) -> QuerySet[Contest]: | ||
return Contest._default_manager.order_by("-start_time")[:5] | ||
|
||
|
||
class DetailView(DetailViewBase): | ||
model = Contest | ||
template_name = "contests/detail.html" | ||
|
||
|
||
def send(request: HttpRequest, contest_id: int) -> HttpResponse: | ||
contest = get_object_or_404(Contest, pk=contest_id) | ||
code = request.POST["code"] | ||
|
||
old_stdout = sys.stdout | ||
sys.stdout = buffer = StringIO() | ||
|
||
try: | ||
eval(code) | ||
except Exception as exc: | ||
sys.stdout = old_stdout | ||
return HttpResponse(f"Contest {contest.title} failed.\n{exc}") | ||
|
||
sys.stdout = old_stdout | ||
message = buffer.getvalue() | ||
|
||
return HttpResponse( | ||
f"Contest {contest.title} ran successfully.\n{message}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}{{ contest.title }}{% endblock title %} | ||
|
||
{% block content %} | ||
<div> | ||
<h1>{{ contest }}</h1> | ||
<p>{{ contest.description }}</p> | ||
</div> | ||
|
||
<form action="{% url 'contests:send' contest.id %}" method="post"> | ||
{% csrf_token %} | ||
<fieldset> | ||
<legend> | ||
<h1>{{ question.question_text }}</h1> | ||
</legend> | ||
|
||
{% if error_message %} | ||
<p><strong>{{ error_message }}</strong></p> | ||
{% endif %} | ||
|
||
<textarea name="code" id="code" cols="50" rows="10"></textarea> | ||
</fieldset> | ||
|
||
<input type="submit" value="Vote"> | ||
</form> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %}Contests{% endblock title %} | ||
|
||
{% block content %} | ||
{% if contests %} | ||
<ul> | ||
{% for contest in contests %} | ||
<li> | ||
<a href="{% url 'contests:detail' contest.id %}"> | ||
{{ contest }} | ||
</a> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
<p>No contests are available.</p> | ||
{% endif %} | ||
{% endblock content %} |