mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 19:29:59 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a9e1f77c7 | ||
| 2454bc61cb | |||
| 85297daaaf |
1
app.py
1
app.py
@@ -5,7 +5,6 @@ from flask import (
|
|||||||
)
|
)
|
||||||
from flask_login import (
|
from flask_login import (
|
||||||
login_required,
|
login_required,
|
||||||
logout_user,
|
|
||||||
current_user,
|
current_user,
|
||||||
)
|
)
|
||||||
from models import User
|
from models import User
|
||||||
|
|||||||
0
application/static/css/macros.css
Normal file
0
application/static/css/macros.css
Normal file
@@ -25,6 +25,9 @@
|
|||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="{{ url_for('user.daily_log') }}">Daily Log</a>
|
<a class="nav-link" href="{{ url_for('user.daily_log') }}">Daily Log</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="{{ url_for('user.daily_log2') }}">Daily Log (new)</a>
|
||||||
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="{{ url_for('user.dashboard') }}">Dashboard</a>
|
<a class="nav-link" href="{{ url_for('user.dashboard') }}">Dashboard</a>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ from forms import FoodItemForm
|
|||||||
from models import FoodItem, FoodLog
|
from models import FoodItem, FoodLog
|
||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timezone, timedelta
|
||||||
from application.utils import login_required
|
from application.utils import login_required
|
||||||
|
from typing import cast
|
||||||
|
from numpy import array
|
||||||
|
|
||||||
user_bp = Blueprint(
|
user_bp = Blueprint(
|
||||||
"user",
|
"user",
|
||||||
@@ -21,6 +23,57 @@ user_bp = Blueprint(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def macro_arr_to_json(data: list[float]):
|
||||||
|
assert len(data) == 4
|
||||||
|
cal = data[0]
|
||||||
|
pro = data[3]
|
||||||
|
car = data[2]
|
||||||
|
fat = data[1]
|
||||||
|
macros = [
|
||||||
|
{
|
||||||
|
"name": "Calories",
|
||||||
|
"current": cal,
|
||||||
|
"target": 2000,
|
||||||
|
"bar_width": 100 - abs(cal / 20 - 100),
|
||||||
|
"bar_width_overflow": max(0, cal / 20 - 100),
|
||||||
|
"unit": " kcal",
|
||||||
|
"color": "bg-calories",
|
||||||
|
"overflow_color": "bg-calories-dark",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Protein",
|
||||||
|
"current": pro,
|
||||||
|
"target": 150,
|
||||||
|
"bar_width": 100 - abs(pro / 1.5 - 100),
|
||||||
|
"bar_width_overflow": max(0, pro / 1.5 - 100),
|
||||||
|
"unit": "g",
|
||||||
|
"color": "bg-protein",
|
||||||
|
"overflow_color": "bg-protein-dark",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Carbs",
|
||||||
|
"current": car,
|
||||||
|
"target": 250,
|
||||||
|
"bar_width": 100 - abs(car / 2.5 - 100),
|
||||||
|
"bar_width_overflow": max(0, car / 2.5 - 100),
|
||||||
|
"unit": "g",
|
||||||
|
"color": "bg-carbs",
|
||||||
|
"overflow_color": "bg-carbs-dark",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Fat",
|
||||||
|
"current": fat,
|
||||||
|
"target": 70,
|
||||||
|
"bar_width": 100 - abs(fat / 0.7 - 100),
|
||||||
|
"bar_width_overflow": max(0, fat / 0.7 - 100),
|
||||||
|
"unit": "g",
|
||||||
|
"color": "bg-fat",
|
||||||
|
"overflow_color": "bg-fat-dark",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
return macros
|
||||||
|
|
||||||
|
|
||||||
user_bp.before_request(login_required)
|
user_bp.before_request(login_required)
|
||||||
|
|
||||||
|
|
||||||
@@ -100,6 +153,18 @@ def daily_log(offset: int = 0):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@user_bp.route("/daily_log2", methods=["GET"])
|
||||||
|
def daily_log2():
|
||||||
|
today = datetime.now(timezone.utc).date()
|
||||||
|
logs_today = current_user.food_logs.filter_by(date_=today).all()
|
||||||
|
macros = array((0.0, 0.0, 0.0, 0.0))
|
||||||
|
for log in logs_today:
|
||||||
|
item = cast(FoodItem, log.food_item)
|
||||||
|
macros += array(item.macros()) / 100 * log.amount
|
||||||
|
macros = macro_arr_to_json(macros.tolist())
|
||||||
|
return render_template("daily_log2.html", macros=macros, logs=logs_today)
|
||||||
|
|
||||||
|
|
||||||
@user_bp.route("/remove_log/<int:id>", methods=["POST"])
|
@user_bp.route("/remove_log/<int:id>", methods=["POST"])
|
||||||
def remove_log(id: int):
|
def remove_log(id: int):
|
||||||
log = db.session.get(FoodLog, id)
|
log = db.session.get(FoodLog, id)
|
||||||
|
|||||||
67
application/user/templates/daily_log2.html
Normal file
67
application/user/templates/daily_log2.html
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
|
||||||
|
{% block title %}Daily Calorie Dashboard{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='css/macros.css') }}">
|
||||||
|
|
||||||
|
<div class="container my-4">
|
||||||
|
<h2 class="mb-3">Daily Calorie Dashboard</h2>
|
||||||
|
|
||||||
|
<!-- Macro Summary -->
|
||||||
|
<div class="card p-3 mb-3">
|
||||||
|
<h5>Macros</h5>
|
||||||
|
{% for macro in macros %}
|
||||||
|
<div class="mb-2">
|
||||||
|
<span class="macro-text">{{ macro.name }}: {{ macro.current }} / {{ macro.target }}</span>
|
||||||
|
<div class="progress rounded" style="height: 24px;">
|
||||||
|
<div class="progress-bar bg-danger macro-bar" role="progressbar"
|
||||||
|
style="width: {{ macro.bar_width_overflow }}%">
|
||||||
|
{{ macro.current - macro.target }}{{ macro.unit }}
|
||||||
|
</div>
|
||||||
|
<div class="progress-bar bg-success macro-bar" role="progressbar" style="width: {{ macro.bar_width }}%">
|
||||||
|
{{ macro.current }}{{ macro.unit }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Items List -->
|
||||||
|
<div class="card p-3">
|
||||||
|
<h5>Items Eaten Today</h5>
|
||||||
|
<div class="list-group list-group-flush">
|
||||||
|
{% for log in logs %}
|
||||||
|
<div class="list-group-item item-row d-flex justify-content-between align-items-center bg-dark text-light">
|
||||||
|
<span>({{ log.amount }}g) {{ log.food_item.name }}</span>
|
||||||
|
<span>{{ log.food_item.energy_100 }} kcal</span>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Bottom Navigation Buttons -->
|
||||||
|
<div class="container-fluid fixed-bottom py-2">
|
||||||
|
<div class="d-flex p-3">
|
||||||
|
<!-- Left Button -->
|
||||||
|
<a href="" class="btn btn-outline-light flex-fill me-2 rounded-pill">
|
||||||
|
‹ Previous
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<!-- Center Button (highlighted) -->
|
||||||
|
<a href="{{ url_for('add_meal.step1', meal_type=0) }}"
|
||||||
|
class="btn btn-success flex-fill mx-2 fw-bold rounded-pill">
|
||||||
|
+ Add Item
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<!-- Right Button -->
|
||||||
|
<a href="" class="btn btn-outline-light flex-fill ms-2 rounded-pill">
|
||||||
|
Next ›
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
41
models.py
41
models.py
@@ -49,17 +49,18 @@ class Unit(db.Model):
|
|||||||
|
|
||||||
class FoodItem(db.Model):
|
class FoodItem(db.Model):
|
||||||
__tablename__ = "food_item"
|
__tablename__ = "food_item"
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
barcode = db.Column(db.String)
|
barcode = db.Column(db.String)
|
||||||
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
|
||||||
name = db.Column(db.String(150), nullable=False)
|
name = db.Column(db.String(150), nullable=False)
|
||||||
|
|
||||||
energy_100 = db.Column(db.Float, nullable=False)
|
energy_100: float = db.Column(db.Float, nullable=False)
|
||||||
protein_100 = db.Column(db.Float, nullable=False)
|
protein_100: float = db.Column(db.Float, nullable=False)
|
||||||
carbs_100 = db.Column(db.Float, nullable=False)
|
carbs_100: float = db.Column(db.Float, nullable=False)
|
||||||
sugar_100 = db.Column(db.Float)
|
sugar_100: Optional[float] = db.Column(db.Float)
|
||||||
fat_100 = db.Column(db.Float, nullable=False)
|
fat_100: float = db.Column(db.Float, nullable=False)
|
||||||
saturated_fat_100 = db.Column(db.Float)
|
saturated_fat_100: Optional[float] = db.Column(db.Float)
|
||||||
|
|
||||||
food_logs = db.relationship(
|
food_logs = db.relationship(
|
||||||
"FoodLog",
|
"FoodLog",
|
||||||
@@ -99,26 +100,20 @@ class FoodItem(db.Model):
|
|||||||
|
|
||||||
def updateFromForm(self, form: FoodItemForm):
|
def updateFromForm(self, form: FoodItemForm):
|
||||||
self.name = form.name.data
|
self.name = form.name.data
|
||||||
self.energy_100 = form.energy.data
|
self.energy_100 = form.energy.data or 0
|
||||||
self.protein_100 = form.protein.data
|
self.protein_100 = form.protein.data or 0
|
||||||
self.carbs_100 = form.carbs.data
|
self.carbs_100 = form.carbs.data or 0
|
||||||
self.sugar_100 = form.sugar.data
|
self.sugar_100 = form.sugar.data
|
||||||
self.fat_100 = form.fat.data
|
self.fat_100 = form.fat.data or 0
|
||||||
self.saturated_fat_100 = form.saturated_fat.data
|
self.saturated_fat_100 = form.saturated_fat.data
|
||||||
|
|
||||||
def to_dict(self):
|
def macros(self) -> tuple[float, float, float, float]:
|
||||||
return {
|
return (
|
||||||
"id": self.id,
|
self.energy_100,
|
||||||
"barcode": self.barcode,
|
self.fat_100,
|
||||||
"name": self.name,
|
self.carbs_100,
|
||||||
"owner_id": self.owner_id,
|
self.protein_100,
|
||||||
"energy_100": self.energy_100,
|
)
|
||||||
"protein_100": self.protein_100,
|
|
||||||
"carbs_100": self.carbs_100,
|
|
||||||
"sugar_100": self.sugar_100,
|
|
||||||
"fat_100": self.fat_100,
|
|
||||||
"saturated_fat_100": self.saturated_fat_100,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class FoodLog(db.Model):
|
class FoodLog(db.Model):
|
||||||
|
|||||||
Reference in New Issue
Block a user