Theflame47 commited on
Commit
8551941
·
verified ·
1 Parent(s): d4c150c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -20
app.py CHANGED
@@ -667,12 +667,15 @@ def _create_product_all_variants_with_grid(
667
  return created
668
 
669
 
670
- def _update_product_enable_chunk_with_grid(
671
  logs: List[str],
672
  shop_id: str,
673
  product_id: str,
674
- enabled_variants: List[Dict[str, Any]],
 
675
  upload: Dict[str, Any],
 
 
676
  ) -> Dict[str, Any]:
677
  img_id = upload.get("id")
678
  img_w = float(upload.get("width") or 0)
@@ -682,7 +685,7 @@ def _update_product_enable_chunk_with_grid(
682
  print_areas_payload = []
683
 
684
  enabled_count = 0
685
- for v in enabled_variants:
686
  if not isinstance(v, dict):
687
  continue
688
  vid = v.get("id")
@@ -692,13 +695,28 @@ def _update_product_enable_chunk_with_grid(
692
  vid_i = int(vid)
693
  except Exception:
694
  continue
695
- enabled_count += 1
 
 
696
  variants_payload.append({
697
  "id": vid_i,
698
  "price": 1,
699
- "is_enabled": True,
700
  })
701
 
 
 
 
 
 
 
 
 
 
 
 
 
 
702
  placeholders = v.get("placeholders") or []
703
  if not isinstance(placeholders, list) or not placeholders:
704
  continue
@@ -736,20 +754,23 @@ def _update_product_enable_chunk_with_grid(
736
  })
737
 
738
  if not variants_payload:
739
- raise RuntimeError("No variants in chunk update payload.")
740
-
741
- if not print_areas_payload:
742
- _log(logs, f"PHASE_B_UPDATE_WARN product_id={product_id} reason=no_print_areas_for_chunk enabled={enabled_count}")
743
 
744
- payload = {
745
  "variants": variants_payload,
 
746
  }
747
- if print_areas_payload:
748
- payload["print_areas"] = print_areas_payload
 
 
749
 
750
  _log(
751
  logs,
752
- f"PHASE_B_UPDATE product_id={product_id} enable_variants={len(variants_payload)} "
 
753
  f"print_areas_payload={len(print_areas_payload)}",
754
  )
755
 
@@ -762,7 +783,7 @@ def _update_product_enable_chunk_with_grid(
762
  if not isinstance(upd, dict):
763
  raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
764
 
765
- _log(logs, f"PHASE_B_UPDATE_DONE product_id={product_id}")
766
  return upd
767
 
768
 
@@ -1024,15 +1045,27 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
1024
 
1025
  else:
1026
  total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
 
 
1027
  chunk_index = 0
1028
- offset = 0
1029
 
1030
  first_chunk = variants_all[0:CHUNK_TARGET_VARIANTS]
 
 
 
 
 
 
 
 
 
 
 
1031
  p_info_first = dict(product_info_full)
1032
  p_info_first["variants"] = first_chunk
1033
  p_info_first["options"] = _rebuild_options_for_variants(first_chunk)
1034
  p_info_first["_allVariants"] = variants_all
1035
- p_info_first["_enabledVariantIds"] = [int(v.get("id")) for v in first_chunk if v.get("id") is not None]
1036
 
1037
  _log(
1038
  logs,
@@ -1077,21 +1110,37 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
1077
 
1078
  while offset < total_variants:
1079
  chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
 
 
 
 
 
 
 
 
 
 
1080
 
1081
  _log(
1082
  logs,
1083
- f"PROVIDER_CHUNK_UPDATE provider_id={provider_id} name={provider_name} "
1084
  f"chunk_index={chunk_index} total_chunks={total_chunks} "
1085
  f"offset={offset} size={len(chunk)} total_variants={total_variants} product_id={created_id}",
1086
  )
1087
  yield flush()
1088
 
1089
- upd_enable = _update_product_enable_chunk_with_grid(
 
 
 
1090
  logs,
1091
  shop_id=shop_id,
1092
  product_id=created_id,
1093
- enabled_variants=chunk,
 
1094
  upload=upload,
 
 
1095
  )
1096
  yield flush()
1097
 
@@ -1117,7 +1166,7 @@ def phase_b(currency: str) -> Generator[Tuple[str, str], None, None]:
1117
  "shopVariantCountAfterCreate": len(prod_u.get("variants") or []),
1118
  "shopVariantCountFinal": len(prod_u.get("variants") or []),
1119
  "productId": created_id,
1120
- "priceUpdateResponse": upd_enable,
1121
  }
1122
  )
1123
 
 
667
  return created
668
 
669
 
670
+ def _update_product_stitch_cumulative_with_grid(
671
  logs: List[str],
672
  shop_id: str,
673
  product_id: str,
674
+ all_variants: List[Dict[str, Any]],
675
+ enabled_ids: set,
676
  upload: Dict[str, Any],
677
+ title: Optional[str],
678
+ description: Optional[str],
679
  ) -> Dict[str, Any]:
680
  img_id = upload.get("id")
681
  img_w = float(upload.get("width") or 0)
 
685
  print_areas_payload = []
686
 
687
  enabled_count = 0
688
+ for v in all_variants:
689
  if not isinstance(v, dict):
690
  continue
691
  vid = v.get("id")
 
695
  vid_i = int(vid)
696
  except Exception:
697
  continue
698
+ is_on = vid_i in enabled_ids
699
+ if is_on:
700
+ enabled_count += 1
701
  variants_payload.append({
702
  "id": vid_i,
703
  "price": 1,
704
+ "is_enabled": bool(is_on),
705
  })
706
 
707
+ for v in all_variants:
708
+ if not isinstance(v, dict):
709
+ continue
710
+ vid = v.get("id")
711
+ if vid is None:
712
+ continue
713
+ try:
714
+ vid_i = int(vid)
715
+ except Exception:
716
+ continue
717
+ if vid_i not in enabled_ids:
718
+ continue
719
+
720
  placeholders = v.get("placeholders") or []
721
  if not isinstance(placeholders, list) or not placeholders:
722
  continue
 
754
  })
