Added seed media fix for same image across 2 templates
This commit is contained in:
parent
b44e0a2dd2
commit
d6c571ce8b
|
|
@ -31,32 +31,42 @@ def build_source_key(*, template_code: str, source_code: str) -> str:
|
||||||
|
|
||||||
def canonical_media_filename(
|
def canonical_media_filename(
|
||||||
*,
|
*,
|
||||||
template_code: str,
|
|
||||||
source_code: str,
|
source_code: str,
|
||||||
usage_type: str,
|
usage_type: str,
|
||||||
ordinal: int,
|
ordinal: int,
|
||||||
extension: str,
|
extension: str,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
"""Build a template-independent canonical filename for one media asset."""
|
||||||
source_component = slugify(source_code)
|
source_component = slugify(source_code)
|
||||||
|
if not source_component:
|
||||||
|
raise ValueError("source_code becomes empty after canonicalization")
|
||||||
|
|
||||||
usage_component = slugify(usage_type)
|
usage_component = slugify(usage_type)
|
||||||
|
if not usage_component:
|
||||||
|
raise ValueError("usage_type becomes empty after canonicalization")
|
||||||
|
|
||||||
extension = extension.lower()
|
extension = extension.lower()
|
||||||
if not extension.startswith("."):
|
if not extension.startswith("."):
|
||||||
extension = f".{extension}"
|
extension = f".{extension}"
|
||||||
|
|
||||||
return (
|
return f"{source_component}--{usage_component}--{ordinal:02d}{extension}"
|
||||||
f"{require_non_empty_string(template_code, 'template_code').lower()}--{source_component}--"
|
|
||||||
f"{usage_component}--{ordinal:02d}{extension}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def build_storage_key(
|
def build_storage_key(
|
||||||
*,
|
*,
|
||||||
module_code: str,
|
module_code: str,
|
||||||
template_code: str,
|
|
||||||
source_code: str,
|
|
||||||
filename: str,
|
filename: str,
|
||||||
) -> str:
|
) -> str:
|
||||||
return (
|
"""
|
||||||
f"images/{require_non_empty_string(module_code, 'module_code').lower()}/{require_non_empty_string(template_code, 'template_code').lower()}/"
|
Build a module-scoped, template-independent object-storage key.
|
||||||
f"{slugify(source_code)}/{Path(filename).name}"
|
|
||||||
)
|
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}"
|
||||||
|
|
|
||||||
|
|
@ -147,7 +147,6 @@ class ImageSeedService:
|
||||||
raise RuntimeError(f"Target disappeared after preflight: {source_key}")
|
raise RuntimeError(f"Target disappeared after preflight: {source_key}")
|
||||||
|
|
||||||
canonical_filename = canonical_media_filename(
|
canonical_filename = canonical_media_filename(
|
||||||
template_code=entry.target.template_code,
|
|
||||||
source_code=entry.target.source_code,
|
source_code=entry.target.source_code,
|
||||||
usage_type=entry.usage_type,
|
usage_type=entry.usage_type,
|
||||||
ordinal=entry.sort_order,
|
ordinal=entry.sort_order,
|
||||||
|
|
@ -155,8 +154,6 @@ class ImageSeedService:
|
||||||
)
|
)
|
||||||
storage_key = build_storage_key(
|
storage_key = build_storage_key(
|
||||||
module_code=entry.target.module_code,
|
module_code=entry.target.module_code,
|
||||||
template_code=entry.target.template_code,
|
|
||||||
source_code=entry.target.source_code,
|
|
||||||
filename=canonical_filename,
|
filename=canonical_filename,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -181,15 +178,19 @@ class ImageSeedService:
|
||||||
uploaded_this_iteration = True
|
uploaded_this_iteration = True
|
||||||
report.uploaded += 1
|
report.uploaded += 1
|
||||||
|
|
||||||
metadata: dict[str, Any] = dict(entry.metadata)
|
asset_metadata: dict[str, Any] = dict(entry.metadata)
|
||||||
metadata.update(
|
asset_metadata.update(
|
||||||
{
|
{
|
||||||
"module_code": entry.target.module_code,
|
"module_code": entry.target.module_code,
|
||||||
"template_code": entry.target.template_code,
|
"canonical_filename": canonical_filename,
|
||||||
"source_code": entry.target.source_code,
|
|
||||||
"source_key": source_key,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
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():
|
async with self.session.begin_nested():
|
||||||
asset = await self.repository.upsert_media_asset(
|
asset = await self.repository.upsert_media_asset(
|
||||||
|
|
@ -202,7 +203,7 @@ class ImageSeedService:
|
||||||
width=int(facts["width"]),
|
width=int(facts["width"]),
|
||||||
height=int(facts["height"]),
|
height=int(facts["height"]),
|
||||||
alt_text=entry.alt_text,
|
alt_text=entry.alt_text,
|
||||||
metadata=metadata,
|
metadata=asset_metadata,
|
||||||
)
|
)
|
||||||
report.db_upserts += 1
|
report.db_upserts += 1
|
||||||
|
|
||||||
|
|
@ -220,7 +221,7 @@ class ImageSeedService:
|
||||||
sort_order=entry.sort_order,
|
sort_order=entry.sort_order,
|
||||||
alt_text=entry.alt_text,
|
alt_text=entry.alt_text,
|
||||||
caption=entry.caption,
|
caption=entry.caption,
|
||||||
metadata=metadata,
|
metadata=association_metadata,
|
||||||
)
|
)
|
||||||
report.associations_upserted += 1
|
report.associations_upserted += 1
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue