image timestamp fix
This commit is contained in:
11
app.py
11
app.py
@@ -6,6 +6,7 @@ from flask_socketio import SocketIO, emit
|
|||||||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import time
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' # Change this if you have actual friends
|
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' # Change this if you have actual friends
|
||||||
@@ -250,22 +251,16 @@ def bulk_delete():
|
|||||||
def upload_image():
|
def upload_image():
|
||||||
if 'image' not in request.files or 'barcode' not in request.form:
|
if 'image' not in request.files or 'barcode' not in request.form:
|
||||||
return jsonify({"error": "Missing data"}), 400
|
return jsonify({"error": "Missing data"}), 400
|
||||||
|
|
||||||
file = request.files['image']
|
file = request.files['image']
|
||||||
barcode = request.form['barcode']
|
barcode = request.form['barcode']
|
||||||
|
|
||||||
if file.filename == '' or not barcode:
|
if file.filename == '' or not barcode:
|
||||||
return jsonify({"error": "Invalid data"}), 400
|
return jsonify({"error": "Invalid data"}), 400
|
||||||
|
|
||||||
# Detect extension
|
|
||||||
ext = mimetypes.guess_extension(file.mimetype) or '.jpg'
|
ext = mimetypes.guess_extension(file.mimetype) or '.jpg'
|
||||||
filename = f"{barcode}{ext}"
|
filename = f"{barcode}{ext}"
|
||||||
filepath = os.path.join(CACHE_DIR, filename)
|
filepath = os.path.join(CACHE_DIR, filename)
|
||||||
|
|
||||||
file.save(filepath)
|
file.save(filepath)
|
||||||
|
timestamp = int(time.time())
|
||||||
# Return the relative path for the frontend
|
return jsonify({"status": "success", "image_url": f"/static/cache/{filename}?t={timestamp}"}), 200
|
||||||
return jsonify({"status": "success", "image_url": f"/static/cache/{filename}"}), 200
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
init_db()
|
init_db()
|
||||||
|
|||||||
@@ -603,16 +603,22 @@
|
|||||||
updateForm(d.barcode, d.name || '', '', d.image || '', 'Crear: ' + d.barcode);
|
updateForm(d.barcode, d.name || '', '', d.image || '', 'Crear: ' + d.barcode);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Replace your existing updateForm function with this one
|
||||||
function updateForm(b, n, p, i, t) {
|
function updateForm(b, n, p, i, t) {
|
||||||
dismissPrompt();
|
dismissPrompt();
|
||||||
document.getElementById('form-barcode').value = b;
|
document.getElementById('form-barcode').value = b;
|
||||||
document.getElementById('form-name').value = n;
|
document.getElementById('form-name').value = n;
|
||||||
document.getElementById('form-price').value = (p !== undefined && p !== null) ? p : '';
|
document.getElementById('form-price').value = (p !== undefined && p !== null) ? p : '';
|
||||||
|
|
||||||
|
// Add a timestamp to the URL if it's a local cache image
|
||||||
|
let displayImg = i || './static/placeholder.png';
|
||||||
|
if (displayImg.includes('/static/cache/')) {
|
||||||
|
displayImg += (displayImg.includes('?') ? '&' : '?') + 't=' + Date.now();
|
||||||
|
}
|
||||||
|
|
||||||
document.getElementById('form-image').value = i || '';
|
document.getElementById('form-image').value = i || '';
|
||||||
document.getElementById('form-title').innerText = t;
|
document.getElementById('form-title').innerText = t;
|
||||||
|
document.getElementById('display-img').src = displayImg;
|
||||||
// ADD THESE LINES TO UPDATE THE PREVIEW CARD
|
|
||||||
document.getElementById('display-img').src = i || './static/placeholder.png';
|
|
||||||
document.getElementById('display-name').innerText = n || 'Producto Nuevo';
|
document.getElementById('display-name').innerText = n || 'Producto Nuevo';
|
||||||
document.getElementById('display-price').innerText = clp.format(p || 0);
|
document.getElementById('display-price').innerText = clp.format(p || 0);
|
||||||
document.getElementById('display-barcode').innerText = b;
|
document.getElementById('display-barcode').innerText = b;
|
||||||
|
|||||||
Reference in New Issue
Block a user