adding food logging, phase 1
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
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
|
||||
|
||||
|
||||
NON_NEGATIVE = [MinValueValidator(0)]
|
||||
POSITIVE = [MinValueValidator(0.001)]
|
||||
|
||||
|
||||
class Unit(models.TextChoices):
|
||||
GRAM = 'g', 'g'
|
||||
MILLILITRE = 'ml', 'ml'
|
||||
|
||||
|
||||
class MealType(models.TextChoices):
|
||||
BREAKFAST = 'breakfast', 'Breakfast'
|
||||
LUNCH = 'lunch', 'Lunch'
|
||||
DINNER = 'dinner', 'Dinner'
|
||||
SNACKS = 'snacks', 'Snacks'
|
||||
|
||||
|
||||
class FoodProduct(models.Model):
|
||||
class Source(models.TextChoices):
|
||||
OPEN_FOOD_FACTS = 'openfoodfacts', 'Open Food Facts'
|
||||
MANUAL = 'manual', 'Manual'
|
||||
|
||||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='food_products')
|
||||
barcode = models.CharField(max_length=64, blank=True, db_index=True)
|
||||
product_name = models.CharField(max_length=255)
|
||||
brand = models.CharField(max_length=255, blank=True)
|
||||
image_url = models.URLField(blank=True)
|
||||
package_quantity = models.CharField(max_length=100, blank=True)
|
||||
serving_size_text = models.CharField(max_length=100, blank=True)
|
||||
serving_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
|
||||
serving_unit = models.CharField(max_length=2, choices=Unit.choices, blank=True)
|
||||
base_unit = models.CharField(max_length=2, choices=Unit.choices, default=Unit.GRAM)
|
||||
energy_kcal_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
carbohydrates_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
protein_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
fat_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
saturated_fat_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
sugars_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
fibre_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
salt_100 = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
source = models.CharField(max_length=16, choices=Source.choices, default=Source.MANUAL)
|
||||
raw_data = models.JSONField(default=dict, blank=True)
|
||||
fetched_at = models.DateTimeField(null=True, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('owner', 'barcode'), condition=~Q(barcode=''), name='food_unique_owner_barcode')]
|
||||
|
||||
def __str__(self):
|
||||
return self.product_name
|
||||
|
||||
|
||||
class FoodLogEntry(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='food_log_entries')
|
||||
date = models.DateField(db_index=True)
|
||||
time = models.TimeField(null=True, blank=True)
|
||||
meal = models.CharField(max_length=9, choices=MealType.choices)
|
||||
product = models.ForeignKey(FoodProduct, on_delete=models.SET_NULL, null=True, blank=True, related_name='log_entries')
|
||||
product_name_snapshot = models.CharField(max_length=255)
|
||||
brand_snapshot = models.CharField(max_length=255, blank=True)
|
||||
barcode_snapshot = models.CharField(max_length=64, blank=True)
|
||||
portion_label_snapshot = models.CharField(max_length=100, blank=True)
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=3, validators=POSITIVE)
|
||||
unit = models.CharField(max_length=2, choices=Unit.choices)
|
||||
calories_kcal = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
carbohydrates_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
protein_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
fat_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
fibre_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
sugars_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
salt_g = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
source = models.CharField(max_length=16, choices=FoodProduct.Source.choices, default=FoodProduct.Source.MANUAL)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ('date', 'time', 'pk')
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
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.'})
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.product_name_snapshot} on {self.date}'
|
||||
|
||||
|
||||
class FoodFavourite(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='food_favourites')
|
||||
product = models.ForeignKey(FoodProduct, on_delete=models.CASCADE, related_name='favourites')
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'product'), name='food_unique_favourite')]
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
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 FoodPortion(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='food_portions')
|
||||
product = models.ForeignKey(FoodProduct, on_delete=models.CASCADE, related_name='portions')
|
||||
label = models.CharField(max_length=100)
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=3, validators=POSITIVE)
|
||||
unit = models.CharField(max_length=2, choices=Unit.choices)
|
||||
is_default = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'product', 'label'), name='food_unique_portion_label')]
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
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.'})
|
||||
Reference in New Issue
Block a user