From 0181a1392d33380cd971722b9d4a958255ed1d12 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 6 Apr 2024 23:08:35 +0200 Subject: [PATCH] Guess the CSV delimiter when uploading a notation sheet Signed-off-by: Emmy D'Anello --- participation/forms.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/participation/forms.py b/participation/forms.py index 0ce2096..c518001 100644 --- a/participation/forms.py +++ b/participation/forms.py @@ -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. "