Specify imported file should be in CSV format (#1305)

This also moves format detection before the request is passed over to the
afterjob, so it's possible to warn user with a meaningful message.
This commit is contained in:
Frédéric Péters 2012-03-09 14:16:59 +01:00
parent b1b941e565
commit 81aa6c1490
1 changed files with 45 additions and 0 deletions

View File

@ -558,9 +558,14 @@ class DiffusionDirectory(Directory):
self.html_top(_('Import a List of Participants'))
'<h2>%s</h2>' % _('Importing a List of Participants')
'<p>'
_('The file should be in the CSV file format. Using your spreadsheet '\
'program (Calc, Excel...), click "Save as" and select the CSV format.')
'</p>'
'<p>'
_('The file should have email addresses in the first column, and, '\
'optionnaly, names in the second column.')
'</p>'
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'))
'<h2>%s</h2>' % _('Importing a List of Participants to Disable')
'<p>'
_('The file should be in the CSV file format. Using your spreadsheet '\
'program (Calc, Excel...), click "Save as" and select the CSV format.')
'</p>'
'<p>'
_('The file should consist of email addresses, one per line.')
'</p>'
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