Recipes phase 4
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 6.1.1 on 2026-10-12 07:41
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0009_backfill_food_hydration'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='foodlogentry',
|
||||
name='unit',
|
||||
field=models.CharField(choices=[('g', 'g'), ('ml', 'ml'), ('servings', 'servings')], max_length=8),
|
||||
),
|
||||
]
|
||||
+1
-1
@@ -79,7 +79,7 @@ class FoodLogEntry(models.Model):
|
||||
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)
|
||||
unit = models.CharField(max_length=8, choices=[*Unit.choices, ('servings', 'servings')])
|
||||
beverage_volume_ml = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
|
||||
hydration_amount_ml = models.DecimalField(max_digits=10, decimal_places=3, default=0, validators=NON_NEGATIVE)
|
||||
hydration_factor_snapshot = models.DecimalField(max_digits=4, decimal_places=3, default=0, validators=NON_NEGATIVE)
|
||||
|
||||
@@ -25,12 +25,19 @@ def selected_date(request):
|
||||
|
||||
def diary_context(user, day, language='en'):
|
||||
goals, _ = UserSettings.objects.get_or_create(user=user)
|
||||
entries = list(FoodLogEntry.objects.filter(user=user, date=day).select_related('product')
|
||||
entries = list(FoodLogEntry.objects.filter(user=user, date=day).select_related('product', 'recipe_cooking_run')
|
||||
.prefetch_related('attachments').defer('product__raw_data'))
|
||||
for entry in entries:
|
||||
run = getattr(entry, 'recipe_cooking_run', None)
|
||||
entry.nutrition_incomplete = bool(run and any(
|
||||
not row.get('complete', False) for row in run.nutrition_snapshot.values()))
|
||||
totals = {name: sum((getattr(entry, name) or ZERO for entry in entries), ZERO) for name in
|
||||
('calories_kcal', 'carbohydrates_g', 'protein_g', 'fat_g')}
|
||||
fibre_values = [entry.fibre_g for entry in entries if entry.fibre_g is not None]
|
||||
fibre_incomplete = len(fibre_values) != len(entries)
|
||||
fibre_incomplete = len(fibre_values) != len(entries) or any(
|
||||
(getattr(entry, 'recipe_cooking_run', None) and
|
||||
not entry.recipe_cooking_run.nutrition_snapshot.get('fibre_g', {}).get('complete', False))
|
||||
for entry in entries)
|
||||
totals['fibre_g'] = sum(fibre_values, ZERO) if fibre_values or not entries else None
|
||||
meals = []
|
||||
for key, label in MealType.choices:
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
{% if images %}<div class="food-entry-images">{% for attachment in images %}<a href="{% url 'food:attachment-file' attachment.api_id %}" target="_blank" rel="noopener" aria-label="View attached image{% if attachment.caption %}: {{ attachment.caption }}{% else %} for {{ entry.product_name_snapshot }}{% endif %}"><img src="{% url 'food:attachment-file' attachment.api_id %}" alt="" loading="lazy"></a>{% endfor %}</div>
|
||||
{% elif entry.product and entry.product.image_url %}<img src="{{ entry.product.image_url }}" alt="" loading="lazy">{% endif %}
|
||||
{% endwith %}
|
||||
<div><strong>{{ entry.product_name_snapshot }}</strong>{% if entry.brand_snapshot %}<span>{{ entry.brand_snapshot }}</span>{% endif %}<small>{% if entry.calories_kcal is not None %}{{ entry.calories_kcal|floatformat:0 }} kcal{% else %}— kcal{% endif %} · {% if entry.portion_label_snapshot %}{{ entry.portion_label_snapshot }} {% endif %}({{ entry.amount|floatformat:-3 }} {{ entry.unit }})</small></div>
|
||||
<div><strong>{{ entry.product_name_snapshot }}</strong>{% if entry.brand_snapshot %}<span>{{ entry.brand_snapshot }}</span>{% endif %}<small>{% if entry.calories_kcal is not None %}{{ entry.calories_kcal|floatformat:0 }} kcal{% else %}— kcal{% endif %} · {% if entry.portion_label_snapshot %}{{ entry.portion_label_snapshot }} {% endif %}({{ entry.amount|floatformat:-3 }} {{ entry.unit }}){% if entry.nutrition_incomplete %} · Incomplete nutrition estimate{% endif %}</small></div>
|
||||
</div>
|
||||
<div class="food-entry-actions"><a href="{% url 'food:edit' entry.pk %}" aria-label="Edit {{ entry.product_name_snapshot }}">Edit</a>{% if entry.product %}<a href="{% url 'food:product-hydration' entry.product.pk %}" aria-label="Drink settings for {{ entry.product_name_snapshot }}">Drink settings</a>{% endif %}</div>
|
||||
</li>
|
||||
|
||||
@@ -372,6 +372,11 @@ def edit_entry(request, pk):
|
||||
sync_food_hydration(updated)
|
||||
updated.full_clean()
|
||||
updated.save()
|
||||
if updated.created_via == 'recipe':
|
||||
run = getattr(updated, 'recipe_cooking_run', None)
|
||||
if run:
|
||||
run.consumed_servings = updated.amount
|
||||
run.save(update_fields=('consumed_servings',))
|
||||
if product:
|
||||
_save_custom_portion(form, request.user, product, updated)
|
||||
return _diary_response(request, entry.date)
|
||||
|
||||
Reference in New Issue
Block a user