SOITA/trials/models.py

92 lines
2.3 KiB
Python
Raw Normal View History

2021-12-05 13:50:34 +01:00
from django.db import models
2022-01-30 15:20:55 +01:00
from questions.models import Question
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)
2022-01-22 19:58:42 +01:00
created_by = models.ForeignKey(
"users.User",
null=True,
related_name="tests",
on_delete=models.CASCADE
)
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
2022-01-23 17:10:05 +01:00
def get_maxscore(self, answers):
"""
[
{
"question": 1,
"answer": 1
},
{
"question": 2,
"answer": 1
}
]
"""
points = 0
2022-01-25 16:53:48 +01:00
# for answer in answers:
# question = self.questions.get(id=answer["question"])
# points += question.points
for question in self.questions.all():
2022-01-23 17:10:05 +01:00
points += question.points
return points
2022-01-30 15:20:55 +01:00
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)
2022-01-23 17:10:05 +01:00
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
)