This commit is contained in:
2024-11-25 19:21:44 +01:00
parent 27b8c40c1c
commit 6ad9b46543
26 changed files with 260 additions and 338 deletions

View File

@@ -1,47 +1,47 @@
from crispy_forms.helper import FormHelper
from django import forms
from django.http import Http404
from django.shortcuts import render, redirect
from django.contrib import messages
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
from django.views import View
from django.views.generic.base import TemplateResponseMixin
from ..models import Season, Candidate
from ..models import Candidate, Season
class EnterNameForm(forms.Form):
name = forms.CharField()
name = forms.CharField(label=_("Name"))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
class EnterNameView(View):
class EnterNameView(View, TemplateResponseMixin):
template_name = "quiz/enter_name.html"
forms_class = EnterNameForm
def get(self, request, season: Season, *args, **kwargs):
if season.active_quiz == None:
raise Http404("No quiz active")
messages.info(request, _("This season has no active quiz."))
return redirect("home")
return render(
request,
self.template_name,
{"form": self.forms_class(), "season": season},
)
return self.render_to_response({"form": self.forms_class()})
def post(self, request, season: Season, *args, **kwargs):
name = request.POST.get("name")
if season.preregister_candidates:
try:
candidate = Candidate.objects.get(season=season, name=name)
except Candidate.DoesNotExist:
raise Http404("Candidate not found")
else:
candidate, created = Candidate.objects.get_or_create(
season=season, name=name
)
try:
candidate = Candidate.objects.get(season=season, name__iexact=name)
except Candidate.DoesNotExist:
if season.preregister_candidates:
messages.warning(request, _("Candidate does not exist"))
return redirect(reverse("quiz", kwargs={"season": season}))
candidate = Candidate.objects.create(season=season, name=name)
return redirect(
reverse(

View File

@@ -1,15 +1,17 @@
from django.contrib import messages
from django.core.exceptions import BadRequest
from django.http import Http404, HttpRequest, HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views import View
from django.views.generic.base import TemplateResponseMixin
from ..models import Candidate, Answer, GivenAnswer
from ..models.question import NoActiveTestForSeason
from ..models import Answer, Candidate, GivenAnswer
from ..models.question import NoActiveTestForSeason, QuizAlreadyFinished
class QuestionView(View):
class QuestionView(View, TemplateResponseMixin):
template_name = "quiz/question.html"
def get(
@@ -18,14 +20,26 @@ class QuestionView(View):
try:
question = candidate.get_next_question(candidate)
except NoActiveTestForSeason:
messages.error(request, _("No active Quiz for season"))
raise Http404("No active Quiz for seaon")
messages.error(request, _("No active quiz for season"))
return redirect("home")
except QuizAlreadyFinished:
if not kwargs.get("from_post"):
messages.error(request, _("Quiz done"))
return render(
request,
"quiz/question.html",
{"candidate": candidate, "question": question},
)
return redirect(reverse("quiz", kwargs={"season": candidate.season}))
# TODO: On first question -> record time
if (
GivenAnswer.objects.filter(
candidate=candidate, quiz=candidate.season.active_quiz
).count()
== 0
):
GivenAnswer.objects.create(
candidate=candidate, quiz=question.quiz, answer=None
)
return self.render_to_response({"candidate": candidate, "question": question})
def post(self, request: HttpRequest, candidate: Candidate, *args, **kwargs):
answer_id = request.POST.get("answer")
@@ -38,7 +52,8 @@ class QuestionView(View):
raise BadRequest
GivenAnswer.objects.create(
candidate=candidate, question=answer.question, answer=answer
candidate=candidate,
quiz=answer.question.quiz,
answer=answer,
)
return self.get(request, candidate, args, kwargs)
return self.get(request, candidate, from_post=True)

View File

@@ -1,9 +1,12 @@
from crispy_forms.helper import FormHelper
from django import forms
from django.contrib import messages
from django.http import Http404
from django.shortcuts import redirect
from django.urls import reverse
from django.utils.translation import gettext as _
from django.views.generic.edit import FormView
from django import forms
from mypy.dmypy.client import request
from ..models import Season
@@ -24,6 +27,7 @@ class SelectSeasonView(FormView):
try:
season = Season.objects.get(season_code=form.cleaned_data["code"].upper())
except Season.DoesNotExist:
raise Http404("Season does not exist")
messages.warning(self.request, _("Invalid season code"))
return redirect("home")
return redirect(reverse("quiz", kwargs={"season": season}))