Fix detection of YAML and JSON files with leading whitespace

This commit is contained in:
Per von Zweigbergk 2023-09-05 10:33:21 +02:00
parent df3a5cd422
commit c554443a6a

View File

@ -81,12 +81,12 @@ class BulkImportForm(BootstrapMixin, SyncedDataMixin, forms.Form):
Attempt to automatically detect the format (CSV, JSON, or YAML) of the given data, or raise Attempt to automatically detect the format (CSV, JSON, or YAML) of the given data, or raise
a ValidationError. a ValidationError.
""" """
first_line = data.strip().split('\n', 1)[0]
try: try:
if data[0] in ('{', '['): if first_line[0] in ('{', '['):
return ImportFormatChoices.JSON return ImportFormatChoices.JSON
if data.startswith('---') or data.startswith('- '): if first_line.startswith('---') or first_line.startswith('- '):
return ImportFormatChoices.YAML return ImportFormatChoices.YAML
first_line = data.split('\n', 1)[0]
if ',' in first_line: if ',' in first_line:
return ImportFormatChoices.CSV return ImportFormatChoices.CSV
if ';' in first_line: if ';' in first_line: