latest changes
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import uuid
|
||||
from decimal import Decimal
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
|
||||
|
||||
class GroceryItem(models.Model):
|
||||
class Status(models.TextChoices):
|
||||
NEEDED = 'needed', 'Needed'
|
||||
PURCHASED = 'purchased', 'Purchased'
|
||||
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='grocery_items')
|
||||
name = models.CharField(max_length=180)
|
||||
quantity = models.DecimalField(max_digits=10, decimal_places=3,
|
||||
validators=[MinValueValidator(Decimal('0.001'))])
|
||||
unit = models.CharField(max_length=10, choices=[('pieces', 'pieces'), ('g', 'g'), ('kg', 'kg'),
|
||||
('ml', 'ml'), ('L', 'L'), ('packs', 'packs'),
|
||||
('portions', 'portions'), ('tsp', 'tsp'), ('tbsp', 'tbsp')])
|
||||
category = models.CharField(max_length=80, blank=True)
|
||||
product = models.ForeignKey('food.FoodProduct', on_delete=models.SET_NULL, null=True, blank=True)
|
||||
notes = models.TextField(blank=True)
|
||||
status = models.CharField(max_length=12, choices=Status.choices, default=Status.NEEDED)
|
||||
purchased_at = models.DateTimeField(null=True, blank=True)
|
||||
inventory_transfer = models.OneToOneField('food.KitchenInventoryItem', on_delete=models.SET_NULL,
|
||||
null=True, blank=True, related_name='grocery_origin')
|
||||
inventory_transferred_at = models.DateTimeField(null=True, blank=True)
|
||||
idempotency_key = models.CharField(max_length=100, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('status', 'category', 'name', 'created_at')
|
||||
constraints = [
|
||||
models.CheckConstraint(condition=Q(quantity__gt=0), name='groceries_positive_quantity'),
|
||||
models.UniqueConstraint(fields=('user', 'idempotency_key'), condition=~Q(idempotency_key=''),
|
||||
name='groceries_unique_idempotency_key'),
|
||||
]
|
||||
|
||||
def clean(self):
|
||||
if self.product_id and self.user_id and self.product.owner_id != self.user_id:
|
||||
raise ValidationError({'product': 'Choose one of your own food products.'})
|
||||
|
||||
|
||||
class GrocerySource(models.Model):
|
||||
item = models.ForeignKey(GroceryItem, on_delete=models.CASCADE, related_name='sources')
|
||||
recipe = models.ForeignKey('recipes.Recipe', on_delete=models.CASCADE)
|
||||
ingredient = models.ForeignKey('recipes.RecipeIngredient', on_delete=models.SET_NULL, null=True, blank=True)
|
||||
requested_quantity = models.DecimalField(max_digits=10, decimal_places=3)
|
||||
requested_unit = models.CharField(max_length=10)
|
||||
selection_key = models.CharField(max_length=120)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('item', 'recipe', 'ingredient', 'selection_key'),
|
||||
name='groceries_unique_source')]
|
||||
|
||||
def clean(self):
|
||||
if self.item_id and self.recipe_id and self.item.user_id != self.recipe.owner_id:
|
||||
raise ValidationError('Recipe and grocery item must have the same owner.')
|
||||
|
||||
|
||||
class GroceryRecipeDemand(models.Model):
|
||||
"""Remember recipe demand even when inventory currently covers it."""
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
recipe = models.ForeignKey('recipes.Recipe', on_delete=models.CASCADE)
|
||||
ingredient = models.ForeignKey('recipes.RecipeIngredient', on_delete=models.CASCADE)
|
||||
selection_key = models.CharField(max_length=120, blank=True)
|
||||
name = models.CharField(max_length=180)
|
||||
product = models.ForeignKey('food.FoodProduct', on_delete=models.SET_NULL, null=True, blank=True)
|
||||
quantity = models.DecimalField(max_digits=10, decimal_places=3)
|
||||
unit = models.CharField(max_length=10)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'recipe', 'ingredient', 'selection_key'),
|
||||
name='groceries_unique_recipe_demand')]
|
||||
Reference in New Issue
Block a user