diff --git a/extra/modules/bodiffusion.ptl b/extra/modules/bodiffusion.ptl index 777756e..51c5a7b 100644 --- a/extra/modules/bodiffusion.ptl +++ b/extra/modules/bodiffusion.ptl @@ -558,9 +558,14 @@ class DiffusionDirectory(Directory): self.html_top(_('Import a List of Participants')) '

%s

' % _('Importing a List of Participants') '

' + _('The file should be in the CSV file format. Using your spreadsheet '\ + 'program (Calc, Excel...), click "Save as" and select the CSV format.') + '

' + '

' _('The file should have email addresses in the first column, and, '\ 'optionnaly, names in the second column.') '

' + get_session().display_message() form.render() def import_submit(self, form): @@ -611,8 +616,25 @@ class DiffusionDirectory(Directory): tmpfile = tempfile.NamedTemporaryFile() fp = form.get_widget('file').parse().fp + dialect = None while True: s = fp.read(1024*1024) + if s and not dialect: + try: + dialect = csv.Sniffer().sniff(s, delimiters=',; \t') + except csv.Error: + # perhaps this is just a list of emails, and it raised + # "Could not determine delimiter", so we check the first + # line + if '\n' in s: + first_line = s[:s.index('\n')] + else: + first_line = s + if first_line.count('@') != 1: + tmpfile.close() + get_session().message = ('error', _('Failed to use the file, please check its format.')) + return redirect('import') + dialect = 'excel' tmpfile.write(s) if not s: break @@ -742,8 +764,13 @@ class DiffusionDirectory(Directory): self.html_top(_('Import a List of Participants to Disable')) '

%s

' % _('Importing a List of Participants to Disable') '

' + _('The file should be in the CSV file format. Using your spreadsheet '\ + 'program (Calc, Excel...), click "Save as" and select the CSV format.') + '

' + '

' _('The file should consist of email addresses, one per line.') '

' + get_session().display_message() form.render() def import_disabled_submit(self, form): @@ -792,8 +819,26 @@ class DiffusionDirectory(Directory): tmpfile = tempfile.NamedTemporaryFile() fp = form.get_widget('file').parse().fp + dialect = None while True: s = fp.read(1024*1024) + if s and not dialect: + try: + dialect = csv.Sniffer().sniff(s, delimiters=',; \t') + except csv.Error: + # perhaps this is just a list of emails, and it raised + # "Could not determine delimiter", so we check the first + # line + if '\n' in s: + first_line = s[:s.index('\n')] + else: + first_line = s + if first_line.count('@') != 1: + get_session().message = ('error', _('Failed to use the file, please check its format.')) + tmpfile.close() + return redirect('import-disabled') + dialect = 'excel' + tmpfile.write(s) if not s: break