From 2ef510358d34fe2df49a25f932385758cfb5e95b Mon Sep 17 00:00:00 2001 From: Shiro-Nek0 Date: Thu, 26 Feb 2026 03:22:05 -0300 Subject: [PATCH] cache image not being used fixed --- README.md | 1 - app.py | 33 ++++++++++++++++++++++++++------- templates/index.html | 23 ++++++++++++++++++----- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5a710d0..c80ec12 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,6 @@ python app.py - Better admin registration(?) - Cache user edited pictures - clear edited fields after scanning of new product -- FIX cached images of not saved items dont get displayed ## 🥼 Food Datasets - https://www.ifpsglobal.com/plu-codes-search diff --git a/app.py b/app.py index 9577789..a3bb4af 100644 --- a/app.py +++ b/app.py @@ -62,10 +62,16 @@ def download_image(url, barcode): return url def fetch_from_openfoodfacts(barcode): + # Check if we already have a cached image even if API fails + local_filename = f"{barcode}.jpg" + local_path = os.path.join(CACHE_DIR, local_filename) + cached_url = f"/static/cache/{local_filename}" if os.path.exists(local_path) else None + url = f"https://world.openfoodfacts.org/api/v2/product/{barcode}.json" try: headers = {'User-Agent': 'SekiPOS/1.0'} resp = requests.get(url, headers=headers, timeout=5).json() + if resp.get('status') == 1: p = resp.get('product', {}) name = p.get('product_name_es') or p.get('product_name') or p.get('brands', 'Unknown') @@ -73,7 +79,14 @@ def fetch_from_openfoodfacts(barcode): img_url = imgs.get('es') or imgs.get('en') or p.get('image_url', '') local_img = download_image(img_url, barcode) return {"name": name, "image": local_img} - except: pass + + except Exception as e: + print(f"API Error: {e}") + + # If API fails but we have a cache, return the cache with a generic name + if cached_url: + return {"name": "Producto Cacheado", "image": cached_url} + return None # --- ROUTES --- @@ -137,13 +150,19 @@ def scan(): if p: socketio.emit('new_scan', {"barcode": p[0], "name": p[1], "price": int(p[2]), "image": p[3]}) return jsonify({"status": "ok"}) + + # Not in DB, try external API or Local Cache + ext = fetch_from_openfoodfacts(barcode) + if ext: + socketio.emit('scan_error', { + "barcode": barcode, + "name": ext['name'], + "image": ext['image'] + }) else: - ext = fetch_from_openfoodfacts(barcode) - if ext: - socketio.emit('scan_error', {"barcode": barcode, "name": ext['name'], "image": ext['image']}) - else: - socketio.emit('scan_error', {"barcode": barcode}) - return jsonify({"status": "not_found"}), 404 + socketio.emit('scan_error', {"barcode": barcode}) + + return jsonify({"status": "not_found"}), 404 @app.route('/static/cache/') def serve_cache(filename): diff --git a/templates/index.html b/templates/index.html index e9281d4..76095ec 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,7 +15,8 @@

SekiPOS v1.2

- + | Usuario: {{ user.username }} | @@ -26,8 +27,10 @@
- ¡Nuevo! Barcode . ¿Deseas agregarlo? - + ¡Nuevo! Barcode . ¿Deseas + agregarlo? +
@@ -71,9 +74,11 @@ {{ p[1] }} - +
- +
@@ -131,6 +136,14 @@ 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 + 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 || '';