Skip to content

Commit

Permalink
Merge branch 'dev' into fearure/TS-81-Contest-view-page
Browse files Browse the repository at this point in the history
  • Loading branch information
FeshDr authored Dec 14, 2024
2 parents f62f774 + 557b554 commit a40b61e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
11 changes: 9 additions & 2 deletions source/web/main/management/commands/init_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.core.management.base import BaseCommand

from main.models import Compiler, TaskType, Verdict
from main.models import (
Compiler,
ContestRole,
TaskType,
Verdict,
)

class Command(BaseCommand):
help = 'Initialize models'
Expand All @@ -19,4 +24,6 @@ def handle(self, *args, **options):
Verdict.objects.get_or_create(name='Fail', short_name='fail')
Verdict.objects.get_or_create(name='Presentation error', short_name='pe')
Verdict.objects.get_or_create(name='Compilation error', short_name='ce')
Verdict.objects.get_or_create(name='Error', short_name='err')
Verdict.objects.get_or_create(name='Error', short_name='err')

ContestRole.objects.get_or_create(name='Participant')
2 changes: 2 additions & 0 deletions source/web/main/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path
from django.contrib.auth.views import LogoutView
from main.views import (
ContestRegisterView,
IndexView,
SignUpView,
TaskView,
Expand All @@ -26,4 +27,5 @@

path('contest/<int:id>/', ContestDetailView.as_view(), name='contest_detail'),
path('contest/<int:id>/<int:task_order>', ContestTaskView.as_view(), name='contest_detail_task'),
path('contest/<int:id>/register', ContestRegisterView.as_view(), name='contest_register'),
]
40 changes: 37 additions & 3 deletions source/web/main/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from django.urls import reverse_lazy
from django.views.generic.base import TemplateView
from django.utils import timezone
from django.shortcuts import get_object_or_404, redirect, render
from django.contrib.auth import get_user_model
from django.contrib.auth.views import LoginView

from main.forms import LoginForm, SignUpForm
from main.models import Answer, Contest, Task, TaskOnContest
from main.models import (
Answer,
Contest,
ContestRole,
Task,
UserToContest,
TaskOnContest,
)


User = get_user_model()
Expand Down Expand Up @@ -104,6 +110,33 @@ def get(self, request, *args, **kwargs):
return render(request, self.template_name, {'tasks': tasks})


class ContestRegisterView(TemplateView):
template_name = 'contests/registration.html'

def get_contest(self, contest_id):
return get_object_or_404(Contest, pk=contest_id)

def get(self, request, *args, **kwargs):
contest = self.get_contest(kwargs['id'])
if contest not in Contest.objects.opened_contests():
return redirect('index')
if request.user.profile in contest.users.all():
return redirect('contest_detail', kwargs['id'])
return render(request, self.template_name, {'contest': self.get_contest(kwargs['id'])})

def post(self, request, *args, **kwargs):
contest = self.get_contest(kwargs['id'])
if contest in Contest.objects.opened_contests() and\
request.user.profile not in contest.users.all():
UserToContest.objects.create(
contest=contest,
user=request.user.profile,
role=ContestRole.objects.get(name="Participant")
)
return redirect('contest_detail', kwargs['id'])
return render(request, self.template_name, {'contest': contest})


class ContestDetailView(TemplateView):
template_name = 'contests/contest_detail.html'

Expand Down Expand Up @@ -144,4 +177,5 @@ def get(self, request, *args, **kwargs):
'task': task,
'contest': contest,
}
)
)

14 changes: 14 additions & 0 deletions source/web/templates/contests/registration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% extends "wrapper.html" %}

{% block title %}
Contest registration
{% endblock %}

{% block content %}
<h1>Contest registration</h1>
<p>Rules for contest:</p>
<form method="POST">
{% csrf_token %}
<button type="submit">Register</button>
</form>
{% endblock %}

0 comments on commit a40b61e

Please sign in to comment.