VortexCommerceBackend / fix_slugs.py
hisham-cmd's picture
Optimized Sync: Differential Upload & Performance Tweak
60470bc
Raw
History Blame Contribute Delete
1.16 kB
import json
import os
import time
from app.db.base import SessionLocal
from app.models.product import Product
def fix_slugs():
db = SessionLocal()
products = db.query(Product).filter(Product.slug == None).all()
# Load JSON to map existing products to their legacy ref
json_path = "app/data/extra_products_vortex.json"
legacy_map = {}
if os.path.exists(json_path):
with open(json_path, 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data:
# the product_name_ar logic in seed.py
title_ar = item.get("title_ar") or item.get("title_en", "")
if title_ar:
legacy_map[title_ar] = item.get("legacy_ref") or item.get("sku_base")
count = 0
for p in products:
slug = legacy_map.get(p.name_ar)
if not slug:
slug = legacy_map.get(p.name_en)
if not slug:
slug = f"vortex-product-{p.id}"
p.slug = slug
count += 1
db.commit()
print(f"Fixed {count} product slugs.")
if __name__ == "__main__":
fix_slugs()