models.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from django.db import models
  2. from django.core.validators import FileExtensionValidator
  3. from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
  4. class Genre(models.Model):
  5. title = models.CharField(verbose_name="Название", max_length=50)
  6. class Meta:
  7. verbose_name = "Жанр"
  8. verbose_name_plural = "Жанры"
  9. def __str__(self):
  10. return self.title
  11. class Author(models.Model):
  12. name = models.CharField(max_length=100, unique=True, verbose_name="ФИО")
  13. avatar = models.ImageField(verbose_name='Аватар автора', upload_to='images/author/avatars/%Y/%m/%d/',
  14. default='images/author/avatars/default.jpeg', blank=True,
  15. validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])])
  16. class Meta:
  17. verbose_name = "Автор"
  18. verbose_name_plural = "Авторы"
  19. def __str__(self):
  20. return self.name
  21. class Collection(models.Model):
  22. title = models.CharField(max_length=100, unique=True, verbose_name="Название")
  23. class Meta:
  24. verbose_name = "Коллекция"
  25. verbose_name_plural = "Коллекции"
  26. def __str__(self):
  27. return self.title
  28. class Genders(models.Model):
  29. title = models.CharField(max_length=15, unique=True, verbose_name="Название")
  30. class Meta:
  31. verbose_name = "Пол"
  32. verbose_name_plural = "Полы"
  33. def __str__(self):
  34. return self.title
  35. class UserManager(BaseUserManager):
  36. use_in_migrations = True
  37. def create_user(self, email, name, password):
  38. user = self.model(
  39. email=self.normalize_email(email),
  40. name=name,
  41. )
  42. user.set_password(password)
  43. user.save(using=self._db)
  44. return user
  45. def create_staffuser(self, email, name, password):
  46. user = self.create_user(
  47. email,
  48. password=password,
  49. name=name,
  50. )
  51. user.is_staff = True
  52. user.save(using=self._db)
  53. return user
  54. def create_superuser(self, email, name, password):
  55. user = self.create_user(
  56. email,
  57. password=password,
  58. name=name,
  59. )
  60. user.is_staff = True
  61. user.admin = True
  62. user.save(using=self._db)
  63. return user
  64. class User(AbstractBaseUser, PermissionsMixin):
  65. username = None
  66. email = models.EmailField(max_length=50, unique=True)
  67. name = models.CharField(max_length=30)
  68. birthdate = models.DateField(null=True)
  69. avatar = models.ImageField(verbose_name='Аватар пользователя', upload_to='images/user/avatars/%Y/%m/%d/',
  70. default='images/user/avatars/default.jpeg', blank=True,
  71. validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])])
  72. is_staff = models.BooleanField(default=False)
  73. admin = models.BooleanField(default=False)
  74. USERNAME_FIELD = "email"
  75. REQUIRED_FIELDS = ['name']
  76. objects = UserManager()
  77. def __str__(self):
  78. return self.email
  79. class Action(models.Model):
  80. title = models.CharField(max_length=50, unique=True, verbose_name="Название")
  81. weight = models.FloatField(null=True, verbose_name="Вес")
  82. users = models.ManyToManyField(User)
  83. class Meta:
  84. verbose_name = "Действие"
  85. verbose_name_plural = "Действия"
  86. def __str__(self):
  87. return self.title
  88. class Metric(models.Model):
  89. user = models.ForeignKey(User, on_delete=models.CASCADE)
  90. date = models.DateTimeField(auto_now_add=True, verbose_name="Дата")
  91. pages_read = models.IntegerField(null=True, verbose_name="Страниц прочитано")
  92. reading_time = models.TimeField(null=True, verbose_name="Время чтения")
  93. books_read = models.IntegerField(null=True, verbose_name="Книг прочитано")
  94. class Meta():
  95. verbose_name = "Метрика"
  96. verbose_name_plural = "Метрики"
  97. def __str__(self):
  98. return f'{self.date} {self.user}'
  99. class Book(models.Model):
  100. title = models.CharField(max_length=50, unique=True, verbose_name="Название")
  101. annotation = models.TextField(verbose_name="Аннотация")
  102. symbols_count = models.IntegerField(null = True, verbose_name="Количество символов")
  103. book_file = models.FileField(verbose_name='Файл книги', upload_to='books/covers/', null=True,
  104. validators=[FileExtensionValidator(allowed_extensions=['epub', 'fb2', 'pdf', 'docx'])])
  105. pages_count = models.IntegerField(null=True,verbose_name="Количество страниц")
  106. is_recognized = models.BooleanField(default=False, verbose_name="Распознана")
  107. last_update = models.DateTimeField(auto_now=True, verbose_name="Последнее обновление")
  108. cover = models.FileField(verbose_name='Обложка книги', upload_to='images/covers/', blank=True,
  109. validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])])
  110. is_private = models.BooleanField(default=False, verbose_name="Личная")
  111. authors = models.ManyToManyField(Author)
  112. genres = models.ManyToManyField(Genre)
  113. class Meta:
  114. verbose_name = "Книга"
  115. verbose_name_plural = "Книги"
  116. def __str__(self):
  117. return self.title
  118. class UsersBooksCollections(models.Model):
  119. user = models.ForeignKey(User, on_delete=models.CASCADE)
  120. book = models.ForeignKey(Book, on_delete=models.CASCADE)
  121. collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
  122. current_page = models.IntegerField(default=0, verbose_name="Текущая страница")
  123. date_added = models.DateTimeField(auto_now=True, verbose_name="Дата добавления")
  124. class Meta:
  125. verbose_name = "ПользовательКнигаКоллекция"
  126. verbose_name_plural = "ПользователиКнигиКоллекции"
  127. def __str__(self):
  128. return f"{self.user} {self.book} {self.collection}"