This commit is contained in:
2024-12-11 23:22:09 +01:00
parent 4b86b33872
commit 9b7944c14d
53 changed files with 2054 additions and 249 deletions

View File

@@ -1,7 +1,10 @@
from django.db import models
from django.db.models import QuerySet
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta
from quiz.models import Answer
class NoActiveTestForSeason(Exception):
pass
@@ -21,6 +24,33 @@ class Question(models.Model):
)
enabled = models.BooleanField(default=True, verbose_name=_("enabled"))
@property
def order(self):
return self._order
@property
def right_answer(self) -> QuerySet[Answer]:
return self.answers.filter(is_right_answer=True)
@property
def has_right_answer(self) -> bool:
return self.answers.filter(is_right_answer=True).count() > 0
@property
def errors(self) -> str | None:
if self.answers.count() == 0:
return _("Error: Question has no answers")
n_correct_answers = self.answers.filter(is_right_answer=True).count()
if n_correct_answers == 0:
return _("Error: This question has no right answer!")
if n_correct_answers > 1:
return _("Warning: This question has multiple correct answers")
return None
def __str__(self) -> str:
return f"{self._order + 1}. {self.question} ({self.quiz}) ({self.answers.count()} answers, {self.answers.filter(is_right_answer=True).count()} correct)"