adding food logging, phase 5

This commit is contained in:
Joren de Graaf
2026-10-03 21:25:56 +02:00
parent e7ddb0ea28
commit 8d1afab9e8
20 changed files with 627 additions and 73 deletions
+1 -46
View File
@@ -2,50 +2,5 @@
{% block title %}Add food · Tasknest{% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'food/food.css' %}">
<div id="food-workspace" class="food-form-page">
<a href="{% url 'food:diary' %}?date={{ day|date:'Y-m-d' }}">← Back to diary</a>
<h1>Add food</h1>
{% if product %}
<section class="food-card">
{% if product.image_url %}<img class="food-product-image" src="{{ product.image_url }}" alt="" loading="lazy">{% endif %}
<h2>{{ product.product_name }}</h2>
{% if product.brand %}<p>{{ product.brand }}</p>{% endif %}
<p>Per 100 {{ product.base_unit }}: {% if product.energy_kcal_100 is not None %}{{ product.energy_kcal_100|floatformat:0 }} kcal{% else %}— kcal{% endif %}</p>
<details><summary>Nutrition information</summary><dl>
<dt>Carbohydrates</dt><dd>{{ product.carbohydrates_100|default_if_none:'—' }} g</dd>
<dt>Protein</dt><dd>{{ product.protein_100|default_if_none:'—' }} g</dd>
<dt>Fat</dt><dd>{{ product.fat_100|default_if_none:'—' }} g</dd>
<dt>Sugars</dt><dd>{{ product.sugars_100|default_if_none:'—' }} g</dd>
<dt>Fibre</dt><dd>{{ product.fibre_100|default_if_none:'—' }} g</dd>
<dt>Salt</dt><dd>{{ product.salt_100|default_if_none:'—' }} g</dd>
</dl></details>
{% if product.source == 'openfoodfacts' %}<p class="food-attribution">Product data from <a href="https://world.openfoodfacts.org/product/{{ product.barcode }}" rel="noopener noreferrer">Open Food Facts</a>.</p>{% endif %}
</section>
<form method="post" class="food-card" hx-post="{% url 'food:add' %}" hx-target="#food-workspace" hx-push-url="{% url 'food:diary' %}?date={{ day|date:'Y-m-d' }}" hx-swap="outerHTML">
{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="product" value="{{ product.pk }}">
{% for field in form %}<div class="form-field">{{ field.label_tag }}{{ field }}{{ field.errors }}</div>{% endfor %}
<button class="button" type="submit">Log food</button>
</form>
{% else %}
<form method="get" class="food-search">
<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><input type="hidden" name="tab" value="{{ tab }}">
<label for="food-query">Search your foods</label><input id="food-query" name="q" value="{{ query }}" type="search"><button class="button" type="submit">Search</button>
</form>
<nav class="food-tabs" aria-label="Local food lists">
<a {% if tab == 'recent' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=recent&amp;q={{ query|urlencode }}">Recent</a>
<a {% if tab == 'favourites' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=favourites&amp;q={{ query|urlencode }}">Favourites</a>
<a {% if tab == 'mine' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=mine&amp;q={{ query|urlencode }}">My foods</a>
</nav>
<ul class="food-product-list">{% for item in products %}
<li><a href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;product={{ item.pk }}">
{% if item.image_url %}<img src="{{ item.image_url }}" alt="" loading="lazy">{% endif %}<span><strong>{{ item.product_name }}</strong>{% if item.brand %}<small>{{ item.brand }}</small>{% endif %}<small>{% if item.energy_kcal_100 is not None %}{{ item.energy_kcal_100|floatformat:0 }} kcal / 100 {{ item.base_unit }}{% else %}— kcal / 100 {{ item.base_unit }}{% endif %}</small></span>
</a></li>{% empty %}<li>No foods in this list yet.</li>{% endfor %}</ul>
<div class="food-actions"><a class="button" href="{% url 'food:manual' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Create a food manually</a>
<a class="button" href="{% url 'food:scanner' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Scan barcode</a>
<a class="button button--secondary" href="{% url 'food:external-search' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;q={{ query|urlencode }}">Search Open Food Facts</a></div>
<form method="post" action="{% url 'food:import-product' %}" class="food-barcode-form">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><label for="food-barcode">Barcode</label><input id="food-barcode" name="barcode" inputmode="numeric" pattern="[0-9]+" required><button class="button button--secondary" type="submit">Look up barcode</button></form>
<p class="food-attribution">External product data from <a href="https://world.openfoodfacts.org/" rel="noopener noreferrer">Open Food Facts</a>.</p>
{% endif %}
</div>
{% if not product %}<nav class="food-mobile-switch" aria-label="Add food method"><span aria-current="page">Search</span><a href="{% url 'food:scanner' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Barcode</a></nav>{% endif %}
{% include 'food/partials/add_content.html' %}
{% endblock %}
+4 -1
View File
@@ -1,3 +1,6 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Edit food log · Tasknest{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}"><div id="food-workspace" class="food-form-page"><a href="{% url 'food:diary' %}?date={{ entry.date|date:'Y-m-d' }}">← Back to diary</a><h1>{{ entry.product_name_snapshot }}</h1>{% if entry.brand_snapshot %}<p>{{ entry.brand_snapshot }}</p>{% endif %}<div class="food-card"><p>{% if entry.calories_kcal is not None %}{{ entry.calories_kcal|floatformat:0 }} kcal{% else %}— kcal{% endif %} · {{ entry.carbohydrates_g|default_if_none:'—' }} g carbs · {{ entry.protein_g|default_if_none:'—' }} g protein · {{ entry.fat_g|default_if_none:'—' }} g fat</p><details><summary>Nutrition information</summary><dl><dt>Fibre</dt><dd>{{ entry.fibre_g|default_if_none:'—' }} g</dd><dt>Sugars</dt><dd>{{ entry.sugars_g|default_if_none:'—' }} g</dd><dt>Salt</dt><dd>{{ entry.salt_g|default_if_none:'—' }} g</dd></dl></details></div><form method="post" class="food-card" hx-post="{% url 'food:edit' entry.pk %}" hx-target="#food-workspace" hx-swap="outerHTML" hx-push-url="{% url 'food:diary' %}?date={{ entry.date|date:'Y-m-d' }}">{% csrf_token %}{% for field in form %}<div class="form-field">{{ field.label_tag }}{{ field }}{{ field.errors }}</div>{% endfor %}<button class="button" type="submit">Save changes</button></form><form method="post" action="{% url 'food:delete' entry.pk %}" onsubmit="return confirm('Delete this food entry?')" hx-post="{% url 'food:delete' entry.pk %}" hx-target="#food-workspace" hx-swap="outerHTML" hx-push-url="{% url 'food:diary' %}?date={{ entry.date|date:'Y-m-d' }}">{% csrf_token %}<button type="submit" class="button button--secondary">Delete entry</button></form></div>{% endblock %}
{% block content %}
<link rel="stylesheet" href="{% static 'food/food.css' %}">
{% include 'food/partials/edit_content.html' %}
{% endblock %}
+1 -1
View File
@@ -1,3 +1,3 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Create food · Tasknest{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}"><div class="food-form-page"><a href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">← Back to foods</a><h1>Create a food</h1><p>Enter nutrition per 100 g or 100 ml. Missing optional values stay unknown.</p><form method="post" class="food-card">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}">{% for field in form %}<div class="form-field">{{ field.label_tag }}{{ field }}{{ field.errors }}</div>{% endfor %}<button class="button" type="submit">Save food</button></form></div>{% endblock %}
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}"><div class="food-form-page"><a href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">← Back to foods</a><h1>Create a food</h1><p>Enter nutrition for the base quantity and unit you choose. Missing optional values stay unknown.</p><form method="post" class="food-card">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}">{% for field in form %}<div class="form-field">{{ field.label_tag }}{{ field }}{{ field.errors }}</div>{% endfor %}<button class="button" type="submit">Save food</button></form></div>{% endblock %}
@@ -0,0 +1,49 @@
{% load static %}
<div id="food-workspace" class="food-form-page">
<a href="{% url 'food:diary' %}?date={{ day|date:'Y-m-d' }}">← Back to diary</a>
<h1>Add food</h1>
{% if product %}
<section class="food-card">
{% if product.image_url %}<img class="food-product-image" src="{{ product.image_url }}" alt="" loading="lazy">{% endif %}
<div class="food-detail-title"><h2>{{ product.product_name }}</h2>{% include "food/partials/favourite_button.html" with is_favourite=product.is_favourite %}</div>
{% if product.brand %}<p>{{ product.brand }}</p>{% endif %}
<p>Per 100 {{ product.base_unit }}: {% if product.energy_kcal_100 is not None %}{{ product.energy_kcal_100|floatformat:0 }} kcal{% else %}— kcal{% endif %}</p>
<details><summary>Nutrition information</summary><dl>
<dt>Carbohydrates</dt><dd>{{ product.carbohydrates_100|default_if_none:'—' }} g</dd>
<dt>Protein</dt><dd>{{ product.protein_100|default_if_none:'—' }} g</dd>
<dt>Fat</dt><dd>{{ product.fat_100|default_if_none:'—' }} g</dd>
<dt>Sugars</dt><dd>{{ product.sugars_100|default_if_none:'—' }} g</dd>
<dt>Fibre</dt><dd>{{ product.fibre_100|default_if_none:'—' }} g</dd>
<dt>Salt</dt><dd>{{ product.salt_100|default_if_none:'—' }} g</dd>
</dl></details>
{% if product.source == 'openfoodfacts' %}<p class="food-attribution">Product data from <a href="https://world.openfoodfacts.org/product/{{ product.barcode }}" rel="noopener noreferrer">Open Food Facts</a>.</p>{% endif %}
</section>
<form method="post" class="food-card" hx-post="{% url 'food:add' %}" hx-target="#food-workspace" hx-swap="outerHTML">
{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="product" value="{{ product.pk }}">
{% include 'food/partials/portion_fields.html' %}
<button class="button" type="submit">Log food</button>
</form>
{% else %}
<form method="get" class="food-search">
<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><input type="hidden" name="tab" value="{{ tab }}">
<label for="food-query">Search your foods</label><input id="food-query" name="q" value="{{ query }}" type="search"><button class="button" type="submit">Search</button>
</form>
<nav class="food-tabs" aria-label="Local food lists">
<a {% if tab == 'overview' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=overview">For you</a>
<a {% if tab == 'recent' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=recent&amp;q={{ query|urlencode }}">Recent</a>
<a {% if tab == 'favourites' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=favourites&amp;q={{ query|urlencode }}">Favourites</a>
<a {% if tab == 'mine' %}aria-current="page"{% endif %} href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;tab=mine&amp;q={{ query|urlencode }}">My foods</a>
</nav>
{% if tab == 'overview' and not query %}
<div id="food-favourites-list"><h2>Favourites</h2>{% include 'food/partials/product_list.html' with products=favourites %}</div>
<div id="food-recents-list"><h2>Recent</h2>{% include 'food/partials/product_list.html' with products=recents %}</div>
{% else %}{% include 'food/partials/product_list.html' with products=products %}{% endif %}
<div class="food-actions"><a class="button" href="{% url 'food:manual' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Create a food manually</a>
<a class="button" href="{% url 'food:scanner' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Scan barcode</a>
<a class="button button--secondary" href="{% url 'food:external-search' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;q={{ query|urlencode }}">Search Open Food Facts</a></div>
<form method="post" action="{% url 'food:import-product' %}" class="food-barcode-form">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><label for="food-barcode">Barcode</label><input id="food-barcode" name="barcode" inputmode="numeric" pattern="[0-9]+" required><button class="button button--secondary" type="submit">Look up barcode</button></form>
<p class="food-attribution">External product data from <a href="https://world.openfoodfacts.org/" rel="noopener noreferrer">Open Food Facts</a>.</p>
{% endif %}
</div>
{% if product %}<script src="{% static 'food/portions.js' %}" defer></script>{% endif %}
{% if not product %}<nav class="food-mobile-switch" aria-label="Add food method"><span aria-current="page">Search</span><a href="{% url 'food:scanner' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Barcode</a></nav>{% endif %}
@@ -13,6 +13,7 @@
<aside class="food-sidebar" aria-label="Daily nutrition summary">
<section class="food-card" id="food-summary"><h2>Calories</h2>{% if calorie_goal is not None %}<p class="food-remaining">{% if calorie_remaining < 0 %}{{ calorie_remaining|floatformat:0|cut:'-' }} kcal over goal{% else %}{{ calorie_remaining|floatformat:0 }} kcal remaining{% endif %}</p><div class="food-progress" role="progressbar" aria-label="Calories eaten" aria-valuenow="{{ totals.calories_kcal|floatformat:0 }}" aria-valuemin="0" aria-valuemax="{{ calorie_goal }}"><span style="width:{{ calorie_percent }}%"></span></div><p>{{ totals.calories_kcal|floatformat:0 }} kcal eaten · Goal: {{ calorie_goal }} kcal</p>{% else %}<p class="food-remaining">{{ totals.calories_kcal|floatformat:0 }} kcal eaten</p><p>No calorie goal. <a href="{% url 'planner:user-settings' %}">Set one in Settings</a>.</p>{% endif %}</section>
<section class="food-macros" id="food-macro-summary" aria-label="Macronutrients">{% for macro in macros %}<article class="food-card food-macro food-macro--{{ macro.color }}"><div class="food-ring" {% if macro.percent is not None %}style="--progress:{{ macro.percent }}%"{% endif %}><span>{% if macro.percent is not None %}{{ macro.percent }}%{% else %}—{% endif %}</span></div><div><h3>{{ macro.label }}</h3><p>{{ macro.eaten|floatformat:1 }} g eaten</p>{% if macro.goal is not None %}<p>{% if macro.remaining < 0 %}{{ macro.remaining|floatformat:1|cut:'-' }} g over target{% else %}{{ macro.remaining|floatformat:1 }} g remaining{% endif %}</p>{% else %}<p>No target</p>{% endif %}</div></article>{% endfor %}</section>
<section class="food-card food-export" aria-labelledby="food-export-title"><h2 id="food-export-title">Export food diary</h2><p>Download your logged entries as CSV.</p><div class="food-export-actions"><a href="{% url 'food:export' 'week' %}">Current week CSV</a><a href="{% url 'food:export' 'month' %}">Current month CSV</a></div></section>
</aside>
</div>
</div>
@@ -0,0 +1,15 @@
{% load static %}
<div id="food-workspace" class="food-form-page">
<a href="{% url 'food:diary' %}?date={{ entry.date|date:'Y-m-d' }}">← Back to diary</a>
<h1>{{ entry.product_name_snapshot }}</h1>
{% if entry.brand_snapshot %}<p>{{ entry.brand_snapshot }}</p>{% endif %}
<div class="food-card food-edit-nutrition"><p>{% if entry.calories_kcal is not None %}{{ entry.calories_kcal|floatformat:0 }} kcal{% else %}— kcal{% endif %} · {% if entry.carbohydrates_g is not None %}{{ entry.carbohydrates_g|floatformat:-1 }}{% else %}—{% endif %} g carbs · {% if entry.protein_g is not None %}{{ entry.protein_g|floatformat:-1 }}{% else %}—{% endif %} g protein · {% if entry.fat_g is not None %}{{ entry.fat_g|floatformat:-1 }}{% else %}—{% endif %} g fat</p>
<details><summary>Nutrition information</summary><dl><dt>Fibre</dt><dd>{{ entry.fibre_g|default_if_none:'—' }} g</dd><dt>Sugars</dt><dd>{{ entry.sugars_g|default_if_none:'—' }} g</dd><dt>Salt</dt><dd>{{ entry.salt_g|default_if_none:'—' }} g</dd></dl></details>
</div>
<form method="post" class="food-card food-entry-form" hx-post="{% url 'food:edit' entry.pk %}" hx-target="#food-workspace" hx-swap="outerHTML">{% csrf_token %}
{% include 'food/partials/portion_fields.html' %}
<button class="button" type="submit">Save changes</button>
</form>
<form method="post" action="{% url 'food:delete' entry.pk %}" onsubmit="return confirm('Delete this food entry?')" hx-post="{% url 'food:delete' entry.pk %}" hx-target="#food-workspace" hx-swap="outerHTML">{% csrf_token %}<button type="submit" class="button button--secondary">Delete entry</button></form>
</div>
{% if product %}<script src="{% static 'food/portions.js' %}" defer></script>{% endif %}
@@ -0,0 +1,4 @@
<form method="post" action="{% url 'food:toggle-favourite' product.pk %}" class="food-favourite-form" hx-post="{% url 'food:toggle-favourite' product.pk %}" hx-target="{% if is_list and tab == 'favourites' and is_favourite %}closest li{% else %}this{% endif %}" hx-swap="outerHTML">
{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><input type="hidden" name="tab" value="{{ tab }}"><input type="hidden" name="is_list" value="{% if is_list %}1{% else %}0{% endif %}">
<button type="submit" class="food-favourite" aria-label="{% if is_favourite %}Remove {{ product.product_name }} from favourites{% else %}Add {{ product.product_name }} to favourites{% endif %}" aria-pressed="{% if is_favourite %}true{% else %}false{% endif %}" title="{% if is_favourite %}Remove from favourites{% else %}Add to favourites{% endif %}">{% if is_favourite %}♥{% else %}♡{% endif %}</button>
</form>
@@ -0,0 +1,3 @@
{% include 'food/partials/favourite_button.html' %}
<div id="food-favourites-list" hx-swap-oob="outerHTML"><h2>Favourites</h2>{% include 'food/partials/product_list.html' with products=favourites %}</div>
<div id="food-recents-list" hx-swap-oob="outerHTML"><h2>Recent</h2>{% include 'food/partials/product_list.html' with products=recents %}</div>
@@ -0,0 +1,15 @@
<div class="form-field">{{ form.meal.label_tag }}{{ form.meal }}{{ form.meal.errors }}</div>
{% if product %}
<div class="form-field"><label for="food-portion-choice">Portion</label><select id="food-portion-choice" name="portion" data-food-portion-choice>{% for option in portion_options %}<option value="{{ option.value }}" data-amount="{{ option.amount }}" {% if form.portion.value == option.value %}selected{% endif %}>{{ option.label }}</option>{% endfor %}</select>{{ form.portion.errors }}</div>
<div class="form-field" data-food-amount-field>{{ form.amount.label_tag }}{{ form.amount }}{{ form.amount.errors }}<small>{{ product.base_unit }}</small></div><input type="hidden" name="unit" value="{{ product.base_unit }}">
<div data-food-custom-fields><div class="form-field">{{ form.portion_label_snapshot.label_tag }}{{ form.portion_label_snapshot }}{{ form.portion_label_snapshot.errors }}</div>
<div class="settings-switch-row"><label class="settings-switch"><span class="settings-switch__copy"><strong>Save custom amount as a portion</strong><small>Use this amount again without typing it.</small></span>{{ form.save_portion }}<span class="settings-switch__track" aria-hidden="true"></span></label>{{ form.save_portion.errors }}</div>
<div data-food-save-fields><div class="form-field">{{ form.new_portion_label.label_tag }}{{ form.new_portion_label }}{{ form.new_portion_label.errors }}</div>
<div class="settings-switch-row"><label class="settings-switch"><span class="settings-switch__copy"><strong>Use this portion by default</strong><small>Preselect it next time you log this food.</small></span>{{ form.make_default }}<span class="settings-switch__track" aria-hidden="true"></span></label>{{ form.make_default.errors }}</div>
</div>
</div>
{% else %}
<div class="form-field">{{ form.amount.label_tag }}{{ form.amount }}{{ form.amount.errors }}</div>
<div class="form-field">{{ form.unit.label_tag }}{{ form.unit }}{{ form.unit.errors }}</div>
<div class="form-field">{{ form.portion_label_snapshot.label_tag }}{{ form.portion_label_snapshot }}{{ form.portion_label_snapshot.errors }}</div>
{% endif %}
@@ -0,0 +1,5 @@
<ul class="food-product-list">{% for item in products %}
<li><a href="{% url 'food:add' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}&amp;product={{ item.pk }}">
{% if item.image_url %}<img src="{{ item.image_url }}" alt="" loading="lazy">{% endif %}<span><strong>{{ item.product_name }}</strong>{% if item.brand %}<small>{{ item.brand }}</small>{% endif %}<small>{% if item.energy_kcal_100 is not None %}{{ item.energy_kcal_100|floatformat:0 }} kcal / 100 {{ item.base_unit }}{% else %}— kcal / 100 {{ item.base_unit }}{% endif %}</small></span>
</a>{% include 'food/partials/favourite_button.html' with product=item is_favourite=item.is_favourite is_list=True %}</li>
{% empty %}<li class="food-list-empty">No foods in this list yet.</li>{% endfor %}</ul>