SOITA/trials/models.py

234 lines
6.2 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
from users.models import User
2022-05-25 00:06:44 +02:00
from .managers import TestManager, TournamentManager
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-04-09 13:02:21 +02:00
visible = models.BooleanField(default=True)
2022-01-22 19:58:42 +01:00
created_by = models.ForeignKey(
"users.User",
null=True,
related_name="tests",
on_delete=models.CASCADE
)
2022-04-10 15:05:07 +02:00
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)
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
def get_maxscore(self):
2022-01-23 17:10:05 +01:00
"""
[
{
"question": 1,
"answer": 1
},
{
"question": 2,
"answer": 1
}
]
"""
points = 0
2022-01-25 16:53:48 +01:00
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 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)
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
2022-05-24 21:01:45 +02:00
)
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
)
2022-05-25 00:06:44 +02:00
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
2022-06-05 10:27:49 +02:00
)
class TournamentClassification(models.Model):
user = models.ForeignKey(
"users.User",
null=True,
related_name="tournament_classification",
on_delete=models.CASCADE
)
tournament = models.ForeignKey(
"Tournament",
null=True,
related_name="tournament_classification",
on_delete=models.CASCADE
)
score = models.IntegerField()
date = models.DateField(auto_now_add=True)