food diary improvements

This commit is contained in:
Joren de Graaf
2026-10-17 12:12:41 +02:00
parent 76c76bc281
commit e3b14d6823
9 changed files with 258 additions and 20 deletions
+17 -5
View File
@@ -1,5 +1,6 @@
from django import forms
from decimal import Decimal
import uuid
from .models import FoodLogEntry, FoodPortion, FoodProduct, KitchenInventoryItem
@@ -61,6 +62,9 @@ class ManualFoodForm(forms.ModelForm):
class FoodLogForm(forms.ModelForm):
portion = forms.ChoiceField(label='Portion', required=False)
portion_count = forms.IntegerField(label='How many portions', min_value=1, max_value=999,
required=False, initial=1)
inventory_package = forms.ChoiceField(label='Use inventory', required=False)
save_portion = forms.BooleanField(required=False, label='Save custom amount as a portion')
new_portion_label = forms.CharField(max_length=100, required=False, label='New portion label')
make_default = forms.BooleanField(required=False, label='Use this portion by default')
@@ -71,9 +75,10 @@ class FoodLogForm(forms.ModelForm):
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):
def __init__(self, *args, product=None, inventory_options=None, **kwargs):
super().__init__(*args, **kwargs)
self.product = product or getattr(self.instance, 'product', None)
self.fields['inventory_package'].choices = [('', 'Log without using inventory')] + (inventory_options or [])
if self.instance.pk and self.instance.created_via == 'recipe':
self.fields['unit'].choices = [('servings', 'servings')]
if self.product and self.product.pk:
@@ -121,18 +126,19 @@ class FoodLogForm(forms.ModelForm):
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'
count = data.get('portion_count') or 1
if not self.product or not selected:
return data
if selected == 'base':
data['amount'], data['portion_label_snapshot'] = Decimal('100'), ''
data['amount'], data['portion_label_snapshot'] = Decimal('100') * count, ''
elif selected == 'serving':
data['amount'], data['portion_label_snapshot'] = self.product.serving_amount, 'Serving'
data['amount'], data['portion_label_snapshot'] = self.product.serving_amount * count, f'{count} × serving' if count > 1 else 'Serving'
elif selected.startswith('portion:'):
portion = self.portions.get(selected.partition(':')[2])
if not portion:
self.add_error('portion', 'Choose a valid portion.')
else:
data['amount'], data['portion_label_snapshot'] = portion.amount, portion.label
data['amount'], data['portion_label_snapshot'] = portion.amount * count, f'{count} × {portion.label}' if count > 1 else portion.label
elif selected == 'custom':
if data.get('amount') is None or data['amount'] <= 0:
self.add_error('amount', 'Enter an amount greater than zero.')
@@ -141,7 +147,13 @@ class FoodLogForm(forms.ModelForm):
if not label:
self.add_error('new_portion_label', 'Enter a label for the saved portion.')
elif FoodPortion.objects.filter(user=self.product.owner, product=self.product, label=label).exists():
self.add_error('new_portion_label', 'You already have a portion with this label.')
try:
submission_id = uuid.UUID(self.data.get('submission_id', ''))
except (ValueError, TypeError, AttributeError):
submission_id = None
if not (submission_id and FoodLogEntry.objects.filter(user=self.product.owner,
api_id=submission_id).exists()):
self.add_error('new_portion_label', 'You already have a portion with this label.')
else:
data['portion_label_snapshot'] = label
elif data.get('make_default'):