def upload_csv(request, form_class):
"""
Upload csv file to server and validate it.
:param form_class: - form class to instantiate (taken from urls.py)
:rtype: HttpResponse
"""
csv_file = request.FILES.get('0')
if csv_file:
# Apply custom validation
new_csv_file = None
try:
new_csv_file = form_class.validate_csv_file(csv_file)
except Exception:
pass
if not new_csv_file:
return HttpResponseNotFound(simplejson.dumps({'response': _('File is not valid')}))
else:
response = {'response': _('File is valid'), 'lines': new_csv_file.line_count,
'names': new_csv_file.names}
if new_csv_file.warning:
# add warning
response['warning'] = new_csv_file.warning
response['response'] = _('File is partially valid')
request.session[form_class.CSV_FILE_NAME] = new_csv_file
return HttpResponse(simplejson.dumps(response))
raise Http404
评论列表
文章目录