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:
2025-10-20 16:45:58 -04:00
parent 6dc276a292
commit 92eb45fc8c

View File

@@ -110,8 +110,9 @@ def add_product(tenant_id):
'mime_type': mime_type
})
# Set the image URL path
image_path = f"/images/{product_id}_{field_name}.{extension}"
# Set the image URL path (strip leading underscore from field_name to avoid double underscores)
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
if field_name == '_image':
@@ -231,7 +232,7 @@ def edit_product(tenant_id, product_id):
# Read image data
file.seek(0)
img_data = file.read()
# Determine mime type
extension = file.filename.rsplit('.', 1)[1].lower()
mime_types = {
@@ -241,21 +242,23 @@ def edit_product(tenant_id, product_id):
'gif': 'image/gif'
}
mime_type = mime_types.get(extension, 'image/jpeg')
# Store for later saving
images_to_save.append({
'field_name': field_name,
'data': img_data,
'mime_type': mime_type
})
# Update image path
existing_field["value"] = f"/images/{product_id}_{field_name}.{extension}"
# Update image path (strip leading underscore from field_name to avoid double underscores)
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
if field_name == '_image':
image_data = img_data
image_mime_type = mime_type
# If no new image uploaded, keep the existing value (already in existing_field)
else:
# Get value from form
value = request.form.get(f'field_{field_name}', '')