Spaces:
Sleeping
Sleeping
Upload app/api/seed.py
Browse files- app/api/seed.py +40 -24
app/api/seed.py
CHANGED
|
@@ -98,7 +98,11 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
|
|
| 98 |
statusCode=200,
|
| 99 |
)
|
| 100 |
|
| 101 |
-
json_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if not os.path.exists(json_path):
|
| 103 |
return AuthResponse(
|
| 104 |
isSuccess=False,
|
|
@@ -148,52 +152,64 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
|
|
| 148 |
used_names = set() # Avoid absolute name duplicates in current seed run
|
| 149 |
|
| 150 |
for item in products_data:
|
| 151 |
-
|
|
|
|
| 152 |
if not cat_name or cat_name not in sub_cats:
|
| 153 |
continue
|
| 154 |
|
| 155 |
-
product_name_ar = item.get("اسم المنتج", "").strip()
|
| 156 |
if not product_name_ar or product_name_ar in used_names:
|
| 157 |
continue
|
| 158 |
|
| 159 |
sub_cat_info = sub_cats[cat_name]
|
| 160 |
used_names.add(product_name_ar)
|
| 161 |
|
| 162 |
-
price = float(item.get("سعر البيع (ريال)", 0))
|
| 163 |
-
compare_price = price * 1.2
|
| 164 |
|
| 165 |
prod = Product(
|
| 166 |
name_ar=product_name_ar,
|
| 167 |
-
name_en=product_name_ar,
|
| 168 |
-
description_ar=item.get("الوصف التسويقي", ""),
|
| 169 |
-
description_en=item.get("الوصف التسويقي", ""),
|
| 170 |
price=price,
|
| 171 |
compare_price=round(compare_price, 2),
|
| 172 |
-
stock=random.randint(10, 100),
|
| 173 |
category_id=sub_cat_info["model"].id,
|
| 174 |
rating=round(random.uniform(3.5, 5.0), 1),
|
| 175 |
rating_count=random.randint(0, 50),
|
| 176 |
-
is_featured=random.choice([True, False]),
|
| 177 |
-
is_active=True
|
|
|
|
| 178 |
)
|
| 179 |
db.add(prod)
|
| 180 |
db.flush()
|
| 181 |
|
| 182 |
-
# 4.
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
inserted_count += 1
|
| 195 |
-
if inserted_count >= 500: # Maintain a safe limit for demonstration
|
| 196 |
-
break
|
| 197 |
|
| 198 |
# 4. Enrich with Real Discovered Products
|
| 199 |
ext_service = ExternalCatalogService(db)
|
|
|
|
| 98 |
statusCode=200,
|
| 99 |
)
|
| 100 |
|
| 101 |
+
json_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "real_products_500.json")
|
| 102 |
+
# Fallback to products_dump.json if 500 file not found
|
| 103 |
+
if not os.path.exists(json_path):
|
| 104 |
+
json_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "products_dump.json")
|
| 105 |
+
|
| 106 |
if not os.path.exists(json_path):
|
| 107 |
return AuthResponse(
|
| 108 |
isSuccess=False,
|
|
|
|
| 152 |
used_names = set() # Avoid absolute name duplicates in current seed run
|
| 153 |
|
| 154 |
for item in products_data:
|
| 155 |
+
# Handle both formats (real_products_500.json vs products_dump.json)
|
| 156 |
+
cat_name = item.get("category_name") or item.get("الفئة")
|
| 157 |
if not cat_name or cat_name not in sub_cats:
|
| 158 |
continue
|
| 159 |
|
| 160 |
+
product_name_ar = item.get("name_ar") or item.get("اسم المنتج", "").strip()
|
| 161 |
if not product_name_ar or product_name_ar in used_names:
|
| 162 |
continue
|
| 163 |
|
| 164 |
sub_cat_info = sub_cats[cat_name]
|
| 165 |
used_names.add(product_name_ar)
|
| 166 |
|
| 167 |
+
price = float(item.get("price") or item.get("سعر البيع (ريال)", 0))
|
| 168 |
+
compare_price = float(item.get("compare_price") or (price * 1.2))
|
| 169 |
|
| 170 |
prod = Product(
|
| 171 |
name_ar=product_name_ar,
|
| 172 |
+
name_en=item.get("name_en") or product_name_ar,
|
| 173 |
+
description_ar=item.get("description_ar") or item.get("الوصف التسويقي", ""),
|
| 174 |
+
description_en=item.get("description_en") or item.get("الوصف التسويقي", ""),
|
| 175 |
price=price,
|
| 176 |
compare_price=round(compare_price, 2),
|
| 177 |
+
stock=item.get("stock") or random.randint(10, 100),
|
| 178 |
category_id=sub_cat_info["model"].id,
|
| 179 |
rating=round(random.uniform(3.5, 5.0), 1),
|
| 180 |
rating_count=random.randint(0, 50),
|
| 181 |
+
is_featured=item.get("is_featured", random.choice([True, False])),
|
| 182 |
+
is_active=item.get("is_active", True),
|
| 183 |
+
specs=item.get("specs", {})
|
| 184 |
)
|
| 185 |
db.add(prod)
|
| 186 |
db.flush()
|
| 187 |
|
| 188 |
+
# 4. Handle Images
|
| 189 |
+
raw_images = item.get("images", [])
|
| 190 |
+
if not raw_images:
|
| 191 |
+
# Generate fallback images if none provided
|
| 192 |
+
num_images = random.randint(3, 4)
|
| 193 |
+
for i in range(num_images):
|
| 194 |
+
img_url = get_image_url(sub_cat_info["parent_name"], product_name_ar, i)
|
| 195 |
+
prod_img = ProductImage(
|
| 196 |
+
product_id=prod.id,
|
| 197 |
+
image_url=img_url,
|
| 198 |
+
alt_text=prod.name_ar,
|
| 199 |
+
sort_order=i
|
| 200 |
+
)
|
| 201 |
+
db.add(prod_img)
|
| 202 |
+
else:
|
| 203 |
+
for i, img_url in enumerate(raw_images):
|
| 204 |
+
prod_img = ProductImage(
|
| 205 |
+
product_id=prod.id,
|
| 206 |
+
image_url=img_url,
|
| 207 |
+
alt_text=prod.name_ar,
|
| 208 |
+
sort_order=i
|
| 209 |
+
)
|
| 210 |
+
db.add(prod_img)
|
| 211 |
|
| 212 |
inserted_count += 1
|
|
|
|
|
|
|
| 213 |
|
| 214 |
# 4. Enrich with Real Discovered Products
|
| 215 |
ext_service = ExternalCatalogService(db)
|