<gh_stars>0
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.conf import settings

import uuid
import os

def recipe_image_file_path(instance, filename):
	"""Generate file path for new recipe image"""
	ext = filename.split('.')[-1]
	filename = f'{uuid.uuid4()}.{ext}'

	return os.path.join('uploads/recipe/', filename)

class UserManager(BaseUserManager):
	
	def create_user(self, email, password = None, **extra_fields):
		"""Creates and saves a new user"""
		if not email:
			raise ValueError('User must have email')
		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):
		"""Creates 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):
	"""Custom user model that supports email instead of username"""
	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)

	objects = UserManager()

	USERNAME_FIELD = 'email'
	

class Tag(models.Model):
	"""Tag to be user for recipe"""
	name = models.CharField(max_length=255)
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)

	def __str__(self):
		return self.name


class Ingredient(models.Model):
	"""Ingredient to be added in the recipe"""
	name = models.CharField(max_length=255)
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)

	def __str__(self):
		return self.name


class Recipe(models.Model):
	"""Recipe object"""
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)
	title = models.CharField(max_length=255)
	time_minutes = models.IntegerField()
	price = models.DecimalField(max_digits=5, decimal_places=2)
	link = models.CharField(max_length=255, blank=True)
	ingredients = models.ManyToManyField('Ingredient')
	tags = models.ManyToManyField('Tag')
	image = models.ImageField(null=True, upload_to=recipe_image_file_path)

	def __str__(self):
		return self.title
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.conf import settings

import uuid
import os

def recipe_image_file_path(instance, filename):
	"""Generate file path for new recipe image"""
	ext = filename.split('.')[-1]
	filename = f'{uuid.uuid4()}.{ext}'

	return os.path.join('uploads/recipe/', filename)

class UserManager(BaseUserManager):
	
	def create_user(self, email, password = None, **extra_fields):
		"""Creates and saves a new user"""
		if not email:
			raise ValueError('User must have email')
		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):
		"""Creates 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):
	"""Custom user model that supports email instead of username"""
	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)

	objects = UserManager()

	USERNAME_FIELD = 'email'
	

class Tag(models.Model):
	"""Tag to be user for recipe"""
	name = models.CharField(max_length=255)
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)

	def __str__(self):
		return self.name


class Ingredient(models.Model):
	"""Ingredient to be added in the recipe"""
	name = models.CharField(max_length=255)
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)

	def __str__(self):
		return self.name


class Recipe(models.Model):
	"""Recipe object"""
	user = models.ForeignKey(
		settings.AUTH_USER_MODEL,
		on_delete=models.CASCADE
	)
	title = models.CharField(max_length=255)
	time_minutes = models.IntegerField()
	price = models.DecimalField(max_digits=5, decimal_places=2)
	link = models.CharField(max_length=255, blank=True)
	ingredients = models.ManyToManyField('Ingredient')
	tags = models.ManyToManyField('Tag')
	image = models.ImageField(null=True, upload_to=recipe_image_file_path)

	def __str__(self):
		return self.title
Generate file path for new recipe image Creates and saves a new user Creates new superuser Custom user model that supports email instead of username Tag to be user for recipe Ingredient to be added in the recipe Recipe object