adding food logging, phase 2

This commit is contained in:
Joren de Graaf
2026-10-03 20:47:32 +02:00
parent 761cb180c5
commit fca2ef2acd
11 changed files with 294 additions and 12 deletions
+21 -1
View File
@@ -1,6 +1,6 @@
from django import forms
from .models import FoodProduct, Unit
from .models import FoodLogEntry, FoodProduct, MealType
class ManualFoodForm(forms.ModelForm):
@@ -18,3 +18,23 @@ class ManualFoodForm(forms.ModelForm):
super().__init__(*args, **kwargs)
for field in ('energy_kcal_100', 'carbohydrates_100', 'protein_100', 'fat_100'):
self.fields[field].required = True
class FoodLogForm(forms.ModelForm):
class Meta:
model = FoodLogEntry
fields = ('meal', 'amount', 'unit', 'portion_label_snapshot')
labels = {'portion_label_snapshot': 'Portion label (optional)'}
widgets = {'amount': forms.NumberInput(attrs={'min': '0.001', 'step': '0.001'})}
def __init__(self, *args, product=None, **kwargs):
super().__init__(*args, **kwargs)
self.product = product or getattr(self.instance, 'product', None)
if self.product and self.product.pk:
self.fields['unit'].initial = self.product.base_unit
def clean_unit(self):
unit = self.cleaned_data['unit']
if self.product and unit != self.product.base_unit:
raise forms.ValidationError('Use the same unit as the product nutrition basis.')
return unit