mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 03:10:00 +00:00
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:
9
app.py
9
app.py
@@ -9,6 +9,7 @@ from forms import LoginForm, FoodItemForm
|
||||
from models import User, FoodItem
|
||||
from application import db, app, login_manager
|
||||
from application.admin.routes import admin_bp
|
||||
from application.user.routes import user_bp
|
||||
from typing import Optional
|
||||
|
||||
# Config
|
||||
@@ -24,6 +25,7 @@ def load_user(user_id: int):
|
||||
|
||||
# Register blueprints
|
||||
app.register_blueprint(admin_bp)
|
||||
app.register_blueprint(user_bp)
|
||||
|
||||
|
||||
# Routes
|
||||
@@ -62,12 +64,6 @@ def login():
|
||||
return render_template("login.html", form=form)
|
||||
|
||||
|
||||
@app.route("/dashboard")
|
||||
@login_required
|
||||
def dashboard():
|
||||
return render_template("dashboard.html", name=current_user.username)
|
||||
|
||||
|
||||
@app.route("/logout")
|
||||
@login_required
|
||||
def logout():
|
||||
@@ -122,6 +118,7 @@ def add_food_item():
|
||||
db.session.add(
|
||||
FoodItem(
|
||||
name=form.name.data,
|
||||
owner_id=current_user.id,
|
||||
energy=form.energy.data,
|
||||
protein=form.protein.data,
|
||||
carbs=form.carbs.data,
|
||||
|
||||
@@ -32,6 +32,7 @@ def barcode_test():
|
||||
def delete_food(id):
|
||||
item = FoodItem.query.get(id)
|
||||
if item:
|
||||
# if item.owner_id == current_user.id:
|
||||
db.session.delete(item)
|
||||
db.session.commit()
|
||||
return redirect(url_for("admin.food_items"))
|
||||
|
||||
20
application/user/routes.py
Normal file
20
application/user/routes.py
Normal 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)
|
||||
47
application/user/templates/dashboard.html
Normal file
47
application/user/templates/dashboard.html
Normal 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%}
|
||||
12
models.py
12
models.py
@@ -11,6 +11,8 @@ class User(UserMixin, db.Model):
|
||||
password = db.Column(db.String, nullable=False)
|
||||
is_admin = db.Column(db.Boolean, nullable=False, default=False)
|
||||
|
||||
food_items = db.relationship("FoodItem", lazy="dynamic")
|
||||
|
||||
def __init__(
|
||||
self, username: str, password: str, is_admin: bool = False
|
||||
) -> None:
|
||||
@@ -37,7 +39,8 @@ class FoodItem(db.Model):
|
||||
__tablename__ = "food_item"
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
barcode = db.Column(db.Integer, nullable=False)
|
||||
name = db.Column(db.String(150), unique=True, nullable=False)
|
||||
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
||||
name = db.Column(db.String(150), nullable=False)
|
||||
|
||||
energy_100g = db.Column(db.Integer, nullable=False)
|
||||
protein_100g = db.Column(db.Float, nullable=False)
|
||||
@@ -46,9 +49,14 @@ class FoodItem(db.Model):
|
||||
fats_100g = db.Column(db.Float, nullable=False)
|
||||
saturated_fats_100g = db.Column(db.Float)
|
||||
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint("barcode", "owner_id", name="barcode_owner_key"),
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
owner_id: int,
|
||||
energy: int,
|
||||
protein: float,
|
||||
carbs: float,
|
||||
@@ -58,6 +66,7 @@ class FoodItem(db.Model):
|
||||
saturated_fats: Optional[float] = None,
|
||||
):
|
||||
self.name = name
|
||||
self.owner_id = owner_id
|
||||
self.energy_100g = energy
|
||||
self.protein_100g = protein
|
||||
self.carbs_100g = carbs
|
||||
@@ -71,6 +80,7 @@ class FoodItem(db.Model):
|
||||
"id": self.id,
|
||||
"barcode": self.barcode,
|
||||
"name": self.name,
|
||||
"owner_id": self.owner_id,
|
||||
"energy_100g": self.energy_100g,
|
||||
"protein_100g": self.protein_100g,
|
||||
"carbs_100g": self.carbs_100g,
|
||||
|
||||
1
seed.py
1
seed.py
@@ -10,6 +10,7 @@ with app.app_context():
|
||||
db.session.add(
|
||||
FoodItem(
|
||||
name="AH Matcha cookie",
|
||||
owner_id=0,
|
||||
energy=430,
|
||||
fats=19,
|
||||
carbs=59,
|
||||
|
||||
15
temp.py
Normal file
15
temp.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from application import db, app
|
||||
from sqlalchemy import MetaData, Table
|
||||
|
||||
with app.app_context():
|
||||
table_name = "_alembic_tmp_food_item"
|
||||
engine = db.engine
|
||||
metadata = MetaData()
|
||||
metadata.reflect(bind=engine)
|
||||
|
||||
if table_name in metadata.tables:
|
||||
tmp_table = Table(table_name, metadata, autoload_with=engine)
|
||||
tmp_table.drop(engine)
|
||||
print(f"Table '{table_name}' dropped.")
|
||||
else:
|
||||
print(f"No table named '{table_name}' found.")
|
||||
Reference in New Issue
Block a user