Recipes phase 4

This commit is contained in:
Joren de Graaf
2026-10-12 10:00:02 +02:00
parent b0e95c62f5
commit 2a888e5346
17 changed files with 590 additions and 5 deletions
+8
View File
@@ -74,6 +74,8 @@ class FoodLogForm(forms.ModelForm):
def __init__(self, *args, product=None, **kwargs):
super().__init__(*args, **kwargs)
self.product = product or getattr(self.instance, 'product', None)
if self.instance.pk and self.instance.created_via == 'recipe':
self.fields['unit'].choices = [('servings', 'servings')]
if self.product and self.product.pk:
self.fields['unit'].initial = self.product.base_unit
unit = self.product.base_unit
@@ -106,12 +108,18 @@ class FoodLogForm(forms.ModelForm):
def clean_unit(self):
unit = self.cleaned_data['unit']
if self.instance.pk and self.instance.created_via == 'recipe' and unit != 'servings':
raise forms.ValidationError('Recipe entries use servings.')
if self.product and unit != self.product.base_unit:
raise forms.ValidationError('Use the same unit as the product nutrition basis.')
return unit
def clean(self):
data = super().clean()
if self.instance.pk and self.instance.created_via == 'recipe':
run = getattr(self.instance, 'recipe_cooking_run', None)
if run and data.get('amount') is not None and data['amount'] > run.prepared_servings:
self.add_error('amount', 'This portion exceeds the prepared servings.')
selected = data.get('portion') or 'custom'
if not self.product or not selected:
return data