hisham-cmd commited on
Commit
e89f417
·
verified ·
1 Parent(s): f1db8b0

Update backend with high-quality catalog and fixes

Browse files
Files changed (1) hide show
  1. app/api/seed.py +62 -4
app/api/seed.py CHANGED
@@ -105,7 +105,20 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
105
 
106
  try:
107
  with open(json_path, "r", encoding="utf-8") as f:
108
- products_data = json.load(f)
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  # 1. Create Parent Categories
111
  parent_cats = {}
@@ -148,10 +161,25 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
148
  skipped_count = 0
149
 
150
  for item in products_data:
151
- cat_name = item.get("category_name")
 
 
 
 
 
 
152
  if not cat_name or cat_name not in sub_cats:
153
  # Automatic fallback mapping for consistency
154
- if not cat_name: continue
 
 
 
 
 
 
 
 
 
155
  # Try to find best match
156
  matched = False
157
  for sub, info in sub_cats.items():
@@ -171,6 +199,36 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
171
 
172
  sub_cat_info = sub_cats[cat_name]
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  prod = Product(
175
  name_ar=product_name_ar,
176
  name_en=product_name_en,
@@ -184,7 +242,7 @@ async def seed_database(clear: bool = False, db: Session = Depends(get_db)):
184
  rating_count=item.get("rating_count", 20),
185
  is_featured=item.get("is_featured", False),
186
  is_active=item.get("is_active", True),
187
- specs=item.get("specs", {})
188
  )
189
  db.add(prod)
190
  db.flush()
 
105
 
106
  try:
107
  with open(json_path, "r", encoding="utf-8") as f:
108
+ data = json.load(f)
109
+
110
+ # Handle both formats: {"products": [...]} or direct array [...} with metadata
111
+ if isinstance(data, dict) and "products" in data:
112
+ products_data = data["products"]
113
+ elif isinstance(data, list):
114
+ products_data = data
115
+ else:
116
+ return AuthResponse(
117
+ isSuccess=False,
118
+ value={"message": "Invalid JSON format"},
119
+ statusCode=400,
120
+ error="Invalid format"
121
+ )
122
 
123
  # 1. Create Parent Categories
124
  parent_cats = {}
 
161
  skipped_count = 0
162
 
163
  for item in products_data:
164
+ # Skip non-dict items (handle malformed JSON items)
165
+ if not isinstance(item, dict):
166
+ skipped_count += 1
167
+ continue
168
+
169
+ # Handle both "category" and "category_name" fields
170
+ cat_name = item.get("category_name") or item.get("category")
171
  if not cat_name or cat_name not in sub_cats:
172
  # Automatic fallback mapping for consistency
173
+ if not cat_name:
174
+ # Try to use category from specs if available - safely handle if specs is not a dict
175
+ specs_val = item.get("specs")
176
+ specs_category = None
177
+ if isinstance(specs_val, dict):
178
+ specs_category = specs_val.get("category")
179
+ if specs_category and specs_category in sub_cats:
180
+ cat_name = specs_category
181
+ else:
182
+ continue
183
  # Try to find best match
184
  matched = False
185
  for sub, info in sub_cats.items():
 
199
 
200
  sub_cat_info = sub_cats[cat_name]
201
 
202
+ # Build specs - include variants from options if present
203
+ specs = {}
204
+ raw_specs = item.get("specs", {})
205
+
206
+ # Ensure specs is a dict, not a string
207
+ if isinstance(raw_specs, dict):
208
+ specs = raw_specs.copy()
209
+ elif raw_specs:
210
+ specs = {"data": str(raw_specs)}
211
+
212
+ # Convert options to variants in specs if not already present
213
+ if "variants" not in specs and "options" in item:
214
+ options = item.get("options", [])
215
+ if options and isinstance(options, list):
216
+ variants = []
217
+ for opt in options:
218
+ # Ensure opt is a dict
219
+ if not isinstance(opt, dict):
220
+ continue
221
+ variants.append({
222
+ "id": opt.get("sku", opt.get("id", "v_default")),
223
+ "name_en": opt.get("name_en", ""),
224
+ "name_ar": opt.get("name_ar", ""),
225
+ "price_modifier": float(opt.get("price", 0)) - float(item.get("price", 0)) if "price" in opt else 0,
226
+ "stock": opt.get("stock", 10),
227
+ "image_url": opt.get("image", opt.get("image_url", ""))
228
+ })
229
+ if variants:
230
+ specs["variants"] = variants
231
+
232
  prod = Product(
233
  name_ar=product_name_ar,
234
  name_en=product_name_en,
 
242
  rating_count=item.get("rating_count", 20),
243
  is_featured=item.get("is_featured", False),
244
  is_active=item.get("is_active", True),
245
+ specs=specs if specs else None
246
  )
247
  db.add(prod)
248
  db.flush()