diff --git a/app.py b/app.py index 25efcea..359383d 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import render_template, redirect, url_for +from flask import render_template, redirect, url_for, request, jsonify from flask_login import ( login_required, logout_user, @@ -6,7 +6,7 @@ from flask_login import ( current_user, ) from forms import LoginForm -from models import User +from models import User, FoodItem from application import db, app, login_manager from application.admin.routes import admin_bp @@ -43,8 +43,9 @@ def login(): user = User.query.filter_by(username=form.username.data).first() if user and user.check_password(password=form.password.data): # User found and password correct - login_user(user) - return redirect(url_for("dashboard")) + next_page = request.args.get("next") # Get next page if given + login_user(user) # Log in the user + return redirect(next_page or url_for("dashboard")) else: pass # invalid user @@ -64,6 +65,22 @@ def logout(): return redirect(url_for("index")) +@app.route("/scan") +@login_required +def scan(): + return render_template("scan.html") + + +@app.route("/nutri/", methods=["GET"]) +@login_required +def nutri(barcode): + food = FoodItem.query.filter_by(barcode=barcode).first() + if food: + return jsonify(food.to_dict()) + else: + return jsonify({}) + + # Run if __name__ == "__main__": diff --git a/application/templates/scan.html b/application/templates/scan.html new file mode 100644 index 0000000..1ae4155 --- /dev/null +++ b/application/templates/scan.html @@ -0,0 +1,108 @@ +{% extends "base.html" %} + +{% block content %} +
+
+ + + + + + +{% endblock %} \ No newline at end of file diff --git a/models.py b/models.py index 42c0b92..cb2e4ba 100644 --- a/models.py +++ b/models.py @@ -36,6 +36,7 @@ class Unit(db.Model): class FoodItem(db.Model): __tablename__ = "food_item" id = db.Column(db.Integer, primary_key=True) + barcode = db.Column(db.Integer) name = db.Column(db.String(150), unique=True, nullable=False) energy_100g = db.Column(db.Integer, nullable=False) @@ -52,8 +53,9 @@ class FoodItem(db.Model): protein: float, carbs: int, fats: int, - sugar: Optional[int] = False, - saturated_fats: Optional[int] = False, + sugar: Optional[int] = None, + saturated_fats: Optional[int] = None, + barcode: Optional[int] = None, ): self.name = name self.energy_100g = energy @@ -62,3 +64,17 @@ class FoodItem(db.Model): self.sugar_100g = sugar self.fats_100g = fats self.saturated_fats_100g = saturated_fats + self.barcode = barcode + + def to_dict(self): + return { + "id": self.id, + "barcode": self.barcode, + "name": self.name, + "energy_100g": self.energy_100g, + "protein_100g": self.protein_100g, + "carbs_100g": self.carbs_100g, + "sugar_100g": self.sugar_100g, + "fats_100g": self.fats_100g, + "saturated_fats_100g": self.saturated_fats_100g, + } diff --git a/seed.py b/seed.py index d7617b4..900aa59 100644 --- a/seed.py +++ b/seed.py @@ -16,6 +16,7 @@ with app.app_context(): protein=5.5, saturated_fats=10, sugar=35, + barcode=2278012003502, ) )