-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_csv.py
More file actions
56 lines (53 loc) · 1.68 KB
/
import_csv.py
File metadata and controls
56 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from fastapi import APIRouter, UploadFile, File, HTTPException
import csv
import psycopg2
import os
import io
from dotenv import load_dotenv
router = APIRouter()
@router.post("/import-csv")
def import_csv(file: UploadFile = File(...)):
"""Import products from a CSV file into the database."""
load_dotenv()
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT', '5432'),
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
cur = conn.cursor()
try:
reader = csv.DictReader(io.TextIOWrapper(file.file, encoding='utf-8'))
batch = []
batch_size = 100
inserted = 0
for row in reader:
batch.append((
row.get('name'),
row.get('description'),
float(row.get('price', 0)),
int(row.get('in_stock', 0)),
))
if len(batch) >= batch_size:
cur.executemany(
"INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s)",
batch
)
conn.commit()
inserted += len(batch)
batch.clear()
if batch:
cur.executemany(
"INSERT INTO product (name, description, price, in_stock) VALUES (%s, %s, %s, %s)",
batch
)
conn.commit()
inserted += len(batch)
return {"inserted": inserted}
except Exception as e:
conn.rollback()
raise HTTPException(status_code=500, detail=str(e))
finally:
cur.close()
conn.close()