From 380f029399554e24429cac1d84035dfe387b727d Mon Sep 17 00:00:00 2001 From: Vladimir Velikovich Date: Fri, 13 Dec 2024 23:28:49 +0300 Subject: [PATCH] TS-82: Added user registration on contest page --- .../web/main/management/commands/init_db.py | 11 +++++- source/web/main/urls.py | 3 ++ source/web/main/views.py | 38 +++++++++++++++++-- .../web/templates/contests/registration.html | 14 +++++++ 4 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 source/web/templates/contests/registration.html diff --git a/source/web/main/management/commands/init_db.py b/source/web/main/management/commands/init_db.py index b8cbc6f..5899a75 100644 --- a/source/web/main/management/commands/init_db.py +++ b/source/web/main/management/commands/init_db.py @@ -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' @@ -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') \ No newline at end of file + Verdict.objects.get_or_create(name='Error', short_name='err') + + ContestRole.objects.get_or_create(name='Participant') \ No newline at end of file diff --git a/source/web/main/urls.py b/source/web/main/urls.py index 5593985..58c1932 100644 --- a/source/web/main/urls.py +++ b/source/web/main/urls.py @@ -1,6 +1,7 @@ from django.urls import path from django.contrib.auth.views import LogoutView from main.views import ( + ContestRegisterView, IndexView, SignUpView, TaskView, @@ -21,4 +22,6 @@ path('tasks//theta_code', code_answer, name='theta_code'), path('tasks//theta_quiz', quiz_answer, name='theta_quiz'), + + path('contest//register', ContestRegisterView.as_view(), name='contest_register'), ] diff --git a/source/web/main/views.py b/source/web/main/views.py index e2ce01a..4994299 100644 --- a/source/web/main/views.py +++ b/source/web/main/views.py @@ -1,12 +1,17 @@ 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 +from main.models import ( + Answer, + Contest, + ContestRole, + Task, + UserToContest, +) User = get_user_model() @@ -92,4 +97,31 @@ class TaskListView(TemplateView): def get(self, request, *args, **kwargs): tasks = Task.objects.exclude(contest__in=Contest.objects.active_contests()) - return render(request, self.template_name, {'tasks': tasks}) \ No newline at end of file + 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}) \ No newline at end of file diff --git a/source/web/templates/contests/registration.html b/source/web/templates/contests/registration.html new file mode 100644 index 0000000..ca2f4c9 --- /dev/null +++ b/source/web/templates/contests/registration.html @@ -0,0 +1,14 @@ +{% extends "wrapper.html" %} + +{% block title %} +Contest registration +{% endblock %} + +{% block content %} +

Contest registration

+

Rules for contest:

+
+ {% csrf_token %} + +
+{% endblock %} \ No newline at end of file