fix(inventory): support cp1252/latin-1 CSV encodings in bulk import
Some checks failed
CI / lint-and-test (3.11) (push) Has been cancelled
CI / lint-and-test (3.13) (push) Has been cancelled

This commit is contained in:
2026-07-02 22:49:39 +00:00
parent 982315bdaf
commit 31b533f9f9

View File

@@ -450,8 +450,17 @@ def bulk_import_items():
try:
ext = os.path.splitext(file.filename)[1].lower()
if ext == '.csv':
stream = io.TextIOWrapper(file.stream, encoding='utf-8-sig')
reader = csv.DictReader(stream)
raw = file.stream.read()
decoded = None
for enc in ('utf-8-sig', 'cp1252', 'latin-1'):
try:
decoded = raw.decode(enc)
break
except UnicodeDecodeError:
continue
if decoded is None:
return jsonify({'error': 'Unable to detect CSV encoding. Please save the file as UTF-8.'}), 400
reader = csv.DictReader(io.StringIO(decoded))
rows = list(reader)
elif ext in ('.xls', '.xlsx', '.xlsm'):
try: