from django.db import models from django.core.validators import FileExtensionValidator from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin class Genre(models.Model): title = models.CharField(verbose_name="Название", max_length=50) class Meta: verbose_name = "Жанр" verbose_name_plural = "Жанры" def __str__(self): return self.title class Author(models.Model): name = models.CharField(max_length=100, unique=True, verbose_name="ФИО") avatar = models.ImageField(verbose_name='Аватар автора', upload_to='images/author/avatars/%Y/%m/%d/', default='images/author/avatars/default.jpeg', blank=True, validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])]) class Meta: verbose_name = "Автор" verbose_name_plural = "Авторы" def __str__(self): return self.name class Collection(models.Model): title = models.CharField(max_length=100, unique=True, verbose_name="Название") class Meta: verbose_name = "Коллекция" verbose_name_plural = "Коллекции" def __str__(self): return self.title class Genders(models.Model): title = models.CharField(max_length=15, unique=True, verbose_name="Название") class Meta: verbose_name = "Пол" verbose_name_plural = "Полы" def __str__(self): return self.title class UserManager(BaseUserManager): use_in_migrations = True def create_user(self, email, name, password): user = self.model( email=self.normalize_email(email), name=name, ) user.set_password(password) user.save(using=self._db) return user def create_staffuser(self, email, name, password): user = self.create_user( email, password=password, name=name, ) user.is_staff = True user.save(using=self._db) return user def create_superuser(self, email, name, password): user = self.create_user( email, password=password, name=name, ) user.is_staff = True user.admin = True user.save(using=self._db) return user class User(AbstractBaseUser, PermissionsMixin): username = None email = models.EmailField(max_length=50, unique=True) name = models.CharField(max_length=30) birthdate = models.DateField(null=True) avatar = models.ImageField(verbose_name='Аватар пользователя', upload_to='images/user/avatars/%Y/%m/%d/', default='images/user/avatars/default.jpeg', blank=True, validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])]) is_staff = models.BooleanField(default=False) admin = models.BooleanField(default=False) USERNAME_FIELD = "email" REQUIRED_FIELDS = ['name'] objects = UserManager() def __str__(self): return self.email class Action(models.Model): title = models.CharField(max_length=50, unique=True, verbose_name="Название") weight = models.FloatField(null=True, verbose_name="Вес") users = models.ManyToManyField(User) class Meta: verbose_name = "Действие" verbose_name_plural = "Действия" def __str__(self): return self.title class Metric(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) date = models.DateTimeField(auto_now_add=True, verbose_name="Дата") pages_read = models.IntegerField(null=True, verbose_name="Страниц прочитано") reading_time = models.TimeField(null=True, verbose_name="Время чтения") books_read = models.IntegerField(null=True, verbose_name="Книг прочитано") class Meta(): verbose_name = "Метрика" verbose_name_plural = "Метрики" def __str__(self): return f'{self.date} {self.user}' class Book(models.Model): title = models.CharField(max_length=50, unique=True, verbose_name="Название") annotation = models.TextField(verbose_name="Аннотация") symbols_count = models.IntegerField(null = True, verbose_name="Количество символов") book_file = models.FileField(verbose_name='Файл книги', upload_to='books/covers/', null=True, validators=[FileExtensionValidator(allowed_extensions=['epub', 'fb2', 'pdf', 'docx'])]) pages_count = models.IntegerField(null=True,verbose_name="Количество страниц") is_recognized = models.BooleanField(default=False, verbose_name="Распознана") last_update = models.DateTimeField(auto_now=True, verbose_name="Последнее обновление") cover = models.FileField(verbose_name='Обложка книги', upload_to='images/covers/', blank=True, validators=[FileExtensionValidator(allowed_extensions=['jpg', 'png', 'jpeg'])]) is_private = models.BooleanField(default=False, verbose_name="Личная") authors = models.ManyToManyField(Author) genres = models.ManyToManyField(Genre) class Meta: verbose_name = "Книга" verbose_name_plural = "Книги" def __str__(self): return self.title class UsersBooksCollections(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) book = models.ForeignKey(Book, on_delete=models.CASCADE) collection = models.ForeignKey(Collection, on_delete=models.CASCADE) current_page = models.IntegerField(default=0, verbose_name="Текущая страница") date_added = models.DateTimeField(auto_now=True, verbose_name="Дата добавления") class Meta: verbose_name = "ПользовательКнигаКоллекция" verbose_name_plural = "ПользователиКнигиКоллекции" def __str__(self): return f"{self.user} {self.book} {self.collection}"