Theflame47 commited on
Commit
e7d4a8d
·
verified ·
1 Parent(s): 47e793a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -124
app.py CHANGED
@@ -581,11 +581,12 @@ def _create_product_all_variants_with_grid(
581
  return created
582
 
583
 
584
- def _update_product_enable_variants_with_grid(
585
  logs: List[str],
586
  shop_id: str,
587
  product_id: str,
588
- variants_to_enable: List[Dict[str, Any]],
 
589
  upload: Dict[str, Any],
590
  ) -> Dict[str, Any]:
591
  img_id = upload.get("id")
@@ -595,12 +596,8 @@ def _update_product_enable_variants_with_grid(
595
  variants_payload = []
596
  print_areas_payload = []
597
 
598
- enable_attempted = 0
599
- enable_ready = 0
600
- skipped_no_placeholders = 0
601
- skipped_bad_placeholder = 0
602
-
603
- for v in variants_to_enable:
604
  if not isinstance(v, dict):
605
  continue
606
  vid = v.get("id")
@@ -610,12 +607,30 @@ def _update_product_enable_variants_with_grid(
610
  vid_i = int(vid)
611
  except Exception:
612
  continue
 
 
 
 
 
 
 
 
613
 
614
- enable_attempted += 1
 
 
 
 
 
 
 
 
 
 
 
615
 
616
  placeholders = v.get("placeholders") or []
617
  if not isinstance(placeholders, list) or not placeholders:
618
- skipped_no_placeholders += 1
619
  continue
620
 
621
  ph_payload = []
@@ -626,13 +641,11 @@ def _update_product_enable_variants_with_grid(
626
  pw = ph.get("width")
627
  phh = ph.get("height")
628
  if not pos or pw is None or phh is None:
629
- skipped_bad_placeholder += 1
630
  continue
631
  try:
632
  pwf = float(pw)
633
  phf = float(phh)
634
  except Exception:
635
- skipped_bad_placeholder += 1
636
  continue
637
  scale = _scale_fill(pwf, phf, img_w, img_h)
638
  ph_payload.append({
@@ -646,46 +659,31 @@ def _update_product_enable_variants_with_grid(
646
  }],
647
  })
648
 
649
- if not ph_payload:
650
- skipped_bad_placeholder += 1
651
- continue
652
-
653
- enable_ready += 1
654
- variants_payload.append({
655
- "id": vid_i,
656
- "price": 1,
657
- "is_enabled": True,
658
- })
659
- print_areas_payload.append({
660
- "variant_ids": [vid_i],
661
- "placeholders": ph_payload,
662
- })
663
 
664
- if not variants_payload or not print_areas_payload:
665
- raise RuntimeError(
666
- f"Update chunk produced no enableable variants. attempted={enable_attempted} "
667
- f"ready={enable_ready} skipped_no_placeholders={skipped_no_placeholders} skipped_bad_placeholder={skipped_bad_placeholder}"
668
- )
669
 
670
  _log(
671
  logs,
672
- f"PHASE_B_UPDATE product_id={product_id} enable_attempted={enable_attempted} enable_ready={enable_ready} "
673
- f"print_areas_payload={len(print_areas_payload)} skipped_no_placeholders={skipped_no_placeholders} skipped_bad_placeholder={skipped_bad_placeholder}",
674
  )
675
 
676
  upd = _req(
677
  "PUT",
678
  f"/v1/shops/{shop_id}/products/{product_id}.json",
679
- json_body={
680
- "variants": variants_payload,
681
- "print_areas": print_areas_payload,
682
- },
683
  logs=logs,
684
  )
685
  if not isinstance(upd, dict):
686
  raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
687
 
688
- _log(logs, f"PHASE_B_UPDATED product_id={product_id} enabled_in_update={len(variants_payload)}")
689
  return upd
690
 
691
 
@@ -878,16 +876,19 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
878
  continue
879
 
880
  total_variants = len(variants_all)
881
- within_threshold = total_variants <= CHUNK_TARGET_VARIANTS
882
 
883
  _log(
884
  logs,
885
- f"THRESHOLD_CHECK threshold={CHUNK_TARGET_VARIANTS} total_variants={total_variants} "
886
- f"flag={'WITHIN_THRESHOLD' if within_threshold else 'OUTSIDE_THRESHOLD'}",
887
  )
888
  yield flush()
889
 
890
- if within_threshold:
 
 
 
891
  chunk_index = 0
892
  total_chunks = 1
893
  p_info_chunk = dict(product_info_full)
@@ -948,32 +949,46 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
948
  )
949
 
950
  else:
951
- total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
 
 
 
 
 
 
 
 
 
 
 
952
 
953
- first_chunk = variants_all[0:CHUNK_TARGET_VARIANTS]
954
- surplus_variants = variants_all[CHUNK_TARGET_VARIANTS:]
 
 
 
955
 
956
- surplus_ids = []
957
- for v in surplus_variants[:50]:
958
- if isinstance(v, dict) and v.get("id") is not None:
959
- surplus_ids.append(v.get("id"))
960
  _log(
961
  logs,
962
- f"OUTSIDE_THRESHOLD_SURPLUS threshold={CHUNK_TARGET_VARIANTS} total_variants={total_variants} "
963
- f"surplus_count={len(surplus_variants)} surplus_ids_sample={json.dumps(surplus_ids)}",
 
 
 
 
964
  )
965
  yield flush()
966
 
967
- p_info_first = dict(product_info_full)
968
- p_info_first["variants"] = first_chunk
969
- p_info_first["options"] = _rebuild_options_for_variants(first_chunk)
970
- p_info_first["_allVariants"] = variants_all
971
- p_info_first["_enabledVariantIds"] = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
972
 
973
  _log(
974
  logs,
975
  f"PROVIDER_CREATE_THEN_UPDATE provider_id={provider_id} name={provider_name} "
976
- f"first_chunk_size={len(first_chunk)} total_chunks={total_chunks} total_variants={total_variants}",
977
  )
978
  yield flush()
979
 
@@ -981,7 +996,7 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
981
  logs,
982
  shop_id=shop_id,
983
  blob=blob,
984
- product_info=p_info_first,
985
  upload=upload,
986
  )
987
  created_id = str(created.get("id") or "")
@@ -989,79 +1004,30 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
989
  raise RuntimeError("Created product response missing id.")
990
  yield flush()
991
 
992
- chunk_index = 0
993
- provider_runs.append(
994
- {
995
- "providerId": provider_id,
996
- "providerName": provider_name,
997
- "chunkIndex": chunk_index,
998
- "totalChunks": total_chunks,
999
- "catalogVariantCountTotal": total_variants,
1000
- "catalogVariantCountChunk": len(first_chunk),
1001
- "shopVariantCountAfterCreate": 0,
1002
- "shopVariantCountFinal": 0,
1003
- "productId": created_id,
1004
- "priceUpdateResponse": {},
1005
- }
1006
- )
1007
 
1008
- offset = CHUNK_TARGET_VARIANTS
1009
- chunk_index = 1
1010
-
1011
- while offset < total_variants:
1012
- chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
1013
- chunk_ids = []
1014
- for v in chunk[:50]:
1015
- if isinstance(v, dict) and v.get("id") is not None:
1016
- chunk_ids.append(v.get("id"))
1017
-
1018
- _log(
1019
- logs,
1020
- f"PROVIDER_UPDATE_CHUNK provider_id={provider_id} name={provider_name} "
1021
- f"chunk_index={chunk_index} total_chunks={total_chunks} "
1022
- f"offset={offset} size={len(chunk)} product_id={created_id} ids_sample={json.dumps(chunk_ids)}",
1023
- )
1024
- yield flush()
1025
-
1026
- upd_chunk = _update_product_enable_variants_with_grid(
1027
- logs,
1028
- shop_id=shop_id,
1029
- product_id=created_id,
1030
- variants_to_enable=chunk,
1031
- upload=upload,
1032
- )
1033
- yield flush()
1034
-
1035
- provider_runs.append(
1036
- {
1037
- "providerId": provider_id,
1038
- "providerName": provider_name,
1039
- "chunkIndex": chunk_index,
1040
- "totalChunks": total_chunks,
1041
- "catalogVariantCountTotal": total_variants,
1042
- "catalogVariantCountChunk": len(chunk),
1043
- "shopVariantCountAfterCreate": 0,
1044
- "shopVariantCountFinal": 0,
1045
- "productId": created_id,
1046
- "priceUpdateResponse": upd_chunk,
1047
- }
1048
- )
1049
-
1050
- offset += CHUNK_TARGET_VARIANTS
1051
- chunk_index += 1
1052
 
1053
  prod1 = _get_product(logs, shop_id, created_id)
1054
  yield flush()
1055
 
1056
  _log(
1057
  logs,
1058
- f"COMPARE_COUNTS provider={provider_id} chunk_index=FINAL "
1059
- f"catalog_total={len(variants_all)} "
1060
  f"shop={len(prod1.get('variants') or [])}",
1061
  )
1062
  yield flush()
1063
 
1064
- upd_prices = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
1065
  yield flush()
1066
 
1067
  prod2 = _get_product(logs, shop_id, created_id)
@@ -1071,14 +1037,14 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
1071
  {
1072
  "providerId": provider_id,
1073
  "providerName": provider_name,
1074
- "chunkIndex": "PRICE_UPDATE",
1075
- "totalChunks": total_chunks,
1076
  "catalogVariantCountTotal": total_variants,
1077
- "catalogVariantCountChunk": 0,
1078
  "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
1079
  "shopVariantCountFinal": len(prod2.get("variants") or []),
1080
  "productId": created_id,
1081
- "priceUpdateResponse": upd_prices,
1082
  }
1083
  )
