upload flask server
Browse files- flask_sku_server.py +119 -0
flask_sku_server.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify, request
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
from collections import OrderedDict
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
JSON_PATH = r"C:\Users\ASUS\Desktop\Computer_vision_binder\output_cleaning_imagesCarrefourksa_fields.json"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def load_products():
|
| 12 |
+
if os.path.exists(JSON_PATH):
|
| 13 |
+
with open(JSON_PATH, 'r', encoding='utf-8') as file:
|
| 14 |
+
return json.load(file)
|
| 15 |
+
return []
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def save_products(products_list):
|
| 19 |
+
cleaned = []
|
| 20 |
+
for p in products_list:
|
| 21 |
+
ordered = OrderedDict()
|
| 22 |
+
# Desired field order
|
| 23 |
+
field_order = [
|
| 24 |
+
"product_id",
|
| 25 |
+
"image_id",
|
| 26 |
+
"sor_cat",
|
| 27 |
+
"sor_subcat",
|
| 28 |
+
"sor_subsubcat",
|
| 29 |
+
"sor_prdocut_name",
|
| 30 |
+
"sor_product_url",
|
| 31 |
+
"sor_image_url"
|
| 32 |
+
]
|
| 33 |
+
for key in field_order:
|
| 34 |
+
if key in p:
|
| 35 |
+
ordered[key] = p[key]
|
| 36 |
+
# SKU always last
|
| 37 |
+
ordered["binder_cv_sku"] = p.get("binder_cv_sku", None)
|
| 38 |
+
cleaned.append(ordered)
|
| 39 |
+
|
| 40 |
+
with open(JSON_PATH, 'w', encoding='utf-8') as file:
|
| 41 |
+
json.dump(cleaned, file, indent=2, ensure_ascii=False)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def generate_unique_sku(existing_skus):
|
| 45 |
+
while True:
|
| 46 |
+
sku = f"{random.randint(0, 999999999):09d}"
|
| 47 |
+
if sku not in existing_skus:
|
| 48 |
+
return sku
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@app.route('/all-products', methods=['GET'])
|
| 52 |
+
def get_all_products():
|
| 53 |
+
products = load_products()
|
| 54 |
+
|
| 55 |
+
sku_map = {}
|
| 56 |
+
existing_skus = set()
|
| 57 |
+
updated = False
|
| 58 |
+
|
| 59 |
+
for p in products:
|
| 60 |
+
pid = p.get("product_id")
|
| 61 |
+
sku = p.get("binder_cv_sku")
|
| 62 |
+
if sku:
|
| 63 |
+
sku_map[pid] = sku
|
| 64 |
+
existing_skus.add(sku)
|
| 65 |
+
|
| 66 |
+
for p in products:
|
| 67 |
+
pid = p.get("product_id")
|
| 68 |
+
if not p.get("binder_cv_sku"):
|
| 69 |
+
if pid in sku_map:
|
| 70 |
+
p["binder_cv_sku"] = sku_map[pid]
|
| 71 |
+
else:
|
| 72 |
+
new_sku = generate_unique_sku(existing_skus)
|
| 73 |
+
sku_map[pid] = new_sku
|
| 74 |
+
p["binder_cv_sku"] = new_sku
|
| 75 |
+
existing_skus.add(new_sku)
|
| 76 |
+
updated = True
|
| 77 |
+
|
| 78 |
+
if updated:
|
| 79 |
+
save_products(products)
|
| 80 |
+
|
| 81 |
+
return jsonify(products)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
@app.route('/add-product', methods=['POST'])
|
| 85 |
+
def add_product():
|
| 86 |
+
new_product = request.get_json()
|
| 87 |
+
if not new_product or "product_id" not in new_product or "image_id" not in new_product:
|
| 88 |
+
return jsonify({"error": "Missing product_id or image_id"}), 400
|
| 89 |
+
|
| 90 |
+
products = load_products()
|
| 91 |
+
|
| 92 |
+
# Check if image_id already exists — avoid duplicates
|
| 93 |
+
existing_image_ids = {p['image_id'] for p in products}
|
| 94 |
+
if new_product["image_id"] in existing_image_ids:
|
| 95 |
+
return jsonify({"message": "Image already exists. Skipped."}), 200
|
| 96 |
+
|
| 97 |
+
# Map product_id to existing SKUs
|
| 98 |
+
existing_skus = {p['binder_cv_sku'] for p in products if "binder_cv_sku" in p}
|
| 99 |
+
sku_map = {p['product_id']: p['binder_cv_sku'] for p in products if "binder_cv_sku" in p}
|
| 100 |
+
|
| 101 |
+
pid = new_product["product_id"]
|
| 102 |
+
|
| 103 |
+
# Assign SKU: reuse if product_id exists, else generate new SKU
|
| 104 |
+
if pid in sku_map:
|
| 105 |
+
new_product["binder_cv_sku"] = sku_map[pid]
|
| 106 |
+
else:
|
| 107 |
+
new_sku = generate_unique_sku(existing_skus)
|
| 108 |
+
new_product["binder_cv_sku"] = new_sku
|
| 109 |
+
sku_map[pid] = new_sku
|
| 110 |
+
|
| 111 |
+
# Append and save
|
| 112 |
+
products.append(new_product)
|
| 113 |
+
save_products(products)
|
| 114 |
+
|
| 115 |
+
return jsonify({"message": "Product added successfully", "binder_cv_sku": new_product["binder_cv_sku"]})
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
if __name__ == '__main__':
|
| 119 |
+
app.run(debug=True)
|