755
 
756
  if not variants_payload:
757
+ raise RuntimeError("No variants payload could be built for stitch update.")
758
+ if enabled_count > 0 and not print_areas_payload:
759
+ raise RuntimeError("Enabled variants exist but no print_areas payload could be generated (stitch update).")
 
760
 
761
+ payload: Dict[str, Any] = {
762
  "variants": variants_payload,
763
+ "print_areas": print_areas_payload,
764
  }
765
+ if isinstance(title, str) and title:
766
+ payload["title"] = title
767
+ if isinstance(description, str) and description:
768
+ payload["description"] = description
769
 
770
  _log(
771
  logs,
772
+ f"PHASE_B_STITCH_UPDATE product_id={product_id} "
773
+ f"variants_payload_total={len(variants_payload)} enabled={enabled_count} "
774
  f"print_areas_payload={len(print_areas_payload)}",
775
  )
776
 
 
783
  if not isinstance(upd, dict):
784
  raise RuntimeError(f"Unexpected update response: {str(upd)[:500]}")
785
 
786
+ _log(logs, f"PHASE_B_STITCH_UPDATE_DONE product_id={product_id}")
787
  return upd
788
 
789
 
 
1045
 
1046
  else:
1047
  total_chunks = (total_variants + CHUNK_TARGET_VARIANTS - 1) // CHUNK_TARGET_VARIANTS
1048
+
1049
+ enabled_ids: set = set()
1050
  chunk_index = 0
 
1051
 
1052
  first_chunk = variants_all[0:CHUNK_TARGET_VARIANTS]
1053
+ for v in first_chunk:
1054
+ if not isinstance(v, dict):
1055
+ continue
1056
+ vid = v.get("id")
1057
+ if vid is None:
1058
+ continue
1059
+ try:
1060
+ enabled_ids.add(int(vid))
1061
+ except Exception:
1062
+ continue
1063
+
1064
  p_info_first = dict(product_info_full)
1065
  p_info_first["variants"] = first_chunk
1066
  p_info_first["options"] = _rebuild_options_for_variants(first_chunk)
1067
  p_info_first["_allVariants"] = variants_all
1068
+ p_info_first["_enabledVariantIds"] = sorted(list(enabled_ids))
1069
 
1070
  _log(
1071
  logs,
 
1110
 
1111
  while offset < total_variants:
1112
  chunk = variants_all[offset: offset + CHUNK_TARGET_VARIANTS]
1113
+ for v in chunk:
1114
+ if not isinstance(v, dict):
1115
+ continue
1116
+ vid = v.get("id")
1117
+ if vid is None:
1118
+ continue
1119
+ try:
1120
+ enabled_ids.add(int(vid))
1121
+ except Exception:
1122
+ continue
1123
 
1124
  _log(
1125
  logs,
1126
+ f"PROVIDER_CHUNK_STITCH provider_id={provider_id} name={provider_name} "
1127
  f"chunk_index={chunk_index} total_chunks={total_chunks} "
1128
  f"offset={offset} size={len(chunk)} total_variants={total_variants} product_id={created_id}",
1129
  )
1130
  yield flush()
1131
 
1132
+ current_prod = _get_product(logs, shop_id, created_id)
1133
+ yield flush()
1134
+
1135
+ upd_stitch = _update_product_stitch_cumulative_with_grid(
1136
  logs,
1137
  shop_id=shop_id,
1138
  product_id=created_id,
1139
+ all_variants=variants_all,
1140
+ enabled_ids=enabled_ids,
1141
  upload=upload,
1142
+ title=current_prod.get("title"),
1143
+ description=current_prod.get("description"),
1144
  )
1145
  yield flush()
1146
 
 
1166
  "shopVariantCountAfterCreate": len(prod_u.get("variants") or []),
1167
  "shopVariantCountFinal": len(prod_u.get("variants") or []),
1168
  "productId": created_id,
1169
+ "priceUpdateResponse": upd_stitch,
1170
  }
1171
  )
1172