def submit_excel_file(filepath, author, success='success', error='error'): # store basename filename = os.path.basename(filepath) print('Submitting %s' % filename) # create Excel form from path form = MalariaExcelForm(filepath) # test all logic checks on form if form.is_valid(author=author, bulk_import=True): try: print("form is valid") # create the report. form.create_report(author=author) print("report created") # move file to success folder shutil.move(filepath, os.path.join(success, filename)) return True except IncorrectReportData: print("can't create report") pass else: print("form is not valid") # form is not valid or report failed to create. move to failed folder shutil.move(filepath, os.path.join(error, filename)) # get all errors string and write them to an error file errors = form.errors.all(True) pprint.pprint(errors) err_f = open(os.path.join(error, filename + '.error.txt'), 'w') err_f.write(pprint.pformat(errors)) err_f.close() return False
def upload_form(request): context = {'category': 'upload'} provider = request.user.get_profile() if request.method == 'POST': if 'excel_form' in request.FILES: filepath = handle_uploaded_file(request.FILES['excel_form']) status = None instance = None form = MalariaExcelForm(filepath) if form.is_valid(): try: instance = form.create_report(author=provider) context.update({'instance': instance}) status = 'ok' except IncorrectReportData: status = 'error' else: # not valid pass if form.errors.count() > 0: status = 'error' context.update({'all_errors': form.errors.all(True)}) else: status = 'nofile' context.update({'status': status}) return render_to_response('upload_form.html', \ context, RequestContext(request))