mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-29 19:00:00 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fda5f8e17b | |||
| 18fa0fdb2d | |||
| 4d34049850 | |||
| 3f9bd8984d | |||
| 5a0dbef28f | |||
| 93406db07e | |||
| 72fe1b602b |
@@ -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}")
|
||||
|
||||
|
||||
@@ -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") }}
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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 %}
|
||||
@@ -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%}
|
||||
@@ -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">×</button>
|
||||
</form>
|
||||
|
||||
35
forms.py
35
forms.py
@@ -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")
|
||||
|
||||
|
||||
38
migrations/versions/dea130d45cec_.py
Normal file
38
migrations/versions/dea130d45cec_.py
Normal 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 ###
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user