fill edit fileds on scan
This commit is contained in:
@@ -81,7 +81,6 @@ python app.py
|
|||||||
|
|
||||||
## 📋 TODOs?
|
## 📋 TODOs?
|
||||||
- Better admin registration(?)
|
- Better admin registration(?)
|
||||||
- clear edited fields after scanning of new product
|
|
||||||
|
|
||||||
## 🥼 Food Datasets
|
## 🥼 Food Datasets
|
||||||
- https://www.ifpsglobal.com/plu-codes-search
|
- https://www.ifpsglobal.com/plu-codes-search
|
||||||
|
|||||||
11
app.py
11
app.py
@@ -132,16 +132,19 @@ def index():
|
|||||||
def upsert():
|
def upsert():
|
||||||
d = request.form
|
d = request.form
|
||||||
barcode = d['barcode']
|
barcode = d['barcode']
|
||||||
original_url = d['image_url']
|
|
||||||
|
|
||||||
# Process the URL: Download if external, or keep if already local/empty
|
try:
|
||||||
final_image_path = download_image(original_url, barcode)
|
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:
|
with sqlite3.connect(DB_FILE) as conn:
|
||||||
conn.execute('''INSERT INTO products (barcode, name, price, image_url) VALUES (?,?,?,?)
|
conn.execute('''INSERT INTO products (barcode, name, price, image_url) VALUES (?,?,?,?)
|
||||||
ON CONFLICT(barcode) DO UPDATE SET name=excluded.name,
|
ON CONFLICT(barcode) DO UPDATE SET name=excluded.name,
|
||||||
price=excluded.price, image_url=excluded.image_url''',
|
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()
|
conn.commit()
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
|||||||
@@ -125,33 +125,52 @@
|
|||||||
updateThemeUI(savedTheme);
|
updateThemeUI(savedTheme);
|
||||||
|
|
||||||
socket.on('new_scan', function (data) {
|
socket.on('new_scan', function (data) {
|
||||||
dismissPrompt();
|
// 1. Update the Display Card
|
||||||
document.getElementById('display-name').innerText = data.name;
|
document.getElementById('display-name').innerText = data.name;
|
||||||
document.getElementById('display-price').innerText = clpFormatter.format(data.price);
|
document.getElementById('display-price').innerText = clpFormatter.format(data.price);
|
||||||
document.getElementById('display-barcode').innerText = data.barcode;
|
document.getElementById('display-barcode').innerText = data.barcode;
|
||||||
document.getElementById('display-img').src = data.image || './static/placeholder.png';
|
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) {
|
socket.on('scan_error', function (data) {
|
||||||
|
// 1. Show the "Add New" prompt at the top
|
||||||
const prompt = document.getElementById('new-product-prompt');
|
const prompt = document.getElementById('new-product-prompt');
|
||||||
document.getElementById('new-barcode-display').innerText = data.barcode;
|
document.getElementById('new-barcode-display').innerText = data.barcode;
|
||||||
prompt.style.display = 'flex';
|
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-name').innerText = data.name || "Producto Nuevo";
|
||||||
document.getElementById('display-price').innerText = clpFormatter.format(0);
|
document.getElementById('display-price').innerText = clpFormatter.format(0);
|
||||||
document.getElementById('display-barcode').innerText = data.barcode;
|
document.getElementById('display-barcode').innerText = data.barcode;
|
||||||
document.getElementById('display-img').src = data.image || './static/placeholder.png';
|
document.getElementById('display-img').src = data.image || './static/placeholder.png';
|
||||||
|
|
||||||
// Fill the form
|
// 3. Prepare the form for a new entry
|
||||||
document.getElementById('form-barcode').value = data.barcode;
|
updateForm(data.barcode, data.name, '', data.image, "Crear Nuevo: " + (data.name || 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();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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() {
|
function dismissPrompt() {
|
||||||
document.getElementById('new-product-prompt').style.display = 'none';
|
document.getElementById('new-product-prompt').style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user