2023-10-07 16:36:13 -03:00
|
|
|
"""
|
|
|
|
Databse models.
|
|
|
|
"""
|
2023-10-09 01:25:27 -03:00
|
|
|
from django.conf import settings
|
2023-10-07 16:36:13 -03:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import (
|
|
|
|
AbstractBaseUser,
|
|
|
|
BaseUserManager,
|
|
|
|
PermissionsMixin,
|
|
|
|
)
|
2023-10-05 22:48:42 -03:00
|
|
|
|
2023-10-07 16:36:13 -03:00
|
|
|
|
|
|
|
class UserManager(BaseUserManager):
|
|
|
|
"""Manager for users."""
|
|
|
|
|
|
|
|
def create_user(self, email, password=None, **extra_fields):
|
|
|
|
"""Create, save and return a new user."""
|
|
|
|
if not email:
|
|
|
|
raise ValueError('User must have an email address.')
|
|
|
|
user = self.model(email=self.normalize_email(email), **extra_fields)
|
|
|
|
user.set_password(password)
|
|
|
|
user.save(using=self._db)
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
def create_superuser(self, email, password):
|
|
|
|
"""Create and return a new superuser."""
|
|
|
|
user = self.create_user(email, password)
|
|
|
|
user.is_staff = True
|
|
|
|
user.is_superuser = True
|
|
|
|
user.save(using=self._db)
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class User(AbstractBaseUser, PermissionsMixin):
|
|
|
|
"""User in the system."""
|
|
|
|
email = models.EmailField(max_length=255, unique=True)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
is_active = models.BooleanField(default=True)
|
|
|
|
is_staff = models.BooleanField(default=False)
|
|
|
|
# Asignar el UserManager a esta clase User
|
|
|
|
objects = UserManager()
|
|
|
|
|
|
|
|
USERNAME_FIELD = 'email'
|
2023-10-09 01:25:27 -03:00
|
|
|
|
|
|
|
|
|
|
|
class Recipe(models.Model):
|
|
|
|
"""Recipe object."""
|
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
title = models.CharField(max_length=255)
|
|
|
|
description = models.TextField(blank=True)
|
|
|
|
time_minutes = models.IntegerField()
|
2023-10-09 03:25:09 -03:00
|
|
|
price = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
|
2023-10-09 01:25:27 -03:00
|
|
|
link = models.CharField(max_length=255, blank=True)
|
2023-10-10 00:08:52 -03:00
|
|
|
tags = models.ManyToManyField('Tag')
|
2023-10-09 01:25:27 -03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
2023-10-10 00:08:52 -03:00
|
|
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
|
"""Tag for filtering recipes."""
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|