Can now set macro target

This commit is contained in:
2025-10-10 19:50:18 +02:00
parent e934633370
commit 312eda85df
8 changed files with 163 additions and 17 deletions

View File

@@ -17,15 +17,12 @@
</li>
</ul>
<ul class="navbar-nav">
{% if current_user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('auth.logout') }}">Logout</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{{ url_for('auth.login') }}">Login</a>
<a class="nav-link" href="{{ url_for('auth.logout') }}">Set</a>
</li>
{% endif %}
<li class="nav-item">
<button id="toggleTheme" class="btn btn-outline-light">Toggle Theme</button>
</li>

View File

@@ -24,6 +24,7 @@
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="accountDropdown">
<li><a class="dropdown-item" href='{{ url_for("auth.change_pass") }}'>Change Password</a></li>
<li><a class="dropdown-item" href='{{ url_for("user.set_macros") }}'>Set macros</a></li>
<li><a class="dropdown-item" href="#">Profile</a></li>
<li>
<hr class="dropdown-divider">

View File

@@ -9,7 +9,7 @@ from flask import (
)
from flask_login import current_user
from application import db
from forms import FoodItemForm
from forms import FoodItemForm, MacroForm
from models import FoodItem, FoodLog
from datetime import datetime
from application.utils import login_required, macro_arr_to_json
@@ -117,3 +117,20 @@ def remove_log(id: int):
db.session.delete(log)
db.session.commit()
return redirect(url_for("user.daily_log"))
@user_bp.route("/set_macros", methods=["GET", "POST"])
def set_macros():
form = MacroForm()
if form.validate_on_submit():
current_user.set_macros(
form.protein.data,
form.carbohydrates.data,
form.fat.data,
form.calories.data,
)
db.session.commit()
return redirect(url_for("user.daily_log"))
return render_template("settings.html", form=form)

View File

@@ -0,0 +1,30 @@
{% extends "base.html"%}
{% block title %} Daily Macro Settings {% endblock %}
{% block content %}
<div class="container py-5">
<h2 class="mb-4 text-center">Set Your Daily Macro Targets</h2>
<form action="{{ url_for('user.set_macros') }}" method="POST" class="card p-4 shadow-sm bg-body-secondary">
{{ form.hidden_tag() }}
<div class="mb-3">
<label for="protein" class="form-label">Protein (g)</label>
{{form.protein(class="form-control")}}
</div>
<div class="mb-3">
<label for="carbs" class="form-label">Carbohydrates (g)</label>
{{form.carbohydrates(class="form-control")}}
</div>
<div class="mb-3">
<label for="fat" class="form-label">Fat (g)</label>
{{form.fat(class="form-control")}}
</div>
<div class="mb-3">
<label for="calories" class="form-label">Calories (kcal)</label>
{{form.calories(class="form-control")}}
</div>
{{form.submit(class="btn btn-primary")}}
</form>
</div>
{% endblock %}

View File

@@ -41,9 +41,11 @@ def macro_arr_to_json(data: list[float]):
{
"name": "Calories",
"current": cal,
"target": 2000,
"bar_width": 100 - abs(cal / 20 - 100),
"bar_width_overflow": max(0, cal / 20 - 100),
"target": current_user.calories,
"bar_width": 100 - abs(cal * 100 / current_user.calories - 100),
"bar_width_overflow": max(
0, cal * 100 / current_user.calories - 100
),
"unit": " kcal",
"color": "bg-calories",
"overflow_color": "bg-calories-dark",
@@ -51,9 +53,11 @@ def macro_arr_to_json(data: list[float]):
{
"name": "Protein",
"current": pro,
"target": 150,
"bar_width": 100 - abs(pro / 1.5 - 100),
"bar_width_overflow": max(0, pro / 1.5 - 100),
"target": current_user.protein,
"bar_width": 100 - abs(pro * 100 / current_user.protein - 100),
"bar_width_overflow": max(
0, pro * 100 / current_user.protein - 100
),
"unit": "g",
"color": "bg-protein",
"overflow_color": "bg-protein-dark",
@@ -61,9 +65,12 @@ def macro_arr_to_json(data: list[float]):
{
"name": "Carbs",
"current": car,
"target": 250,
"bar_width": 100 - abs(car / 2.5 - 100),
"bar_width_overflow": max(0, car / 2.5 - 100),
"target": current_user.carbohydrates,
"bar_width": 100
- abs(car * 100 / current_user.carbohydrates - 100),
"bar_width_overflow": max(
0, car * 100 / current_user.carbohydrates - 100
),
"unit": "g",
"color": "bg-carbs",
"overflow_color": "bg-carbs-dark",
@@ -71,9 +78,9 @@ def macro_arr_to_json(data: list[float]):
{
"name": "Fat",
"current": fat,
"target": 70,
"bar_width": 100 - abs(fat / 0.7 - 100),
"bar_width_overflow": max(0, fat / 0.7 - 100),
"target": current_user.fat,
"bar_width": 100 - abs(fat * 100 / current_user.fat - 100),
"bar_width_overflow": max(0, fat * 100 / current_user.fat - 100),
"unit": "g",
"color": "bg-fat",
"overflow_color": "bg-fat-dark",