SOITA/trials/models.py
Hubert Jankowski 71cd22bbbb add tournaments
2022-05-24 21:01:45 +02:00

118 lines
3.2 KiB
Python

from django.db import models
from questions.models import Question
from users.models import User
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
)
password = models.CharField(max_length=100, default="")
completions = models.IntegerField(default=0)
total_percentage_scored_by_users = models.IntegerField(default=0)
total_rating = models.IntegerField(default=0)
avg_rating = models.IntegerField(default=0)
rates_amount = models.IntegerField(default=0)
difficulty_label = models.IntegerField(default=0)
avg_difficulty = models.FloatField(default=0.0)
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):
"""
[
{
"question": 1,
"answer": 1
},
{
"question": 2,
"answer": 1
}
]
"""
points = 0
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 get_author_name(self):
if self.created_by:
user = User.objects.get(email=self.created_by)
user_fullname = user.first_name + " " + user.last_name
else:
user_fullname = "SOITA"
return user_fullname
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
)
class Tournament(models.Model):
name = models.CharField(max_length=100)
created_by = models.ForeignKey(
"users.User",
null=True,
related_name="tournaments",
on_delete=models.CASCADE
)
password = models.CharField(max_length=100)