adding food logging, phase 3

This commit is contained in:
Joren de Graaf
2026-10-03 20:53:54 +02:00
parent fca2ef2acd
commit 03fcbcbafc
8 changed files with 413 additions and 4 deletions
+47 -1
View File
@@ -1,3 +1,49 @@
{% extends 'planner/base.html' %}{% load static %}
{% 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></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 }}"><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><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 }}">{{ item.product_name }}{% if item.brand %} · {{ item.brand }}{% endif %}</a></li>{% empty %}<li>No local foods yet.</li>{% endfor %}</ul><a class="button" href="{% url 'food:manual' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">Create a food manually</a>{% endif %}</div>{% 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 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>
{% endblock %}
@@ -0,0 +1,12 @@
{% extends 'planner/base.html' %}{% load static %}
{% block title %}Search Open Food Facts · 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 }}">← Saved foods</a>
<h1>Search Open Food Facts</h1>
<p>Search the external catalogue only when you press the button. Saved foods remain available if it is offline.</p>
<form method="post" class="food-search">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><label for="external-query">Product name</label><input id="external-query" type="search" name="q" value="{{ query }}" required><button class="button" type="submit">Search Open Food Facts</button></form>
{% if notice %}<p role="status">{{ notice }} Recent, favourite, and saved products are still available.</p>{% endif %}
{% if results %}<ul class="food-product-list">{% for item in results %}<li><form method="post" action="{% url 'food:import-product' %}">{% csrf_token %}<input type="hidden" name="date" value="{{ day|date:'Y-m-d' }}"><input type="hidden" name="meal" value="{{ meal }}"><input type="hidden" name="barcode" value="{{ item.barcode }}"><button class="food-result-button" type="submit">{% 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></button></form></li>{% endfor %}</ul>{% elif request.method == 'POST' and not notice %}<p>No matching products found. You can <a href="{% url 'food:manual' %}?date={{ day|date:'Y-m-d' }}&amp;meal={{ meal }}">create a food manually</a>.</p>{% endif %}
<p class="food-attribution">Product data from <a href="https://world.openfoodfacts.org/" rel="noopener noreferrer">Open Food Facts</a>.</p>
</div>{% endblock %}