2021-12-05 13:50:34 +01:00
|
|
|
from django.db import models
|
2021-12-12 11:35:35 +01:00
|
|
|
from .managers import TestManager
|
2021-12-05 13:50:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Test(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2021-12-05 15:23:29 +01:00
|
|
|
passing_score = models.PositiveSmallIntegerField(default=0)
|
2022-01-08 22:15:30 +01:00
|
|
|
# category = models.ForeignKey(
|
|
|
|
# "categories.Category",
|
|
|
|
# on_delete=models.SET_NULL,
|
|
|
|
# null=True,
|
|
|
|
# related_name="tests"
|
|
|
|
# )
|
|
|
|
category = models.CharField(max_length=100)
|
2021-12-12 11:35:35 +01:00
|
|
|
|
|
|
|
objects = TestManager()
|
2021-12-05 13:50:34 +01:00
|
|
|
|
|
|
|
def get_score(self, answers):
|
|
|
|
"""
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"question": 1,
|
|
|
|
"answer": 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"question": 2,
|
|
|
|
"answer": 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
"""
|
|
|
|
points = 0
|
|
|
|
for answer in answers:
|
|
|
|
question = self.questions.get(id=answer["question"])
|
|
|
|
if question.answers.get(id=answer["answer"]).is_correct:
|
|
|
|
points += question.points
|
|
|
|
return points
|