Add user dashboard and per-user food item ownership

Introduces a user dashboard route and template, moving dashboard logic to a user blueprint. FoodItem now has an owner_id field and a unique constraint on (barcode, owner_id), with relationships set up in the User model. Updates food item creation to associate with the current user, and adds a utility script for dropping a temporary table.
This commit is contained in:
2025-06-29 11:24:34 +02:00
parent 36b189c689
commit 970d102831
7 changed files with 98 additions and 7 deletions

View File

@@ -0,0 +1,20 @@
from flask import Blueprint, redirect, url_for, render_template
from flask_login import current_user
user_bp = Blueprint(
"user",
__name__,
template_folder="templates",
)
@user_bp.before_request
def login_required():
if not current_user.is_authenticated:
return redirect(url_for("login"))
@user_bp.route("/dashboard", methods=["GET"])
def dashboard():
items = current_user.food_items.all()
return render_template("dashboard.html", items=items)

View File

@@ -0,0 +1,47 @@
{% extends "base.html" %}
{% block title %}
Food Nutritional Info
{% endblock %}
{% block content %}
<div class="container mt-5">
<h1 class="mb-4">Food Nutritional Information (per 100g/100ml)</h1>
<div class="table-responsive">
<table class="table table-bordered table-hover align-middle">
<thead class="table-dark">
<tr>
<th>Name</th>
<th>Energy (kcal)</th>
<th>Fats (g)</th>
<th>Saturated Fats (g)</th>
<th>Sugars (g)</th>
<th>Carbs (g)</th>
<th>Protein (g)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for food in items %}
<tr>
<td class="bg-body-tertiary">{{ food.name }}</td>
<td class="bg-body-tertiary">{{ food.energy_100g }}</td>
<td class="bg-body-tertiary">{{ food.fats_100g }}</td>
<td class="bg-body-tertiary">{{ food.saturated_fats_100g }}</td>
<td class="bg-body-tertiary">{{ food.sugar_100g }}</td>
<td class="bg-body-tertiary">{{ food.carbs_100g }}</td>
<td class="bg-body-tertiary">{{ food.protein_100g }}</td>
<td class="bg-body-tertiary">
<form method="POST" action="{{ url_for('admin.delete_food', id=food.id) }}"
onsubmit="return confirm('Are you sure you want to delete this item?');">
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock%}