7 Commits

Author SHA1 Message Date
fda5f8e17b Update daily_log.html
Added a confirm button
2025-08-11 16:56:51 +02:00
18fa0fdb2d Remove admin food item and barcode test routes and templates
Deleted the /food_items and /barcode_test routes from admin, along with their associated templates and the delete_food functionality. This streamlines the admin blueprint by removing unused or deprecated features.
2025-08-11 16:56:40 +02:00
4d34049850 Change energy_100 column type to Float in food_item
This migration alters the 'energy_100' column in the 'food_item' table from INTEGER to Float to allow for decimal values. The downgrade reverses this change.
2025-08-11 16:52:57 +02:00
3f9bd8984d Change energy field from integer to float
Updated the FoodItem model and FoodItemForm to use float for the energy field instead of integer.
2025-08-11 16:51:31 +02:00
5a0dbef28f Update forms.py
added input mode to form to indicate a decimal value is expected for each value
2025-08-11 16:50:19 +02:00
93406db07e Update add_item.html
Changed the order of macros when adding new item
2025-08-11 16:43:00 +02:00
72fe1b602b Update routes.py
Fixed a bug where redirect was incorrect after adding an item
2025-08-11 16:36:51 +02:00
9 changed files with 81 additions and 145 deletions

View File

@@ -117,6 +117,7 @@ def step3_alt1_post(input: str):
)
db.session.commit()
print("[DEBUG] New FoodItem Added")
input = barcode if barcode else name # update input
else:
print(f"Item exists: {item.barcode} {item.name}")

View File

@@ -20,8 +20,13 @@
</div>
<div class="mb-3">
{{ form.protein.label(class="form-label") }}
{{ form.protein(class="form-control") }}
{{ form.fat.label(class="form-label") }}
{{ form.fat(class="form-control") }}
</div>
<div class="mb-3">
{{ form.saturated_fat.label(class="form-label") }}
{{ form.saturated_fat(class="form-control") }}
</div>
<div class="mb-3">
@@ -35,13 +40,8 @@
</div>
<div class="mb-3">
{{ form.fat.label(class="form-label") }}
{{ form.fat(class="form-control") }}
</div>
<div class="mb-3">
{{ form.saturated_fat.label(class="form-label") }}
{{ form.saturated_fat(class="form-control") }}
{{ form.protein.label(class="form-label") }}
{{ form.protein(class="form-control") }}
</div>
{{ form.submit(class="btn btn-primary") }}

View File

