SOITA/trials/models.py
Hubert Jankowski d9ad2c2f9d test visibility
2022-04-09 13:02:21 +02:00

93 lines
2.3 KiB
Python

from django.db import models
from questions.models import Question
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"
# )
category = models.CharField(max_length=100)
visible = models.BooleanField(default=True)
created_by = models.ForeignKey(
"users.User",
null=True,
related_name="tests",
on_delete=models.CASCADE
)
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
def get_maxscore(self, answers):
"""
[
{
"question": 1,
"answer": 1
},
{
"question": 2,
"answer": 1
}
]
"""
points = 0
# for answer in answers:
# question = self.questions.get(id=answer["question"])
# points += question.points
for question in self.questions.all():
points += question.points
return points
def question_count(self):
return Question.objects.filter(test_id=self.id).count()
def name_and_passing_score(self):
return {
"name": self.name,
"passing_score": self.passing_score
}
class SolvedTest(models.Model):
score = models.IntegerField(default=0)
max = models.IntegerField(default=0)
percentage = models.IntegerField(default=0)
user = models.ForeignKey(
"users.User",
null=True,
related_name="answers",
on_delete=models.CASCADE
)
test = models.ForeignKey(
"Test",
null=True,
related_name="answers",
on_delete=models.CASCADE
)