fix(migration): sanitize category slugs removing ampersands and allow media relinking overwrite
This commit is contained in:
parent
998ecc025f
commit
75f103a42a
2 changed files with 36 additions and 43 deletions
|
|
@ -845,56 +845,40 @@ class MigrationWorker:
|
|||
if best_score >= 0.55: # High confidence token overlap
|
||||
mg = mg_map[best_cand]
|
||||
|
||||
if matched_mg_assets:
|
||||
if v.variant_id not in existing_vi_variant_ids:
|
||||
for idx, asset in enumerate(matched_mg_assets):
|
||||
vi = VariantImage(
|
||||
image_id=str(ulid.ULID()),
|
||||
variant_id=v.variant_id,
|
||||
image_url=asset.cdn_url,
|
||||
sort_order=idx,
|
||||
is_primary=(idx == 0)
|
||||
)
|
||||
db.add(vi)
|
||||
images_linked_count += 1
|
||||
existing_vi_variant_ids.add(v.variant_id)
|
||||
assets_to_link = matched_mg_assets if matched_mg_assets else (mg.media_assets if (mg and mg.media_assets) else [])
|
||||
if assets_to_link:
|
||||
# If variant already had images, replace them so active job images always take priority
|
||||
if v.variant_id in existing_vi_variant_ids:
|
||||
db.query(VariantImage).filter(VariantImage.variant_id == v.variant_id).delete()
|
||||
existing_vi_variant_ids.discard(v.variant_id)
|
||||
|
||||
for idx, asset in enumerate(assets_to_link):
|
||||
vi = VariantImage(
|
||||
image_id=str(ulid.ULID()),
|
||||
variant_id=v.variant_id,
|
||||
image_url=asset.cdn_url,
|
||||
sort_order=idx,
|
||||
is_primary=(idx == 0)
|
||||
)
|
||||
db.add(vi)
|
||||
images_linked_count += 1
|
||||
existing_vi_variant_ids.add(v.variant_id)
|
||||
|
||||
if p.product_id not in existing_pi_product_ids:
|
||||
pi = ProductImage(
|
||||
image_id=str(ulid.ULID()),
|
||||
product_id=p.product_id,
|
||||
image_url=matched_mg_assets[0].cdn_url,
|
||||
alt_text=matched_mg_assets[0].original_filename or p.name,
|
||||
sort_order=0,
|
||||
is_banner=False
|
||||
)
|
||||
db.add(pi)
|
||||
existing_pi_product_ids.add(p.product_id)
|
||||
elif mg and mg.media_assets:
|
||||
if v.variant_id not in existing_vi_variant_ids:
|
||||
for idx, asset in enumerate(mg.media_assets):
|
||||
vi = VariantImage(
|
||||
image_id=str(ulid.ULID()),
|
||||
variant_id=v.variant_id,
|
||||
image_url=asset.cdn_url,
|
||||
sort_order=idx,
|
||||
is_primary=(idx == 0)
|
||||
)
|
||||
db.add(vi)
|
||||
images_linked_count += 1
|
||||
existing_vi_variant_ids.add(v.variant_id)
|
||||
|
||||
if p.product_id not in existing_pi_product_ids:
|
||||
pi = ProductImage(
|
||||
image_id=str(ulid.ULID()),
|
||||
product_id=p.product_id,
|
||||
image_url=mg.media_assets[0].cdn_url,
|
||||
alt_text=mg.media_assets[0].original_filename or p.name,
|
||||
image_url=assets_to_link[0].cdn_url,
|
||||
alt_text=assets_to_link[0].original_filename or p.name,
|
||||
sort_order=0,
|
||||
is_banner=False
|
||||
)
|
||||
db.add(pi)
|
||||
existing_pi_product_ids.add(p.product_id)
|
||||
else:
|
||||
existing_pi = db.query(ProductImage).filter(ProductImage.product_id == p.product_id, ProductImage.sort_order == 0).first()
|
||||
if existing_pi and job_id and job_id not in (existing_pi.image_url or ""):
|
||||
existing_pi.image_url = assets_to_link[0].cdn_url
|
||||
|
||||
print(f"MEDIA_LINK SUCCESS: {images_linked_count} VariantImage records created.")
|
||||
db.commit()
|
||||
|
|
|
|||
|
|
@ -20,6 +20,15 @@ BRAND_LOGOS = {}
|
|||
CATEGORY_IMAGES = {}
|
||||
|
||||
|
||||
def _safe_slug(text: str) -> str:
|
||||
if not text:
|
||||
return ""
|
||||
clean = str(text).lower().replace("&", "and").replace("/", "-")
|
||||
clean = re.sub(r'[^a-z0-9\-]+', '-', clean)
|
||||
clean = re.sub(r'-+', '-', clean).strip('-')
|
||||
return clean
|
||||
|
||||
|
||||
class UpsertExecutor:
|
||||
"""
|
||||
Executes chunked batch transactions according to selected Import Mode:
|
||||
|
|
@ -102,7 +111,7 @@ class UpsertExecutor:
|
|||
brand_obj = Brand(
|
||||
brand_id=b_id,
|
||||
name=brand_name,
|
||||
slug=brand_name.lower().replace(" ", "-"),
|
||||
slug=_safe_slug(brand_name),
|
||||
logo_url=BRAND_LOGOS.get(brand_name),
|
||||
is_active=True
|
||||
)
|
||||
|
|
@ -158,7 +167,7 @@ class UpsertExecutor:
|
|||
cat_obj = Category(
|
||||
category_id=c_id,
|
||||
name=cat_name,
|
||||
slug=cat_name.lower().replace(" ", "-"),
|
||||
slug=_safe_slug(cat_name),
|
||||
image_url=CATEGORY_IMAGES.get(cat_name),
|
||||
is_parent_feature=is_parent_feature_bool,
|
||||
is_active=True
|
||||
|
|
@ -191,7 +200,7 @@ class UpsertExecutor:
|
|||
parent_cat = Category(
|
||||
category_id=pc_id,
|
||||
name=parent_cat_name,
|
||||
slug=parent_cat_name.lower().replace(" ", "-"),
|
||||
slug=_safe_slug(parent_cat_name),
|
||||
image_url=CATEGORY_IMAGES.get(parent_cat_name),
|
||||
is_active=True
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue