Theflame47 commited on
Commit
ae62b83
·
verified ·
1 Parent(s): f288718

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -9
app.py CHANGED
@@ -441,10 +441,31 @@ def _create_product_all_variants_with_grid(
441
  if bp_id is None or provider_id is None:
442
  raise RuntimeError("Missing blueprint or provider id.")
443
 
444
- variants = product_info.get("variants") or []
445
- if not isinstance(variants, list) or not variants:
446
  raise RuntimeError("No variants to create product with.")
447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  img_id = upload.get("id")
449
  img_w = float(upload.get("width") or 0)
450
  img_h = float(upload.get("height") or 0)
@@ -452,19 +473,39 @@ def _create_product_all_variants_with_grid(
452
  variants_payload = []
453
  print_areas_payload = []
454
 
455
- for v in variants:
 
456
  if not isinstance(v, dict):
457
  continue
458
  vid = v.get("id")
459
  if vid is None:
460
  continue
461
-
 
 
 
 
 
 
462
  variants_payload.append({
463
- "id": int(vid),
464
  "price": 1,
465
- "is_enabled": True,
466
  })
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  placeholders = v.get("placeholders") or []
469
  if not isinstance(placeholders, list) or not placeholders:
470
  continue
@@ -497,14 +538,14 @@ def _create_product_all_variants_with_grid(
497
 
498
  if ph_payload:
499
  print_areas_payload.append({
500
- "variant_ids": [int(vid)],
501
  "placeholders": ph_payload,
502
  })
503
 
504
  if not variants_payload:
505
  raise RuntimeError("No valid variant ids to create product with.")
506
  if not print_areas_payload:
507
- raise RuntimeError("No placeholders payload generated for any variant.")
508
 
509
  provider_details = blob.get("providerDetails") or {}
510
  provider_name = provider_details.get("title") or provider_details.get("name") or str(provider_id)
@@ -524,7 +565,7 @@ def _create_product_all_variants_with_grid(
524
  _log(
525
  logs,
526
  f"PHASE_B_CREATE shop_id={shop_id} blueprint_id={bp_id} provider_id={provider_id} "
527
- f"variants_payload={len(variants_payload)} print_areas_payload={len(print_areas_payload)}",
528
  )
529
 
530
  created = _req(
@@ -583,6 +624,8 @@ def _update_prices_to_cost_plus_margin(
583
  for v in variants:
584
  if not isinstance(v, dict):
585
  continue
 
 
586
  vid = v.get("id")
587
  cost = v.get("cost")
588
  if vid is None or cost is None:
@@ -734,6 +777,8 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
734
  p_info_chunk = dict(product_info_full)
735
  p_info_chunk["variants"] = variants_all
736
  p_info_chunk["options"] = _rebuild_options_for_variants(variants_all)
 
 
737
 
738
  _log(
739
  logs,
@@ -795,6 +840,8 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
795
  p_info_chunk = dict(product_info_full)
796
  p_info_chunk["variants"] = chunk
797
  p_info_chunk["options"] = _rebuild_options_for_variants(chunk)
 
 
798
 
799
  _log(
800
  logs,
 
441
  if bp_id is None or provider_id is None:
442
  raise RuntimeError("Missing blueprint or provider id.")
443
 
444
+ enabled_variants = product_info.get("variants") or []
445
+ if not isinstance(enabled_variants, list) or not enabled_variants:
446
  raise RuntimeError("No variants to create product with.")
447
 
448
+ all_variants = product_info.get("_allVariants") or enabled_variants
449
+ if not isinstance(all_variants, list) or not all_variants:
450
+ raise RuntimeError("No all-variants universe found for this provider.")
451
+
452
+ enabled_ids_raw = product_info.get("_enabledVariantIds") or []
453
+ enabled_ids = set()
454
+ for x in enabled_ids_raw:
455
+ try:
456
+ enabled_ids.add(int(x))
457
+ except Exception:
458
+ pass
459
+ if not enabled_ids:
460
+ for v in enabled_variants:
461
+ vid = v.get("id")
462
+ if vid is None:
463
+ continue
464
+ try:
465
+ enabled_ids.add(int(vid))
466
+ except Exception:
467
+ continue
468
+
469
  img_id = upload.get("id")
470
  img_w = float(upload.get("width") or 0)
471
  img_h = float(upload.get("height") or 0)
 
473
  variants_payload = []
474
  print_areas_payload = []
475
 
476
+ enabled_count = 0
477
+ for v in all_variants:
478
  if not isinstance(v, dict):
479
  continue
480
  vid = v.get("id")
481
  if vid is None:
482
  continue
483
+ try:
484
+ vid_i = int(vid)
485
+ except Exception:
486
+ continue
487
+ is_on = vid_i in enabled_ids
488
+ if is_on:
489
+ enabled_count += 1
490
  variants_payload.append({
491
+ "id": vid_i,
492
  "price": 1,
493
+ "is_enabled": bool(is_on),
494
  })
495
 
496
+ for v in enabled_variants:
497
+ if not isinstance(v, dict):
498
+ continue
499
+ vid = v.get("id")
500
+ if vid is None:
501
+ continue
502
+ try:
503
+ vid_i = int(vid)
504
+ except Exception:
505
+ continue
506
+ if vid_i not in enabled_ids:
507
+ continue
508
+
509
  placeholders = v.get("placeholders") or []
510
  if not isinstance(placeholders, list) or not placeholders:
511
  continue
 
538
 
539
  if ph_payload:
540
  print_areas_payload.append({
541
+ "variant_ids": [vid_i],
542
  "placeholders": ph_payload,
543
  })
544
 
545
  if not variants_payload:
546
  raise RuntimeError("No valid variant ids to create product with.")
547
  if not print_areas_payload:
548
+ raise RuntimeError("No placeholders payload generated for any enabled variant.")
549
 
550
  provider_details = blob.get("providerDetails") or {}
551
  provider_name = provider_details.get("title") or provider_details.get("name") or str(provider_id)
 
565
  _log(
566
  logs,
567
  f"PHASE_B_CREATE shop_id={shop_id} blueprint_id={bp_id} provider_id={provider_id} "
568
+ f"variants_payload_total={len(variants_payload)} enabled={enabled_count} print_areas_payload={len(print_areas_payload)}",
569
  )
570
 
571
  created = _req(
 
624
  for v in variants:
625
  if not isinstance(v, dict):
626
  continue
627
+ if v.get("is_enabled") is not True:
628
+ continue
629
  vid = v.get("id")
630
  cost = v.get("cost")
631
  if vid is None or cost is None:
 
777
  p_info_chunk = dict(product_info_full)
778
  p_info_chunk["variants"] = variants_all
779
  p_info_chunk["options"] = _rebuild_options_for_variants(variants_all)
780
+ p_info_chunk["_allVariants"] = variants_all
781
+ p_info_chunk["_enabledVariantIds"] = [int(v.get("id")) for v in variants_all if v.get("id") is not None]
782
 
783
  _log(
784
  logs,
 
840
  p_info_chunk = dict(product_info_full)
841
  p_info_chunk["variants"] = chunk
842
  p_info_chunk["options"] = _rebuild_options_for_variants(chunk)
843
+ p_info_chunk["_allVariants"] = variants_all
844
+ p_info_chunk["_enabledVariantIds"] = [int(v.get("id")) for v in chunk if v.get("id") is not None]
845
 
846
  _log(
847
  logs,