1
from django.db import models
2
from django.contrib.auth.models import AbstractUser
3
from django.utils.translation import gettext_lazy as _
4
from django.core.validators import MaxValueValidator, MinValueValidator
5
from games.models import Lobby, Game
6
from questions.models import Question
8
NULLABLE = {"blank": True, "null": True}
11
class AuthUser(AbstractUser):
16
('silver', 'Серебро'),
18
('platinum', 'Платина'),
21
('grandmaster', 'Грандмастер'),
22
('challenger', 'Претендент')
25
nickname = models.CharField(max_length=16, verbose_name='Ник')
26
telegram_id = models.PositiveIntegerField(verbose_name='ID Телеграм', **NULLABLE)
27
email = models.EmailField(verbose_name='email', **NULLABLE)
28
birthdate = models.DateField(verbose_name='Дата рождения', null=True)
29
is_moderator = models.BooleanField(default=False, verbose_name='Модератор')
30
level = models.PositiveSmallIntegerField(default=1, verbose_name='Уровень')
31
current_experience = models.PositiveSmallIntegerField(default=0, verbose_name='Опыт текущего уровня')
32
rank = models.CharField(choices=RANKS, default=RANKS[0], max_length=16, verbose_name='Ранг')
33
division = models.PositiveSmallIntegerField(default=4, verbose_name='Дивизион', validators=[
37
current_lp = models.PositiveSmallIntegerField(default=0, verbose_name='Текущие ранговые очки')
38
friends = models.ManyToManyField('self', verbose_name='Друзья')
39
current_lobby = models.ForeignKey(Lobby, related_name='players', on_delete=models.SET_NULL, **NULLABLE)
40
is_lobby_leader = models.BooleanField(default=False)
41
current_game = models.ForeignKey(Game, on_delete=models.SET_NULL, null=True, blank=True)
42
avatar = models.ImageField(upload_to="users", verbose_name=_("Аватар"), **NULLABLE)
45
def get_friends(self):
46
return AuthUser.objects.filter(pk__in=self.friends.values_list('pk'))
49
class QuestionRatedByUser(models.Model):
51
Модель для связи вопроса с пользователем.
52
Каждая запись - факт оценки вопроса пользователем.
54
question = models.ForeignKey(Question, on_delete=models.CASCADE, verbose_name='Вопрос')
55
user = models.ForeignKey(AuthUser, on_delete=models.CASCADE, verbose_name='Пользователь')
58
unique_together = ('question', 'user',)
61
class Remark(models.Model):
62
question = models.ForeignKey(Question, on_delete=models.CASCADE, verbose_name='Вопрос')
63
text = models.CharField(max_length=128, verbose_name='Замечание')
64
author = models.ForeignKey(AuthUser, on_delete=models.CASCADE, verbose_name='Пользователь')
65
rating = models.IntegerField(default=0, verbose_name='Очки')
68
unique_together = ('question', 'author',)
71
return f'#{self.text} {self.rating}'