1084
 
 
581
  return created
582
 
583
 
584
+ def _update_product_all_variants_with_grid(
585
  logs: List[str],
586
  shop_id: str,
587
  product_id: str,
588
+ all_variants: List[Dict[str, Any]],
589
+ enabled_ids: set,
590
  upload: Dict[str, Any],
591
  ) -> Dict[str, Any]:
592
  img_id = upload.get("id")
 
596
  variants_payload = []
597
  print_areas_payload = []
598
 
599
+ enabled_count = 0
600
+ for v in all_variants:
 
 
 
 
601
  if not isinstance(v, dict):
602
  continue
603
  vid = v.get("id")
 
607
  vid_i = int(vid)
608
  except Exception:
609
  continue
610
+ is_on = vid_i in enabled_ids
611
+ if is_on:
612
+ enabled_count += 1
613
+ variants_payload.append({
614
+ "id": vid_i,
615
+ "price": 1,
616
+ "is_enabled": bool(is_on),
617
+ })
618
 
619
+ for v in all_variants:
620
+ if not isinstance(v, dict):
621
+ continue
622
+ vid = v.get("id")
623
+ if vid is None:
624
+ continue
625
+ try:
626
+ vid_i = int(vid)
627
+ except Exception:
628
+ continue
629
+ if vid_i not in enabled_ids:
630
+ continue
631
 
