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

Commit

Permalink
feat(apps/contests): add the initial views for the contest app
Browse files Browse the repository at this point in the history
  • Loading branch information
bitterteriyaki committed Sep 30, 2023
1 parent 443a5e3 commit dcea9ca
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
11 changes: 11 additions & 0 deletions apps/contests/urls.py
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"),
]
28 changes: 28 additions & 0 deletions apps/contests/views.py
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.")
2 changes: 1 addition & 1 deletion server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
path("", include("django.contrib.auth.urls")),
# Local views
path("", home_view, name="home"),
path("register/", register_view, name="register"),
path("contests/", include("apps.contests.urls")),
]
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</li>

<li class="nav-item">
<a class="nav-link text-white" href="#">
<a class="nav-link text-white" href="{% url 'contests:index' %}">
Contests
</a>
</li>
Expand Down
27 changes: 27 additions & 0 deletions templates/contests/detail.html
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 %}
19 changes: 19 additions & 0 deletions templates/contests/index.html
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 %}

0 comments on commit dcea9ca

Please sign in to comment.