|
|
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
|
|
|
from django.db import models
|
|
|
import datetime
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
from .managers import ReyMotaUserManager
|
|
|
|
|
|
def current_year():
|
|
|
return datetime.date.today().year
|
|
|
|
|
|
|
|
|
def max_value_current_year(value):
|
|
|
return MaxValueValidator(current_year())(value)
|
|
|
|
|
|
class Autor(models.Model):
|
|
|
nombre = models.CharField(max_length=200)
|
|
|
biografia = models.TextField(blank=True, null=True)
|
|
|
foto = models.ImageField(upload_to='autores/', blank=True, null=True) # Nuevo campo
|
|
|
|
|
|
def __str__(self):
|
|
|
return self.nombre
|
|
|
|
|
|
class Libro(models.Model):
|
|
|
titulo = models.CharField(max_length=200)
|
|
|
autor = models.ForeignKey(Autor, on_delete=models.CASCADE)
|
|
|
fecha_publicacion = models.PositiveBigIntegerField(default=current_year(), validators=[MinValueValidator(1984), max_value_current_year])
|
|
|
descripcion = models.TextField(blank=True, null=True)
|
|
|
archivo = models.FileField(upload_to='libros/')
|
|
|
portada = models.ImageField(upload_to='portadas/', blank=True, null=True) # Nuevo campo
|
|
|
|
|
|
def __str__(self):
|
|
|
return self.titulo
|
|
|
|
|
|
|
|
|
class ReyMotaUser(AbstractBaseUser, PermissionsMixin):
|
|
|
email = models.EmailField(_("email address"), unique=True)
|
|
|
foto = models.ImageField(upload_to="profile_images", blank=True)
|
|
|
is_staff = models.BooleanField(default=False)
|
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
|
|
USERNAME_FIELD = "email"
|
|
|
REQUIRED_FIELDS = []
|
|
|
|
|
|
objects = ReyMotaUserManager()
|
|
|
|
|
|
def __str__(self):
|
|
|
return self.email
|