Check input files for UTF-8 encoding and display an appropriate error if not.
This commit is contained in:
parent
3bea7f1234
commit
cdf3628e8b
@ -497,6 +497,10 @@ def application_import(request):
|
||||
if form.is_valid():
|
||||
import_permitted = form.cleaned_data['import_permitted']
|
||||
try:
|
||||
# check for valid encoding (will raise UnicodeDecodeError if not)
|
||||
request.FILES['csvfile'].read().decode('utf-8')
|
||||
request.FILES['csvfile'].seek(0)
|
||||
|
||||
users_generated = 0
|
||||
applications_generated = 0
|
||||
applications_modified = 0
|
||||
@ -581,6 +585,8 @@ def application_import(request):
|
||||
|
||||
except csv.Error:
|
||||
message.error(request, _('Import aborted because of severe errors in the input file.'))
|
||||
except UnicodeDecodeError:
|
||||
messages.error(request, _('Import file has wrong character encoding, only UTF-8 is supported!'))
|
||||
else:
|
||||
messages.error(request, _('Please check the form for errors.'))
|
||||
else:
|
||||
|
@ -339,6 +339,10 @@ def user_import(request):
|
||||
form = UserImportForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
try:
|
||||
# check for valid encoding (will raise UnicodeDecodeError if not)
|
||||
request.FILES['csvfile'].read().decode('utf-8')
|
||||
request.FILES['csvfile'].seek(0)
|
||||
|
||||
with transaction.commit_on_success():
|
||||
|
||||
old_users = {}
|
||||
@ -385,6 +389,7 @@ def user_import(request):
|
||||
dialect = csv.Sniffer().sniff(request.FILES['csvfile'].readline())
|
||||
dialect = utils.csv_ext.patchup(dialect)
|
||||
request.FILES['csvfile'].seek(0)
|
||||
|
||||
for (lno, line) in enumerate(csv.reader(request.FILES['csvfile'], dialect=dialect)):
|
||||
i += 1
|
||||
if i > 0:
|
||||
@ -461,6 +466,8 @@ def user_import(request):
|
||||
return redirect(reverse('user_overview'))
|
||||
except csv.Error:
|
||||
message.error(request, _('Import aborted because of severe errors in the input file.'))
|
||||
except UnicodeDecodeError:
|
||||
messages.error(request, _('Import file has wrong character encoding, only UTF-8 is supported!'))
|
||||
else:
|
||||
messages.error(request, _('Please check the form for errors.'))
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user