def file_saver(file): ''' Saves uploaded file as a json file, in a SQL database, and creates a pandas dataframe to run validation test ''' # check filename uniqueness filename = file_exists(file.filename) # saves original uploaded file to files directory file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #creates pandas dataframe from the csv file df = pd.read_csv('files/' + filename, sep='|') # run validation rules validate = Validators(df=df) data = validate.clean_orders() #create a dictionary from pandas dataframe d = [ dict([(colname, row[i]) for i, colname in enumerate(data.columns)]) for row in data.values ] # seeds database with order information Orders.query.delete() for order_row in d: order_id = order_row['id'] name = order_row['name'] state = order_row['state'] email = order_row['email'] zipcode = order_row['zipcode'] birthday = order_row['birthday'] valid = str(order_row['valid']) errors = str(order_row['errors']) save_order = Orders(order_id=order_id, name=name, state=state, email=email, zipcode=zipcode, birthday=birthday, valid=valid, errors=errors) db.session.add(save_order) db.session.commit() # create json file with open('files/' + filename.rsplit('.', 1)[0] + '.json', 'w') as outfile: json.dump(d, outfile) return outfile