632
  placeholders = v.get("placeholders") or []
633
  if not isinstance(placeholders, list) or not placeholders:
 
634
  continue
635
 
636
  ph_payload = []
 
641
  pw = ph.get("width")
642
  phh = ph.get("height")
643
  if not pos or pw is None or phh is None:
 
644
  continue
645
  try:
646
  pwf = float(pw)
647
  phf = float(phh)
648
  except Exception:
 
649
  continue
650
  scale = _scale_fill(pwf, phf, img_w, img_h)
651
  ph_payload.append({
 
659
  }],
660
  })
661
 
662
+ if ph_payload:
663
+ print_areas_payload.append({
664
+ "variant_ids": [vid_i],
665
+ "placeholders": ph_payload,
666
+ })
 
 
 
 
 
 
 
 
 
667
 
668
+ if not variants_payload:
669
+ raise RuntimeError("No variants payload could be built for update.")
 
 
 
670
 
671
  _log(
672
  logs,
673
+ f"PHASE_B_UPDATE product_id={product_id} variants_payload_total={len(variants_payload)} "
674
+ f"enabled={enabled_count} print_areas_payload={len(print_areas_payload)}",
675
  )
676
 
677
  upd = _req(
678
  "PUT",
679
  f"/v1/shops/{shop_id}/products/{product_id}.json",
680
+ json_body={"variants": variants_payload, "print_areas": print_areas_payload},
 
 
 
681
  logs=logs,
682
  )
683
  if not isinstance(upd, dict):
684
  raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
685
 
686
+ _log(logs, f"PHASE_B_UPDATE_DONE product_id={product_id}")
687
  return upd
688
 
689
 
 
876
  continue
877
 
878
  total_variants = len(variants_all)
879
+ threshold = int(CHUNK_TARGET_VARIANTS)
880
 
881
  _log(
882
  logs,
883
+ f"THRESHOLD_CHECK provider_id={provider_id} total_variants={total_variants} "
884
+ f"threshold={threshold} hard_max={HARD_MAX_VARIANTS_PER_PRODUCT}",
885
  )
886
  yield flush()
887
 
