Skip to content

Commit c713e85

Browse files
committed
Refactor orders API and improve CSV import
Update orders endpoint to query the orders table (no first_name/last_name search or ordering by first_name) and adjust error messaging. Enhance CSV import: truncate orders before import, print progress messages, ensure SKUs are unique within the import batch by suffixing duplicates, and change INSERT behavior (remove ON CONFLICT DO NOTHING). Add a sample Magento products CSV at app/static/csv/magento_products_sample.csv for testing/imports.
1 parent fab4165 commit c713e85

3 files changed

Lines changed: 27 additions & 8 deletions

File tree

app/api/orders/orders.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
base_url = os.getenv("BASE_URL", "http://localhost:8000")
99

1010

11+
12+
1113
# Refactored GET /orders endpoint to return paginated, filtered, and ordered results
1214
@router.get("/orders")
1315
def get_orders(
@@ -28,10 +30,7 @@ def get_orders(
2830
params = []
2931
if hideflagged:
3032
where_clauses.append("flag IS NOT TRUE")
31-
if search:
32-
where_clauses.append("(LOWER(first_name) LIKE %s OR LOWER(last_name) LIKE %s)")
33-
search_param = f"%{search.lower()}%"
34-
params.extend([search_param, search_param])
33+
# No first_name/last_name search, as those columns do not exist
3534
where_sql = " AND ".join(where_clauses)
3635

3736
# Count query
@@ -42,9 +41,8 @@ def get_orders(
4241

4342
# Data query
4443
data_query = f'''
45-
SELECT * FROM prospects
44+
SELECT * FROM orders
4645
WHERE {where_sql}
47-
ORDER BY first_name ASC
4846
OFFSET %s LIMIT %s;
4947
'''
5048
cur.execute(data_query, params + [offset, limit])
@@ -57,7 +55,7 @@ def get_orders(
5755
except Exception as e:
5856
data = []
5957
total = 0
60-
meta = make_meta("error", f"Failed to read prospects: {str(e)}")
58+
meta = make_meta("error", f"Failed to read orders: {str(e)}")
6159
finally:
6260
cur.close()
6361
conn.close()

app/api/orders/sql/import_magento_products_to_orders.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,24 @@ def import_csv_to_orders():
1919
total = 0
2020
with conn:
2121
with conn.cursor() as cur:
22+
print("Clearing orders table before import...")
23+
cur.execute("TRUNCATE TABLE orders;")
24+
print("orders table cleared.")
25+
seen_skus = set()
2226
with open(CSV_PATH, newline='', encoding='utf-8') as csvfile:
2327
reader = csv.DictReader(csvfile)
2428
for idx, row in enumerate(reader):
29+
original_sku = row.get("sku")
30+
new_sku = original_sku
31+
# Ensure SKU is unique in this import batch
32+
suffix = 1
33+
while new_sku in seen_skus:
34+
new_sku = f"{original_sku}_{suffix}"
35+
suffix += 1
36+
if new_sku != original_sku:
37+
print(f"Duplicate SKU found: {original_sku}, changed to {new_sku}")
38+
row["sku"] = new_sku
39+
seen_skus.add(new_sku)
2540
total += 1
2641
# Add hide and flag fields if not present
2742
row.setdefault("hide", False)
@@ -54,7 +69,7 @@ def import_csv_to_orders():
5469
except Exception:
5570
values[i] = None
5671
placeholders = ','.join(['%s'] * len(ORDERS_COLUMNS))
57-
sql = f"INSERT INTO orders ({', '.join(ORDERS_COLUMNS)}) VALUES ({placeholders}) ON CONFLICT (sku) DO NOTHING"
72+
sql = f"INSERT INTO orders ({', '.join(ORDERS_COLUMNS)}) VALUES ({placeholders})"
5873
try:
5974
cur.execute(sql, values)
6075
inserted += 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,related_position,crosssell_skus,crosssell_position,upsell_skus,upsell_position,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,bundle_shipment_type,associated_skus,downloadable_links,downloadable_samples,configurable_variations,configurable_variation_labels
2+
24-MB01,,Bag,simple,"Default Category/Gear,Default Category/Gear/Bags",base,"Joust Duffle Bag","Sporty duffle bag","Duffle bag",,1,,"Catalog, Search",34.00,,,,joust-duffle-bag,,,,/m/b/mb01-blue-0.jpg,,/m/b/mb01-blue-0.jpg,,/m/b/mb01-blue-0.jpg,,,,2026-02-05,2026-02-05,,,,,,,,,,,,,,,,100.0,0.0,1,0,0,1,1,1,10000,1,1,,1,1,1,1,0.0,1,0,0,0,,,"24-WG086,24-WG083-blue,24-UG01,24-WG085_Group","1,2,3,4","24-MB02,24-MB03,24-MB05,24-MB06,24-UB02,24-WB03,24-WB04,24-WB07","1,2,3,4,5,6,7,8",/m/b/mb01-blue-0.jpg,Image,,,,,,,,,,,,,
3+
24-MB02,,Bag,simple,"Default Category/Gear,Default Category/Gear/Bags",base,"Fusion Backpack","Fusion backpack for travel","Backpack",,1,,"Catalog, Search",39.99,,,,fusion-backpack,,,,/m/b/mb02-black-0.jpg,,/m/b/mb02-black-0.jpg,,/m/b/mb02-black-0.jpg,,,,2026-02-06,2026-02-06,,,,,,,,,,,,,,,,120.0,0.0,1,0,0,1,1,1,10000,1,1,,1,1,1,1,0.0,1,0,0,0,,,"24-WG087,24-UG01","1,2","24-MB01,24-MB03","1,2",/m/b/mb02-black-0.jpg,Image,,,,,,,,,,,,,
4+
24-MB03,,Bag,simple,"Default Category/Gear,Default Category/Gear/Bags",base,"Summit Backpack","Summit backpack for hiking","Backpack",,1,,"Catalog, Search",38.00,,,,summit-backpack,,,,/m/b/mb03-black-0.jpg,,/m/b/mb03-black-0.jpg,,/m/b/mb03-black-0.jpg,,,,2026-02-07,2026-02-07,,,,,,,,,,,,,,,,110.0,0.0,1,0,0,1,1,1,10000,1,1,,1,1,1,1,0.0,1,0,0,0,,,"24-UG06,24-WG084","1,2","24-MB02,24-MB05","1,2",/m/b/mb03-black-0.jpg,Image,,,,,,,,,,,,,
5+
24-MB04,,Bag,simple,"Default Category/Gear,Default Category/Collections,Default Category/Gear/Bags",base,"Strive Shoulder Pack","Shoulder pack for daily use","Shoulder pack",,1,,"Catalog, Search",32.00,,,,strive-shoulder-pack,,,,/m/b/mb04-black-0.jpg,,/m/b/mb04-black-0.jpg,,/m/b/mb04-black-0.jpg,,,,2026-02-08,2026-02-08,,,,,,,,,,,,,,,,90.0,0.0,1,0,0,1,1,1,10000,1,1,,1,1,1,1,0.0,1,0,0,0,,,"24-UG03,24-UG05","1,2","24-MB01,24-MB02","1,2",/m/b/mb04-black-0.jpg,Image,,,,,,,,,,,,,
6+
24-MB05,,Bag,simple,"Default Category/Gear,Default Category/Collections,Default Category/Gear/Bags,Default Category/Collections/New Luma Yoga Collection",base,"Wayfarer Messenger Bag","Messenger bag for work","Messenger bag",,1,,"Catalog, Search",45.00,,,,wayfarer-messenger-bag,,,,/m/b/mb05-black-0.jpg,,/m/b/mb05-black-0.jpg,,/m/b/mb05-black-0.jpg,,,,2026-02-09,2026-02-09,,,,,,,,,,,,,,,,80.0,0.0,1,0,0,1,1,1,10000,1,1,,1,1,1,1,0.0,1,0,0,0,,,"24-UG04,24-WG088","1,2","24-MB02,24-UB02","1,2",/m/b/mb05-black-0.jpg,Image,,,,,,,,,,,,,

0 commit comments

Comments
 (0)