Fix product image not saving/displaying in tenant admin
Fixed two issues with product images: 1. Images weren't being preserved when editing other product fields - added logic to keep existing image value when no new image is uploaded 2. Image URLs had double underscores (e.g., /images/123__image.jpg) due to field names starting with underscore - now strips leading underscore from field name when constructing image paths
This commit is contained in:
@@ -110,8 +110,9 @@ def add_product(tenant_id):
|
|||||||
'mime_type': mime_type
|
'mime_type': mime_type
|
||||||
})
|
})
|
||||||
|
|
||||||
# Set the image URL path
|
# Set the image URL path (strip leading underscore from field_name to avoid double underscores)
|
||||||
image_path = f"/images/{product_id}_{field_name}.{extension}"
|
field_suffix = field_name[1:] if field_name.startswith('_') else field_name
|
||||||
|
image_path = f"/images/{product_id}_{field_suffix}.{extension}"
|
||||||
|
|
||||||
# For backward compatibility with _image field
|
# For backward compatibility with _image field
|
||||||
if field_name == '_image':
|
if field_name == '_image':
|
||||||
@@ -231,7 +232,7 @@ def edit_product(tenant_id, product_id):
|
|||||||
# Read image data
|
# Read image data
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
img_data = file.read()
|
img_data = file.read()
|
||||||
|
|
||||||
# Determine mime type
|
# Determine mime type
|
||||||
extension = file.filename.rsplit('.', 1)[1].lower()
|
extension = file.filename.rsplit('.', 1)[1].lower()
|
||||||
mime_types = {
|
mime_types = {
|
||||||
@@ -241,21 +242,23 @@ def edit_product(tenant_id, product_id):
|
|||||||
'gif': 'image/gif'
|
'gif': 'image/gif'
|
||||||
}
|
}
|
||||||
mime_type = mime_types.get(extension, 'image/jpeg')
|
mime_type = mime_types.get(extension, 'image/jpeg')
|
||||||
|
|
||||||
# Store for later saving
|
# Store for later saving
|
||||||
images_to_save.append({
|
images_to_save.append({
|
||||||
'field_name': field_name,
|
'field_name': field_name,
|
||||||
'data': img_data,
|
'data': img_data,
|
||||||
'mime_type': mime_type
|
'mime_type': mime_type
|
||||||
})
|
})
|
||||||
|
|
||||||
# Update image path
|
# Update image path (strip leading underscore from field_name to avoid double underscores)
|
||||||
existing_field["value"] = f"/images/{product_id}_{field_name}.{extension}"
|
field_suffix = field_name[1:] if field_name.startswith('_') else field_name
|
||||||
|
existing_field["value"] = f"/images/{product_id}_{field_suffix}.{extension}"
|
||||||
|
|
||||||
# For backward compatibility with _image field
|
# For backward compatibility with _image field
|
||||||
if field_name == '_image':
|
if field_name == '_image':
|
||||||
image_data = img_data
|
image_data = img_data
|
||||||
image_mime_type = mime_type
|
image_mime_type = mime_type
|
||||||
|
# If no new image uploaded, keep the existing value (already in existing_field)
|
||||||
else:
|
else:
|
||||||
# Get value from form
|
# Get value from form
|
||||||
value = request.form.get(f'field_{field_name}', '')
|
value = request.form.get(f'field_{field_name}', '')
|
||||||
|
|||||||
Reference in New Issue
Block a user