1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-12-25 17:42:24 +00:00

Guess the CSV delimiter when uploading a notation sheet

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-04-06 23:08:35 +02:00
parent ec0419a6d7
commit 0181a1392d
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -262,7 +262,15 @@ class UploadNotesForm(forms.Form):
except UnicodeDecodeError:
# This is not UTF-8, grrrr
content = data.decode('latin1')
csvfile = csv.reader(StringIO(content))
for delimiter in [',', ';', '\t', '|']:
if content.split('\n')[0].count(delimiter) > 1:
break
else:
self.add_error('file',
_("Unable to detect the CSV delimiter. Please use a comma-separated file."))
return cleaned_data
csvfile = csv.reader(StringIO(content), delimiter=delimiter)
self.process(csvfile, cleaned_data)
except UnicodeDecodeError:
self.add_error('file', _("This file contains non-UTF-8 and non-ISO-8859-1 content. "