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:
|
try:
|
||||||
ext = os.path.splitext(file.filename)[1].lower()
|
ext = os.path.splitext(file.filename)[1].lower()
|
||||||
if ext == '.csv':
|
if ext == '.csv':
|
||||||
stream = io.TextIOWrapper(file.stream, encoding='utf-8-sig')
|
raw = file.stream.read()
|
||||||
reader = csv.DictReader(stream)
|
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)
|
rows = list(reader)
|
||||||
elif ext in ('.xls', '.xlsx', '.xlsm'):
|
elif ext in ('.xls', '.xlsx', '.xlsm'):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user