From d6c571ce8bf0d14cffe281bf51ef055ef6356866 Mon Sep 17 00:00:00 2001 From: Amit Salunke Date: Sun, 13 Sep 2026 22:44:40 +0530 Subject: [PATCH] Added seed media fix for same image across 2 templates --- app/bootstrap/seed_media/identity.py | 32 ++++++++++++++++++---------- app/bootstrap/seed_media/service.py | 21 +++++++++--------- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/app/bootstrap/seed_media/identity.py b/app/bootstrap/seed_media/identity.py index c77f7e5..a065831 100644 --- a/app/bootstrap/seed_media/identity.py +++ b/app/bootstrap/seed_media/identity.py @@ -31,32 +31,42 @@ def build_source_key(*, template_code: str, source_code: str) -> str: def canonical_media_filename( *, - template_code: str, source_code: str, usage_type: str, ordinal: int, extension: str, ) -> str: + """Build a template-independent canonical filename for one media asset.""" source_component = slugify(source_code) + if not source_component: + raise ValueError("source_code becomes empty after canonicalization") + usage_component = slugify(usage_type) + if not usage_component: + raise ValueError("usage_type becomes empty after canonicalization") + extension = extension.lower() if not extension.startswith("."): extension = f".{extension}" - return ( - f"{require_non_empty_string(template_code, 'template_code').lower()}--{source_component}--" - f"{usage_component}--{ordinal:02d}{extension}" - ) + return f"{source_component}--{usage_component}--{ordinal:02d}{extension}" def build_storage_key( *, module_code: str, - template_code: str, - source_code: str, filename: str, ) -> str: - return ( - f"images/{require_non_empty_string(module_code, 'module_code').lower()}/{require_non_empty_string(template_code, 'template_code').lower()}/" - f"{slugify(source_code)}/{Path(filename).name}" - ) + """ + Build a module-scoped, template-independent object-storage key. + + The same physical media file can therefore be associated with content + from multiple templates without creating a second storage object. + """ + module_component = require_non_empty_string( + module_code, "module_code" + ).lower() + file_component = Path(filename).name + if not file_component: + raise ValueError("filename must not be empty") + return f"images/{module_component}/{file_component}" diff --git a/app/bootstrap/seed_media/service.py b/app/bootstrap/seed_media/service.py index 92522f5..a5b6cdb 100644 --- a/app/bootstrap/seed_media/service.py +++ b/app/bootstrap/seed_media/service.py @@ -147,7 +147,6 @@ class ImageSeedService: raise RuntimeError(f"Target disappeared after preflight: {source_key}") canonical_filename = canonical_media_filename( - template_code=entry.target.template_code, source_code=entry.target.source_code, usage_type=entry.usage_type, ordinal=entry.sort_order, @@ -155,8 +154,6 @@ class ImageSeedService: ) storage_key = build_storage_key( module_code=entry.target.module_code, - template_code=entry.target.template_code, - source_code=entry.target.source_code, filename=canonical_filename, ) @@ -181,15 +178,19 @@ class ImageSeedService: uploaded_this_iteration = True report.uploaded += 1 - metadata: dict[str, Any] = dict(entry.metadata) - metadata.update( + asset_metadata: dict[str, Any] = dict(entry.metadata) + asset_metadata.update( { "module_code": entry.target.module_code, - "template_code": entry.target.template_code, - "source_code": entry.target.source_code, - "source_key": source_key, + "canonical_filename": canonical_filename, } ) + association_metadata: dict[str, Any] = { + "module_code": entry.target.module_code, + "template_code": entry.target.template_code, + "source_code": entry.target.source_code, + "source_key": source_key, + } async with self.session.begin_nested(): asset = await self.repository.upsert_media_asset( @@ -202,7 +203,7 @@ class ImageSeedService: width=int(facts["width"]), height=int(facts["height"]), alt_text=entry.alt_text, - metadata=metadata, + metadata=asset_metadata, ) report.db_upserts += 1 @@ -220,7 +221,7 @@ class ImageSeedService: sort_order=entry.sort_order, alt_text=entry.alt_text, caption=entry.caption, - metadata=metadata, + metadata=association_metadata, ) report.associations_upserted += 1