fix(inventory): support cp1252/latin-1 CSV encodings in bulk import
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user