Recipes phase 3
This commit is contained in:
@@ -28,6 +28,9 @@ class Recipe(models.Model):
|
||||
cooking_minutes = models.PositiveIntegerField(default=0)
|
||||
default_servings = models.DecimalField(max_digits=7, decimal_places=2, default=2,
|
||||
validators=[MinValueValidator(Decimal('0.01'))])
|
||||
yield_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
yield_unit = models.CharField(max_length=2, choices=[('g', 'g'), ('ml', 'ml')], blank=True)
|
||||
category = models.CharField(max_length=80, blank=True)
|
||||
tags = models.JSONField(default=list, blank=True)
|
||||
is_favourite = models.BooleanField(default=False)
|
||||
@@ -50,6 +53,8 @@ class Recipe(models.Model):
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if bool(self.yield_amount) != bool(self.yield_unit):
|
||||
raise ValidationError({'yield_amount': 'Provide both an amount and a unit for recipe yield.'})
|
||||
if not isinstance(self.tags, list) or any(not isinstance(tag, str) for tag in self.tags):
|
||||
raise ValidationError({'tags': 'Tags must be a list of names.'})
|
||||
if self.parent_id:
|
||||
@@ -87,6 +92,9 @@ class RecipeIngredient(models.Model):
|
||||
quantity = models.DecimalField(max_digits=10, decimal_places=3,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
unit = models.CharField(max_length=10, choices=Unit.choices)
|
||||
amount_per_unit = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
amount_per_unit_unit = models.CharField(max_length=2, choices=[('g', 'g'), ('ml', 'ml')], blank=True)
|
||||
display_order = models.PositiveIntegerField(default=0)
|
||||
inventory_product = models.ForeignKey('food.FoodProduct', on_delete=models.SET_NULL,
|
||||
null=True, blank=True, related_name='recipe_inventory_ingredients')
|
||||
@@ -102,6 +110,10 @@ class RecipeIngredient(models.Model):
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if bool(self.amount_per_unit) != bool(self.amount_per_unit_unit):
|
||||
raise ValidationError({'amount_per_unit': 'Provide both an amount and unit for one piece or measure.'})
|
||||
if self.amount_per_unit and self.unit not in ('pieces', 'portions', 'tsp', 'tbsp'):
|
||||
raise ValidationError({'amount_per_unit': 'Only count or measure units need this conversion.'})
|
||||
if self.recipe_id:
|
||||
for field in ('inventory_product', 'nutrition_product'):
|
||||
product = getattr(self, field)
|
||||
@@ -156,6 +168,9 @@ class RecipeIngredientSubstitution(models.Model):
|
||||
quantity = models.DecimalField(max_digits=10, decimal_places=3,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
unit = models.CharField(max_length=10, choices=RecipeIngredient.Unit.choices)
|
||||
amount_per_unit = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
amount_per_unit_unit = models.CharField(max_length=2, choices=[('g', 'g'), ('ml', 'ml')], blank=True)
|
||||
product = models.ForeignKey('food.FoodProduct', on_delete=models.SET_NULL,
|
||||
null=True, blank=True, related_name='recipe_substitutions')
|
||||
display_order = models.PositiveIntegerField(default=0)
|
||||
@@ -165,5 +180,32 @@ class RecipeIngredientSubstitution(models.Model):
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if bool(self.amount_per_unit) != bool(self.amount_per_unit_unit):
|
||||
raise ValidationError({'amount_per_unit': 'Provide both an amount and unit for one piece or measure.'})
|
||||
if self.amount_per_unit and self.unit not in ('pieces', 'portions', 'tsp', 'tbsp'):
|
||||
raise ValidationError({'amount_per_unit': 'Only count or measure units need this conversion.'})
|
||||
if self.product_id and self.ingredient_id and self.product.owner_id != self.ingredient.recipe.owner_id:
|
||||
raise ValidationError({'product': 'Choose one of your own food products.'})
|
||||
|
||||
|
||||
class RecipeUse(models.Model):
|
||||
"""One confirmed, reversible multi-product stock operation."""
|
||||
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
recipe = models.ForeignKey(Recipe, on_delete=models.SET_NULL, null=True, related_name='stock_uses')
|
||||
recipe_title_snapshot = models.CharField(max_length=180)
|
||||
servings = models.DecimalField(max_digits=7, decimal_places=2)
|
||||
request_signature = models.CharField(max_length=64)
|
||||
plan_snapshot = models.JSONField(default=dict)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
undone_at = models.DateTimeField(null=True, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('-created_at',)
|
||||
|
||||
|
||||
class RecipeUseLine(models.Model):
|
||||
recipe_use = models.ForeignKey(RecipeUse, on_delete=models.CASCADE, related_name='lines')
|
||||
consumption = models.OneToOneField('food.InventoryConsumption', on_delete=models.PROTECT,
|
||||
related_name='recipe_use_line')
|
||||
|
||||
Reference in New Issue
Block a user