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.
feat(apps/contests): add the initial views for the contest app
- Loading branch information
1 parent
443a5e3
commit dcea9ca
Showing
6 changed files
with
87 additions
and
2 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,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,28 @@ | ||
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 | ||
|
||
|
||
class IndexView(generic.ListView[Contest]): | ||
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(generic.DetailView[Contest]): | ||
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"] | ||
|
||
eval(code) | ||
|
||
return HttpResponse(f"Contest {contest.title} ran successfully.") |
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 %} |