mirror of
https://github.com/StefBuwalda/cal_counter.git
synced 2025-10-30 03:10:00 +00:00
Refactor add meal flow and remove v2 routes
Migrated add_meal_v2 routes and templates to add_meal, renaming endpoints and updating references throughout the app. Removed legacy daily_log2 route and template, consolidating to daily_log. Moved macro_arr_to_json to utils.py for reuse. Updated navigation and redirect logic to use new routes. Improved item finding and barcode scanning UI.
This commit is contained in:
177
application/add_meal/routes.py
Normal file
177
application/add_meal/routes.py
Normal file
@@ -0,0 +1,177 @@
|
||||
from flask import (
|
||||
Blueprint,
|
||||
redirect,
|
||||
url_for,
|
||||
render_template,
|
||||
session,
|
||||
request,
|
||||
jsonify,
|
||||
)
|
||||
from flask_login import current_user
|
||||
from forms import FoodItemForm, FoodLogForm
|
||||
from application import db
|
||||
from models import FoodItem, FoodLog
|
||||
from sqlalchemy import and_, or_
|
||||
from datetime import datetime
|
||||
from sqlalchemy.sql.elements import BinaryExpression
|
||||
from typing import cast
|
||||
|
||||
bp = Blueprint(
|
||||
"add_meal",
|
||||
__name__,
|
||||
url_prefix="/add_meal",
|
||||
template_folder="templates",
|
||||
)
|
||||
|
||||
|
||||
def date_present(func):
|
||||
def check_date():
|
||||
if "selected_date" not in session:
|
||||
return redirect(url_for("user.daily_log"))
|
||||
|
||||
|
||||
def item_selected(func):
|
||||
def check_item():
|
||||
if check_item():
|
||||
if "item_id" not in session:
|
||||
return redirect(url_for("add_meal.find_item"))
|
||||
|
||||
|
||||
@bp.before_request
|
||||
def login_required():
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(url_for("auth.login"))
|
||||
|
||||
|
||||
@date_present
|
||||
@bp.route("/find_item", methods=["GET"])
|
||||
def find_item():
|
||||
return render_template("find_item.html")
|
||||
|
||||
|
||||
@date_present
|
||||
@bp.route("/select_item/<string:input>", methods=["GET"])
|
||||
def select_item(input: str):
|
||||
# Check if input is a barcode
|
||||
if input.isdigit():
|
||||
item = current_user.food_items.filter_by(barcode=input).first()
|
||||
|
||||
else:
|
||||
item = current_user.food_items.filter_by(name=input).first()
|
||||
|
||||
if item is None:
|
||||
# Does not exist, add item
|
||||
return redirect(url_for("add_meal.add_new_item", input=input))
|
||||
|
||||
# Track item to add and continue to next step
|
||||
session["item_id"] = item.id
|
||||
return redirect(url_for("add_meal.step4"))
|
||||
|
||||
|
||||
@date_present
|
||||
@bp.route("/add_new_item/<string:input>", methods=["GET"])
|
||||
def add_new_item(input: str):
|
||||
form = FoodItemForm()
|
||||
|
||||
if input.isdigit():
|
||||
form.barcode.data = input
|
||||
else:
|
||||
form.name.data = input
|
||||
return render_template("add_new_item.html", form=form)
|
||||
|
||||
|
||||
@date_present
|
||||
@bp.route("/add_new_item/<string:input>", methods=["POST"])
|
||||
def post_new_item(input: str):
|
||||
form = FoodItemForm()
|
||||
|
||||
if form.validate_on_submit():
|
||||
# Form has valid input
|
||||
barcode = form.barcode.data
|
||||
name = form.name.data
|
||||
assert name
|
||||
assert form.energy.data is not None
|
||||
assert form.protein.data is not None
|
||||
assert form.carbs.data is not None
|
||||
assert form.fat.data is not None
|
||||
|
||||
# Check if name or barcode already exists
|
||||
name_filter = cast(BinaryExpression, FoodItem.name == name)
|
||||
barcode_filter = cast(BinaryExpression, FoodItem.barcode == barcode)
|
||||
filter_exp = or_(name_filter, barcode_filter)
|
||||
item = current_user.food_items.filter(filter_exp).first()
|
||||
|
||||
if item is None:
|
||||
# Item does not exist, add to DB
|
||||
barcode = (
|
||||
barcode if barcode else None
|
||||
) # Turn empty strings into None
|
||||
db.session.add(
|
||||
FoodItem(
|
||||
name=name,
|
||||
owner_id=current_user.id,
|
||||
energy=form.energy.data,
|
||||
protein=form.protein.data,
|
||||
carbs=form.carbs.data,
|
||||
fat=form.fat.data,
|
||||
barcode=barcode,
|
||||
saturated_fat=form.saturated_fat.data,
|
||||
sugar=form.sugar.data,
|
||||
)
|
||||
)
|
||||
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}")
|
||||
|
||||
# Item added or already present, return to step 3.
|
||||
return redirect(url_for("add_meal.select_item", input=input))
|
||||
else:
|
||||
print("[DEBUG] Form Invalid")
|
||||
return redirect(url_for("add_meal.add_new_item", input=input))
|
||||
|
||||
|
||||
@date_present
|
||||
@item_selected
|
||||
@bp.route("/step4", methods=["GET", "POST"])
|
||||
def step4():
|
||||
form = FoodLogForm()
|
||||
item = db.session.get(FoodItem, session["item_id"])
|
||||
if not item:
|
||||
return redirect(url_for("add_meal.find_item"))
|
||||
|
||||
if form.validate_on_submit():
|
||||
assert form.amount.data
|
||||
db.session.add(
|
||||
FoodLog(
|
||||
food_item_id=item.id,
|
||||
user_id=current_user.id,
|
||||
amount=form.amount.data,
|
||||
date_=datetime.strptime(
|
||||
session["selected_date"], "%Y-%m-%d"
|
||||
).date(),
|
||||
)
|
||||
)
|
||||
db.session.commit()
|
||||
session.pop("item_id")
|
||||
session.pop("selected_date")
|
||||
return redirect(url_for("user.daily_log"))
|
||||
|
||||
return render_template("step4.html", item=item, form=form)
|
||||
|
||||
|
||||
@date_present
|
||||
@bp.route("/query", methods=["GET"])
|
||||
def query():
|
||||
q = request.args.get("q", "").strip().lower()
|
||||
if not q:
|
||||
return jsonify([])
|
||||
|
||||
words = q.split()
|
||||
filters = [
|
||||
FoodItem.name.ilike(f"%{word}%") for word in words # type: ignore
|
||||
]
|
||||
|
||||
results = current_user.food_items.filter(and_(*filters)).all()
|
||||
return jsonify([item.name for item in results])
|
||||
49
application/add_meal/templates/add_new_item.html
Normal file
49
application/add_meal/templates/add_new_item.html
Normal file
@@ -0,0 +1,49 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<form method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.barcode.label(class="form-label") }}
|
||||
{{ form.barcode(class="form-control-plaintext", readonly=true) }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.name.label(class="form-label") }}
|
||||
{{ form.name(class="form-control") }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.energy.label(class="form-label") }}
|
||||
{{ form.energy(class="form-control") }}
|
||||
</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") }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.carbs.label(class="form-label") }}
|
||||
{{ form.carbs(class="form-control") }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.sugar.label(class="form-label") }}
|
||||
{{ form.sugar(class="form-control") }}
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.protein.label(class="form-label") }}
|
||||
{{ form.protein(class="form-control") }}
|
||||
</div>
|
||||
|
||||
{{ form.submit(class="btn btn-primary") }}
|
||||
</form>
|
||||
{% endblock%}
|
||||
126
application/add_meal/templates/find_item.html
Normal file
126
application/add_meal/templates/find_item.html
Normal file
@@ -0,0 +1,126 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container d-flex flex-column justify-content-start align-items-center py-4" style="min-height: 100vh;">
|
||||
<div class="card shadow-sm w-100" style="max-width: 480px;">
|
||||
<div class="card-body d-flex flex-column">
|
||||
<h5 class="card-title text-center mb-4">Item Scanner</h5>
|
||||
<video id="camera" autoplay class="w-100 mb-3" style="aspect-ratio: 4/3;" muted></video>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="manualSearch" class="form-label">Or search manually</label>
|
||||
<input type="text" class="form-control" id="manualSearch" placeholder="Enter item name">
|
||||
</div>
|
||||
|
||||
<ul class="list-group mb-3" id="searchResults"></ul>
|
||||
|
||||
<button class="btn btn-primary w-100" id="createItemBtn">Create New Item</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock%}
|
||||
|
||||
{% block scripts %}
|
||||
<script>
|
||||
const baseURL = "{{url_for('add_meal.select_item', input='!')}}";
|
||||
const baseURL2 = "{{url_for('add_meal.add_new_item', input='!')}}";
|
||||
</script>
|
||||
<script type="module">
|
||||
// Import barcode scanner
|
||||
import { BrowserMultiFormatReader } from 'https://cdn.jsdelivr.net/npm/@zxing/library@0.21.3/+esm';
|
||||
|
||||
// constants
|
||||
const codeReader = new BrowserMultiFormatReader();
|
||||
const videoElement = document.getElementById('camera');
|
||||
|
||||
// Start async camera thingymibob
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
console.log('[DEBUG] Page loaded, starting barcode scan');
|
||||
|
||||
try {
|
||||
await navigator.mediaDevices.getUserMedia({ video: true });
|
||||
} catch (err) {
|
||||
alert("No camera found or no camera permission");
|
||||
console.error("Could not access the camera:", err);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[DEBUG] Permission given and at least one device present');
|
||||
const devices = await codeReader.listVideoInputDevices();
|
||||
console.log('[DEBUG] Cameras found:', devices);
|
||||
|
||||
const rearCamera = devices.find(device => device.label.toLowerCase().includes('back'))
|
||||
|| devices.find(device => device.label.toLowerCase().includes('rear'))
|
||||
|| devices[0]; // fallback
|
||||
|
||||
const selectedDeviceId = rearCamera?.deviceId;
|
||||
await codeReader.decodeFromVideoDevice(selectedDeviceId, videoElement, async (result, err) => {
|
||||
if (result) {
|
||||
// Result found, redirect to the URL
|
||||
console.log(result)
|
||||
const codeText = result.getText();
|
||||
window.location.href = baseURL.replace("!", encodeURIComponent(codeText));
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
const searchInput = document.getElementById('manualSearch');
|
||||
const resultsList = document.getElementById('searchResults');
|
||||
const createBtn = document.getElementById('createItemBtn');
|
||||
|
||||
let controller; // keeps track of the current fetch
|
||||
|
||||
// TODO: Debounce input, wait for user to stop typing (f.e. 300ms)
|
||||
searchInput.addEventListener('input', () => {
|
||||
const query = searchInput.value.toLowerCase();
|
||||
|
||||
// Check if there is enough input to fetch
|
||||
if (query.length < 2) {
|
||||
resultsList.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
|
||||
if (controller) controller.abort(); // Abort previous fetch if still running
|
||||
controller = new AbortController(); // new controller for this fetch
|
||||
const signal = controller.signal; // Signal to fetch to listen for aborts
|
||||
|
||||
fetch(`{{url_for("add_meal.query")}}?q=${encodeURIComponent(query)}`, { signal })
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
resultsList.innerHTML = ''; // clear before appending
|
||||
data.forEach(item => {
|
||||
// Create list item with button inside that changes the hidden form and submits it
|
||||
const li = document.createElement('li');
|
||||
li.className = 'list-group-item p-0';
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'list-group-item list-group-item-action m-0 border-0';
|
||||
btn.style.width = '100%'; // make it fill the li
|
||||
btn.textContent = item;
|
||||
|
||||
btn.addEventListener('click', () => {
|
||||
window.location.href = baseURL.replace("!", encodeURIComponent(item));
|
||||
});
|
||||
|
||||
li.appendChild(btn);
|
||||
resultsList.appendChild(li);
|
||||
});
|
||||
}).catch(err => {
|
||||
if (err.name == 'AbortError') {
|
||||
console.log("Fetch aborted");
|
||||
}
|
||||
if (err.name !== 'AbortError') {
|
||||
console.error('Fetch error:', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<script>
|
||||
createBtn.addEventListener('click', () => {
|
||||
const newItem = searchInput.value.trim()
|
||||
window.location.href = newItem ? baseURL2.replace("!", encodeURIComponent(newItem)) : baseURL2;
|
||||
});
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
22
application/add_meal/templates/step4.html
Normal file
22
application/add_meal/templates/step4.html
Normal file
@@ -0,0 +1,22 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<p>{{ item.name }}</p>
|
||||
|
||||
<form method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
|
||||
<div class="mb-3">
|
||||
<div class="form-control-plaintext">
|
||||
{{item_id}}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
{{ form.amount.label(class="form-label") }}
|
||||
{{ form.amount(class="form-control") }}
|
||||
</div>
|
||||
|
||||
{{ form.submit(class="btn btn-primary") }}
|
||||
</form>
|
||||
{% endblock%}
|
||||
Reference in New Issue
Block a user