diff --git a/app.py b/app.py index ab86701..9477ec1 100644 --- a/app.py +++ b/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, diff --git a/application/admin/routes.py b/application/admin/routes.py index 47bb3f1..772e917 100644 --- a/application/admin/routes.py +++ b/application/admin/routes.py @@ -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")) diff --git a/application/user/routes.py b/application/user/routes.py new file mode 100644 index 0000000..2659bfb --- /dev/null +++ b/application/user/routes.py @@ -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) diff --git a/application/user/templates/dashboard.html b/application/user/templates/dashboard.html new file mode 100644 index 0000000..2ee2348 --- /dev/null +++ b/application/user/templates/dashboard.html @@ -0,0 +1,47 @@ +{% extends "base.html" %} + +{% block title %} +Food Nutritional Info +{% endblock %} + +{% block content %} +
+

Food Nutritional Information (per 100g/100ml)

+
+ + + + + + + + + + + + + + + {% for food in items %} + + + + + + + + + + + {% endfor %} + +
NameEnergy (kcal)Fats (g)Saturated Fats (g)Sugars (g)Carbs (g)Protein (g)Actions
{{ food.name }}{{ food.energy_100g }}{{ food.fats_100g }}{{ food.saturated_fats_100g }}{{ food.sugar_100g }}{{ food.carbs_100g }}{{ food.protein_100g }} +
+ +
+
+
+
+ +{% endblock%} \ No newline at end of file diff --git a/models.py b/models.py index 730385f..20f902b 100644 --- a/models.py +++ b/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, diff --git a/seed.py b/seed.py index 900aa59..5d57fc8 100644 --- a/seed.py +++ b/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, diff --git a/temp.py b/temp.py new file mode 100644 index 0000000..a1617f1 --- /dev/null +++ b/temp.py @@ -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.")