Theflame47 commited on
Commit
ec76af3
·
verified ·
1 Parent(s): 0f5e782

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -1
app.py CHANGED
@@ -448,6 +448,49 @@ def _log_catalog_variants(logs: List[str], product_info: Dict[str, Any], limit:
448
  break
449
 
450
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  def _create_product_all_variants_with_grid(
452
  logs: List[str],
453
  shop_id: str,
@@ -487,6 +530,15 @@ def _create_product_all_variants_with_grid(
487
 
488
  fp = _batch_fingerprint(str(shop_id), int(bp_id), int(provider_id), sorted(list(enabled_ids)))
489
 
 
 
 
 
 
 
 
 
 
490
  img_id = upload.get("id")
491
  img_w = float(upload.get("width") or 0)
492
  img_h = float(upload.get("height") or 0)
@@ -595,7 +647,6 @@ def _create_product_all_variants_with_grid(
595
  f"/v1/shops/{shop_id}/products.json",
596
  json_body=payload,
597
  logs=logs,
598
- extra_headers={"Idempotency-Key": fp},
599
  )
600
  if not isinstance(created, Dict):
601
  raise RuntimeError(f"Unexpected create response: {str(created)[:500]}")
 
448
  break
449
 
450
 
451
+ def _find_existing_product_by_fp(
452
+ logs: List[str],
453
+ shop_id: str,
454
+ fp: str,
455
+ ) -> Optional[Dict[str, Any]]:
456
+ page = 1
457
+ while True:
458
+ resp = _req(
459
+ "GET",
460
+ f"/v1/shops/{shop_id}/products.json?page={page}&limit=50",
461
+ logs=logs,
462
+ )
463
+ if isinstance(resp, dict):
464
+ items = resp.get("data") or resp.get("products") or resp.get("items")
465
+ if isinstance(items, list):
466
+ products = items
467
+ else:
468
+ products = []
469
+ elif isinstance(resp, list):
470
+ products = resp
471
+ else:
472
+ products = []
473
+
474
+ if not products:
475
+ break
476
+
477
+ for p in products:
478
+ if not isinstance(p, dict):
479
+ continue
480
+ desc = p.get("description") or ""
481
+ if isinstance(desc, str) and f"BATCH_FP={fp}" in desc:
482
+ _log(
483
+ logs,
484
+ f"FOUND_EXISTING_PRODUCT_FOR_FP shop_id={shop_id} product_id={p.get('id')} batch_fp={fp}",
485
+ )
486
+ return p
487
+
488
+ page += 1
489
+
490
+ _log(logs, f"NO_EXISTING_PRODUCT_FOR_FP shop_id={shop_id} batch_fp={fp}")
491
+ return None
492
+
493
+
494
  def _create_product_all_variants_with_grid(
495
  logs: List[str],
496
  shop_id: str,
 
530
 
531
  fp = _batch_fingerprint(str(shop_id), int(bp_id), int(provider_id), sorted(list(enabled_ids)))
532
 
533
+ existing = _find_existing_product_by_fp(logs, shop_id, fp)
534
+ if isinstance(existing, dict) and existing.get("id"):
535
+ _log(
536
+ logs,
537
+ f"PHASE_B_REUSE_EXISTING_PRODUCT shop_id={shop_id} blueprint_id={bp_id} "
538
+ f"provider_id={provider_id} existing_product_id={existing.get('id')} batch_fp={fp}",
539
+ )
540
+ return existing
541
+
542
  img_id = upload.get("id")
543
  img_w = float(upload.get("width") or 0)
544
  img_h = float(upload.get("height") or 0)
 
647
  f"/v1/shops/{shop_id}/products.json",
648
  json_body=payload,
649
  logs=logs,
 
650
  )
651
  if not isinstance(created, Dict):
652
  raise RuntimeError(f"Unexpected create response: {str(created)[:500]}")