File size: 1,162 Bytes
60470bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()