SOITA/trials/models.py
2022-06-04 14:14:59 +02:00

213 lines
5.7 KiB
Python

from django.db import models
from questions.models import Question
from users.models import User
from .managers import TestManager, TournamentManager
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, default="")
passing_score = models.PositiveSmallIntegerField(default=0)
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 = TournamentManager()
def get_score(self, answers):
"""
[
{
"question": 1,
"answer": 1
},
{
"question": 2,
"answer": 1
}
]
"""
points = 0
for answer in answers:
question = Question.objects.filter(tournament=self.id).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 Question.objects.filter(tournament=self.id):
points += question.points
return points
def question_count(self):
return Question.objects.filter(tournament=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 RateTest(models.Model):
user = models.ForeignKey(
"users.User",
null=True,
related_name="rate_tests",
on_delete=models.CASCADE
)
test = models.ForeignKey(
"Test",
null=True,
related_name="rate_tests",
on_delete=models.CASCADE
)
class RateTournament(models.Model):
user = models.ForeignKey(
"users.User",
null=True,
related_name="rate_tournaments",
on_delete=models.CASCADE
)
tournament = models.ForeignKey(
"Tournament",
null=True,
related_name="rate_tournaments",
on_delete=models.CASCADE
)