SOITA/trials/models.py
Hubert Jankowski 7c18f0920a WIP
2021-12-12 11:35:35 +01:00

36 lines
884 B
Python

from django.db import models
from .managers import TestManager
class Test(models.Model):
name = models.CharField(max_length=100)
passing_score = models.PositiveSmallIntegerField(default=0)
category = models.ForeignKey(
"categories.Category",
on_delete=models.SET_NULL,
null=True,
related_name="tests"
)
objects = TestManager()
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