From 9310360903352c20eaf4a35ea40202cee5211f48 Mon Sep 17 00:00:00 2001 From: "russell@unturf.com" Date: Sun, 15 Feb 2026 11:47:27 -0500 Subject: [PATCH] Fix edit page media previews: add presigned URL generation, type-aware embeds, mock S3 in test --- make_post_sell/templates/product_edit.j2 | 48 ++++++++++++++++++++++-- make_post_sell/tests/test_functional.py | 11 +++++- make_post_sell/views/product.py | 20 ++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/make_post_sell/templates/product_edit.j2 b/make_post_sell/templates/product_edit.j2 index 8ca9d14..c08b9f6 100644 --- a/make_post_sell/templates/product_edit.j2 +++ b/make_post_sell/templates/product_edit.j2 @@ -8,6 +8,10 @@ permanent link: {{ product.absolut {% block content -%} +{% set video_extensions = ["mp4", "avi", "mov", "wmv", "flv", "mkv", "webm", "m4v"] %} +{% set audio_extensions = ["mp3", "wav", "ogg", "m4a", "flac", "aac", "opus"] %} +{% set image_extensions = ["jpg", "jpeg", "png", "gif", "webp", "svg", "bmp", "ico"] %} +
@@ -86,6 +90,20 @@ permanent link: {{ product.absolut

Current File: {{ product.originals["product"] }}

File Type: {{ product.extensions.get("product", "unknown") }}

File Size: {{ product.human_file_bytes("product") }}

+ + {% set ext = product.extensions.get("product", "") %} + {% if signed_product_url %} + {% if ext in video_extensions %} + + {% elif ext in audio_extensions %} + + {% elif ext == "pdf" %} + + {% elif ext in image_extensions %} + + {% endif %} + {% endif %} + {% endif %} {% set signed_post = signed_posts["product"] %} @@ -113,6 +131,19 @@ permanent link:
{{ product.absolut

Current File: {{ product.originals["preview"] }}

File Type: {{ product.extensions.get("preview", "unknown") }}

File Size: {{ product.human_file_bytes("preview") }}

+ + {% set preview_ext = product.extensions.get("preview", "") %} + {% set preview_url = request.app["bucket.secure_uploads.get_endpoint"] ~ "/" ~ product.s3_path ~ "/preview?ts=" ~ product.updated_timestamp %} + {% if preview_ext in video_extensions %} + + {% elif preview_ext in audio_extensions %} + + {% elif preview_ext == "pdf" %} + + {% elif preview_ext in image_extensions %} + + {% endif %} + {% endif %} {% set signed_post = signed_posts["preview"] %} @@ -145,9 +176,20 @@ Your cover (thumbnail1) will show up on search pages.
- {% if thumbnail_key in s3_key_thumbnails %} - {% set get_endpoint = request.app["bucket.secure_uploads.get_endpoint"] + "/" + s3_key_thumbnails[thumbnail_key] %} - + {% if thumbnail_key in s3_key_thumbnails %} + {% set get_endpoint = request.app["bucket.secure_uploads.get_endpoint"] ~ "/" ~ s3_key_thumbnails[thumbnail_key] %} + {% set thumb_ext = product.extensions.get(thumbnail_key, "") %} + {% if thumb_ext in video_extensions %} + + {% elif thumb_ext in audio_extensions %} + + {% elif thumb_ext == "pdf" %} + + {% else %} + + + + {% endif %} {% endif %} {% if thumbnail_key in product.originals %} diff --git a/make_post_sell/tests/test_functional.py b/make_post_sell/tests/test_functional.py index bc00adf..49f0eed 100644 --- a/make_post_sell/tests/test_functional.py +++ b/make_post_sell/tests/test_functional.py @@ -3684,9 +3684,18 @@ class TestAnalytics(AuthenticatedFunctionalTests): res = self.testapp.get(f"/s/{shop.id}/{shop.slug}", status=200) self.assertNotIn('data-color-filter', res.text) + @patch("boto3.session.Session") @patch("smtplib.SMTP") - def test_edit_page_shows_media_preview_for_product_file(self, mock_smtp): + def test_edit_page_shows_media_preview_for_product_file(self, mock_smtp, mock_session_cls): """Edit page renders inline media preview when product has an uploaded file.""" + # Mock the S3 client so generate_presigned_url returns a fake URL + mock_client = mock_session_cls.return_value.client.return_value + mock_client.generate_presigned_url.return_value = "https://fake-cdn.example.com/signed-video" + mock_client.generate_presigned_post.return_value = { + "url": "https://fake-cdn.example.com/upload", + "fields": {}, + } + shop = self._create_shop_helper() # Create a product diff --git a/make_post_sell/views/product.py b/make_post_sell/views/product.py index 311a960..7af8a24 100644 --- a/make_post_sell/views/product.py +++ b/make_post_sell/views/product.py @@ -430,6 +430,25 @@ def product_edit(request): "is_current": i == 0, }) + signed_product_url = None + if product.has_product_file: + try: + params = { + "Bucket": request.app["bucket.secure_uploads"], + "Key": product.s3_key, + "ResponseContentDisposition": f"inline; filename={product.slug}.{product.extensions.get('product', '')}", + } + content_type = product.get_content_type("product") + if content_type: + params["ResponseContentType"] = content_type + signed_product_url = request.secure_uploads_client.generate_presigned_url( + ClientMethod="get_object", + Params=params, + ExpiresIn=900, + ) + except Exception: + pass + return { "product": product, "title": title, @@ -437,6 +456,7 @@ def product_edit(request): "price": price, "visibility": visibility, "signed_posts": signed_posts, + "signed_product_url": signed_product_url, "locations": locations, "inventories": inventories if product.is_physical else {}, "price_history": price_history,