888
+ if total_variants <= threshold:
889
+ _log(logs, f"WITHIN_THRESHOLD provider_id={provider_id} total_variants={total_variants} threshold={threshold}")
890
+ yield flush()
891
+
892
  chunk_index = 0
893
  total_chunks = 1
894
  p_info_chunk = dict(product_info_full)
 
949
  )
950
 
951
  else:
952
+ _log(logs, f"OUTSIDE_THRESHOLD provider_id={provider_id} total_variants={total_variants} threshold={threshold}")
953
+ yield flush()
954
+
955
+ cap = int(HARD_MAX_VARIANTS_PER_PRODUCT)
956
+ if total_variants > cap:
957
+ _log(
958
+ logs,
959
+ f"CAP_ENFORCED provider_id={provider_id} total_variants={total_variants} cap={cap} dropped={total_variants - cap}",
960
+ )
961
+ yield flush()
962
+ variants_all = variants_all[:cap]
963
+ total_variants = len(variants_all)
964
 
965
+ first_chunk = variants_all[:threshold]
966
+ surplus = variants_all[threshold:]
967
+
968
+ enabled_ids_create = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
969
+ enabled_ids_final = [int(v.get("id")) for v in variants_all if v.get("id") is not None]
970
 
 
 
 
 
971
  _log(
972
  logs,
973
+ f"SURPLUS_COUNT provider_id={provider_id} create_enabled={len(enabled_ids_create)} "
974
+ f"surplus={len(surplus)} final_enabled={len(enabled_ids_final)}",
975
+ )
976
+ _log(
977
+ logs,
978
+ f"SURPLUS_IDS_SAMPLE provider_id={provider_id} ids={json.dumps(enabled_ids_final[threshold:threshold+25])}",
979
  )
980
  yield flush()
981
 
982
+ p_info_create = dict(product_info_full)
983
+ p_info_create["variants"] = first_chunk
984
+ p_info_create["options"] = _rebuild_options_for_variants(first_chunk)
985
+ p_info_create["_allVariants"] = variants_all
986
+ p_info_create["_enabledVariantIds"] = enabled_ids_create
987
 
988
  _log(
989
  logs,
990
  f"PROVIDER_CREATE_THEN_UPDATE provider_id={provider_id} name={provider_name} "
991
+ f"create_enabled={len(first_chunk)} total_variants={total_variants}",
992
  )
993
  yield flush()
994
 
 
996
  logs,
997
  shop_id=shop_id,
998
  blob=blob,
999
+ product_info=p_info_create,
1000
  upload=upload,
1001
  )
1002
  created_id = str(created.get("id") or "")
 
1004
  raise RuntimeError("Created product response missing id.")
1005
  yield flush()
1006
 
1007
+ enabled_set_final = set(enabled_ids_final)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1008
 
1009
+ upd_variants = _update_product_all_variants_with_grid(
1010
+ logs,
1011
+ shop_id=shop_id,
1012
+ product_id=created_id,
1013
+ all_variants=variants_all,
1014
+ enabled_ids=enabled_set_final,
1015
+ upload=upload,
1016
+ )
1017
+ yield flush()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1018
 
1019
  prod1 = _get_product(logs, shop_id, created_id)
1020
  yield flush()
1021
 
1022
  _log(
1023
  logs,
1024
+ f"COMPARE_COUNTS provider={provider_id} chunk_index=THRESHOLD_FALLBACK "
1025
+ f"catalog_chunk={total_variants} "
1026
  f"shop={len(prod1.get('variants') or [])}",
1027
  )
1028
  yield flush()
1029
 
1030
+ upd = _update_prices_to_cost_plus_margin(logs, shop_id, created_id, prod1)
1031
  yield flush()
1032
 
1033
  prod2 = _get_product(logs, shop_id, created_id)
 
1037
  {
1038
  "providerId": provider_id,
1039
  "providerName": provider_name,
1040
+ "chunkIndex": "THRESHOLD_FALLBACK",
1041
+ "totalChunks": 1,
1042
  "catalogVariantCountTotal": total_variants,
1043
+ "catalogVariantCountChunk": total_variants,
1044
  "shopVariantCountAfterCreate": len(prod1.get("variants") or []),
1045
  "shopVariantCountFinal": len(prod2.get("variants") or []),
1046
  "productId": created_id,
1047
+ "priceUpdateResponse": {"enableUpdate": upd_variants, "priceUpdate": upd},
1048
  }
1049
  )
1050