Theflame47 commited on
Commit
b21d0bb
·
verified ·
1 Parent(s): 0e949d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py CHANGED
@@ -85,6 +85,21 @@ def _build_product(blob: Dict[str, Any], currency: str, logs: List[str]) -> Dict
85
  for v in variants:
86
  cents = v.get("price")
87
  cents = int(cents) if isinstance(cents, (int, str)) and str(cents).isdigit() else None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  snapshot.append({
89
  "id": v.get("id"),
90
  "sku": v.get("sku"),
@@ -92,10 +107,41 @@ def _build_product(blob: Dict[str, Any], currency: str, logs: List[str]) -> Dict
92
  "color": (v.get("options") or {}).get("color"),
93
  "priceCents": cents,
94
  "price": round(cents / 100, 2) if cents is not None else None,
 
95
  })
96
 
97
  _log(logs, f"VARIANT_SNAPSHOT sample={json.dumps(snapshot[:20])}")
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  all_cents = [v["priceCents"] for v in snapshot if v["priceCents"] is not None]
100
  min_price = round(min(all_cents) / 100, 2) if all_cents else DEFAULT_BASE_PRICE
101
 
@@ -122,6 +168,11 @@ def _build_product(blob: Dict[str, Any], currency: str, logs: List[str]) -> Dict
122
  },
123
  "description": description,
124
  "variants": snapshot,
 
 
 
 
 
125
  "raw": blob,
126
  }
127
 
 
85
  for v in variants:
86
  cents = v.get("price")
87
  cents = int(cents) if isinstance(cents, (int, str)) and str(cents).isdigit() else None
88
+
89
+ ph = v.get("placeholders")
90
+ if not isinstance(ph, list):
91
+ ph = []
92
+
93
+ placeholders = []
94
+ for x in ph:
95
+ if not isinstance(x, dict):
96
+ continue
97
+ placeholders.append({
98
+ "position": x.get("position"),
99
+ "width": x.get("width"),
100
+ "height": x.get("height"),
101
+ })
102
+
103
  snapshot.append({
104
  "id": v.get("id"),
105
  "sku": v.get("sku"),
 
107
  "color": (v.get("options") or {}).get("color"),
108
  "priceCents": cents,
109
  "price": round(cents / 100, 2) if cents is not None else None,
110
+ "placeholders": placeholders,
111
  })
112
 
113
  _log(logs, f"VARIANT_SNAPSHOT sample={json.dumps(snapshot[:20])}")
114
 
115
+ print_area_sample = []
116
+ for s in snapshot[:20]:
117
+ print_area_sample.append({
118
+ "id": s.get("id"),
119
+ "size": s.get("size"),
120
+ "color": s.get("color"),
121
+ "placeholders": s.get("placeholders"),
122
+ })
123
+ _log(logs, f"PRINT_AREA_SNAPSHOT sample={json.dumps(print_area_sample)}")
124
+
125
+ print_areas_by_variant: Dict[str, Any] = {}
126
+ for s in snapshot:
127
+ vid = s.get("id")
128
+ if vid is None:
129
+ continue
130
+ per_pos: Dict[str, Any] = {}
131
+ for ph in (s.get("placeholders") or []):
132
+ pos = ph.get("position")
133
+ if not pos:
134
+ continue
135
+ per_pos[str(pos)] = {"width": ph.get("width"), "height": ph.get("height")}
136
+ print_areas_by_variant[str(vid)] = per_pos
137
+
138
+ all_positions = sorted({
139
+ ph.get("position")
140
+ for s in snapshot
141
+ for ph in (s.get("placeholders") or [])
142
+ if ph.get("position")
143
+ })
144
+
145
  all_cents = [v["priceCents"] for v in snapshot if v["priceCents"] is not None]
146
  min_price = round(min(all_cents) / 100, 2) if all_cents else DEFAULT_BASE_PRICE
147
 
 
168
  },
169
  "description": description,
170
  "variants": snapshot,
171
+ "printAreas": {
172
+ "positions": all_positions,
173
+ "byVariantId": print_areas_by_variant,
174
+ "units": "px",
175
+ },
176
  "raw": blob,
177
  }
178