@@ -1,7 +1,5 @@
from flask import Blueprint, render_template, abort, redirect, url_for
from flask import Blueprint, abort
from flask_login import current_user
from models import FoodItem
from application import db
admin_bp = Blueprint(
"admin",
@@ -15,24 +13,3 @@ admin_bp = Blueprint(
def admin_required():
if not current_user.is_admin:
abort(403)
@admin_bp.route("/food_items", methods=["GET"])
def food_items():
items = FoodItem.query.all()
return render_template("food_items.html", items=items)
@admin_bp.route("/barcode_test", methods=["GET"])
def barcode_test():
return render_template("barcode_test.html")
@admin_bp.route("/delete_food/<int:id>", methods=["POST"])
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"))

View File

@@ -1,55 +0,0 @@
{% extends "base.html" %}
{% block title %}
ZXing Barcode Scanner
{% endblock %}
{% block content %}
<div class="container text-center">
<h1 class="mb-4">📷 ZXing Barcode Scanner</h1>
<div class="mb-3">
<video id="video" class="border rounded shadow-sm" width="100%" style="max-width: 500px;"></video>
</div>
<div class="mb-3">
<button id="startButton" class="btn btn-primary">Start Scanning</button>
<button id="stopButton" class="btn btn-danger ms-2">Stop</button>
</div>
<div>
<h5>Result:</h5>
<p id="result" class="fw-bold text-success"></p>
</div>
</div>
<script type="module">
import { BrowserMultiFormatReader } from 'https://cdn.jsdelivr.net/npm/@zxing/library@0.21.3/+esm';
const codeReader = new BrowserMultiFormatReader();
const videoElement = document.getElementById('video');
const resultElement = document.getElementById('result');
document.getElementById('startButton').addEventListener('click', async () => {
await navigator.mediaDevices.getUserMedia({ video: true });
console.log('[DEBUG] Start button clicked');
const devices = await codeReader.listVideoInputDevices();
console.log('[DEBUG] Cameras found:', devices);
const selectedDeviceId = devices[0]?.deviceId;
if (!selectedDeviceId) {
alert('No camera found!');
return;
}
codeReader.decodeFromVideoDevice(selectedDeviceId, videoElement, (result, err, controls) => {
if (result) {
resultElement.textContent = result.getText();
controls.stop();
}
});
});
document.getElementById('stopButton').addEventListener('click', () => {
codeReader.reset();
resultElement.textContent = '';
});
</script>
{% endblock %}

View File

@@ -1,47 +0,0 @@
{% 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>fat (g)</th>
<th>Saturated fat (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_100 }}</td>
<td class="bg-body-tertiary">{{ food.fat_100 }}</td>
<td class="bg-body-tertiary">{{ food.saturated_fat_100 }}</td>
<td class="bg-body-tertiary">{{ food.sugar_100 }}</td>
<td class="bg-body-tertiary">{{ food.carbs_100 }}</td>
<td class="bg-body-tertiary">{{ food.protein_100 }}</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%}

View File

@@ -196,7 +196,8 @@ Food Nutritional Info
{{ "{:g}".format(log.amount) }}
</div>
<div class="col-auto text-end align-self-start" style="min-width: 80px;">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline">
<form method="POST" action="{{url_for('user.remove_log', id=log.id)}}" class="d-inline"
onsubmit="return confirm('Are you sure you want to delete this item?');">
<button type="submit" class="btn btn-sm btn-danger px-3 py-1"
title="Delete">&times;</button>
</form>

View File

@@ -3,7 +3,6 @@ from wtforms import (
StringField,
PasswordField,
SubmitField,
IntegerField,
FloatField,
)
from wtforms.validators import DataRequired, InputRequired, Optional
@@ -18,13 +17,35 @@ class LoginForm(FlaskForm):
class FoodItemForm(FlaskForm):
barcode = StringField("Barcode", validators=[Optional()])
name = StringField("Product Name", validators=[DataRequired()])
energy = IntegerField("Energy per 100g", validators=[InputRequired()])
protein = FloatField("protein per 100g", validators=[InputRequired()])
carbs = FloatField("carbs per 100g", validators=[InputRequired()])
sugar = FloatField("sugar per 100g", validators=[Optional()])
fat = FloatField("fat per 100g", validators=[InputRequired()])
energy = FloatField(
"Energy per 100g",
validators=[InputRequired()],
render_kw={"inputmode": "decimal"},
)
protein = FloatField(
"protein per 100g",
validators=[InputRequired()],
render_kw={"inputmode": "decimal"},
)
carbs = FloatField(
"carbs per 100g",
validators=[InputRequired()],
render_kw={"inputmode": "decimal"},
)
sugar = FloatField(
"sugar per 100g",
validators=[Optional()],
render_kw={"inputmode": "decimal"},
)
fat = FloatField(
"fat per 100g",
validators=[InputRequired()],
render_kw={"inputmode": "decimal"},
)
saturated_fat = FloatField(
"saturated_fat per 100g", validators=[Optional()]
"saturated_fat per 100g",
validators=[Optional()],
render_kw={"inputmode": "decimal"},
)
submit = SubmitField("Add Item")

View File

@@ -0,0 +1,38 @@
"""empty message
Revision ID: dea130d45cec
Revises: f5fbbe915d51
Create Date: 2025-08-11 16:51:55.485569
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'dea130d45cec'
down_revision = 'f5fbbe915d51'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('food_item', schema=None) as batch_op:
batch_op.alter_column('energy_100',
existing_type=sa.INTEGER(),
type_=sa.Float(),
existing_nullable=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('food_item', schema=None) as batch_op:
batch_op.alter_column('energy_100',
existing_type=sa.Float(),
type_=sa.INTEGER(),
existing_nullable=False)
# ### end Alembic commands ###

View File

@@ -45,7 +45,7 @@ class FoodItem(db.Model):
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
name = db.Column(db.String(150), nullable=False)
energy_100 = db.Column(db.Integer, nullable=False)
energy_100 = db.Column(db.Float, nullable=False)
protein_100 = db.Column(db.Float, nullable=False)
carbs_100 = db.Column(db.Float, nullable=False)
sugar_100 = db.Column(db.Float)
@@ -68,7 +68,7 @@ class FoodItem(db.Model):
self,
name: str,
owner_id: int,
energy: int,
energy: float,
protein: float,
carbs: float,
fat: float,