From 9a28daa2cdb05d9f6b419a05c17b29d61c207269 Mon Sep 17 00:00:00 2001 From: Shiro-Nek0 Date: Thu, 26 Feb 2026 03:33:45 -0300 Subject: [PATCH] fill edit fileds on scan --- README.md | 1 - app.py | 11 +++++++---- templates/index.html | 37 ++++++++++++++++++++++++++++--------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 72ae929..8e9f3ab 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,6 @@ python app.py ## 📋 TODOs? - Better admin registration(?) -- clear edited fields after scanning of new product ## 🥼 Food Datasets - https://www.ifpsglobal.com/plu-codes-search diff --git a/app.py b/app.py index 8021a3b..44bc956 100644 --- a/app.py +++ b/app.py @@ -132,16 +132,19 @@ def index(): def upsert(): d = request.form barcode = d['barcode'] - original_url = d['image_url'] - # Process the URL: Download if external, or keep if already local/empty - final_image_path = download_image(original_url, barcode) + try: + price = float(d['price']) + except (ValueError, TypeError): + price = 0.0 + + final_image_path = download_image(d['image_url'], barcode) with sqlite3.connect(DB_FILE) as conn: conn.execute('''INSERT INTO products (barcode, name, price, image_url) VALUES (?,?,?,?) ON CONFLICT(barcode) DO UPDATE SET name=excluded.name, price=excluded.price, image_url=excluded.image_url''', - (barcode, d['name'], d['price'], final_image_path)) + (barcode, d['name'], price, final_image_path)) conn.commit() return redirect(url_for('index')) diff --git a/templates/index.html b/templates/index.html index 76095ec..890d5a4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -125,33 +125,52 @@ updateThemeUI(savedTheme); socket.on('new_scan', function (data) { - dismissPrompt(); + // 1. Update the Display Card document.getElementById('display-name').innerText = data.name; document.getElementById('display-price').innerText = clpFormatter.format(data.price); document.getElementById('display-barcode').innerText = data.barcode; document.getElementById('display-img').src = data.image || './static/placeholder.png'; + + // 2. Sync the Edit Form to this product + updateForm(data.barcode, data.name, data.price, data.image, "Editando: " + data.name); }); socket.on('scan_error', function (data) { + // 1. Show the "Add New" prompt at the top const prompt = document.getElementById('new-product-prompt'); document.getElementById('new-barcode-display').innerText = data.barcode; prompt.style.display = 'flex'; - // FIX: Update the main display card even if it's not in the DB yet + // 2. Update Display Card with whatever info we have (cached or external) document.getElementById('display-name').innerText = data.name || "Producto Nuevo"; document.getElementById('display-price').innerText = clpFormatter.format(0); document.getElementById('display-barcode').innerText = data.barcode; document.getElementById('display-img').src = data.image || './static/placeholder.png'; - // Fill the form - document.getElementById('form-barcode').value = data.barcode; - document.getElementById('form-name').value = data.name || ''; - document.getElementById('form-image').value = data.image || ''; - document.getElementById('form-price').value = ''; - document.getElementById('form-title').innerText = "Crear Nuevo: " + (data.name || data.barcode); - document.getElementById('form-price').focus(); + // 3. Prepare the form for a new entry + updateForm(data.barcode, data.name, '', data.image, "Crear Nuevo: " + (data.name || data.barcode)); }); + function updateForm(barcode, name, price, image, title) { + // Reset the prompt if it was open + dismissPrompt(); + + // Update Form Fields + document.getElementById('form-barcode').value = barcode; + document.getElementById('form-name').value = name || ''; + document.getElementById('form-price').value = price || ''; + document.getElementById('form-image').value = image || ''; + document.getElementById('form-title').innerText = title; + + // Visual feedback: briefly highlight the form + const formCard = document.getElementById('product-form').parentElement; + formCard.style.border = "2px solid var(--accent)"; + setTimeout(() => { formCard.style.border = "none"; }, 500); + + // Focus price because it's usually the only thing missing + document.getElementById('form-price').focus(); + } + function dismissPrompt() { document.getElementById('new-product-prompt').style.display = 'none'; }