def save_file(file, name, is_logo, id=""): try: if is_logo: folder = current_app.config['LOGO_UPLOAD_FOLDER'] name = name.replace(" ", "_") + os.path.splitext(file.filename)[1] else: user_folder = str(current_user.id) + "_" if current_user.short_name: user_folder += current_user.short_name else: user_folder += "vendor" user_folder += "/" + str(id) + "/" folder = current_app.config['UPLOAD_FOLDER'] + user_folder file_dir = os.path.realpath(os.path.dirname(folder)) pathlib.Path(file_dir).mkdir(parents=True, exist_ok=True) file.stream.seek(0) # print(name) # print(secure_filename(name)) # print(os.path.join(file_dir, secure_filename(name))) file.save(os.path.join(file_dir, secure_filename(name))) except: print(sys.exc_info()) return {"message": "1: " + str(sys.exc_info()[0])}, 500 if is_logo: return name else: str_mandatory_columns = FileFormatModel.find_all_mandatory_column_str() str_optional_columns = FileFormatModel.find_all_optional_column_str() return run_bash_script(user_folder, str_mandatory_columns, str_optional_columns, id) return None
def upload(): if current_app.config['ZINC_MODE']: form = UploadForm() formats = FileFormatModel.find_all() if request.method == 'POST' and form.validate_on_submit(): return_msg = validate(form.file.data, form) return jsonify(return_msg) # else: # # return_msg = validate(form) # return jsonify(return_msg) return render_template('upload.html', title='Upload File', form=form, formats=formats) else: if request.method == 'POST': return jsonify(excel_validation(request)) return render_template('upload.html', title='Upload File')
def master_upload(): if current_app.config['ZINC_MODE']: form = MasterUploadForm() formats = FileFormatModel.find_all() if not current_user.company and not current_user.has_role('Admin'): flash('Permission denied', category='warning') return render_template('master_upload.html', title='Upload File', form=form, formats=formats) else: if request.method == 'POST' and form.validate_on_submit(): return_msg = validate(form.file.data, form) return jsonify(return_msg) # else: # # return_msg = validate(form) # return jsonify(return_msg) return render_template('master_upload.html', title='Upload File', form=form, formats=formats) else: if request.method == 'POST': return jsonify(excel_validation(request)) return render_template('master_upload.html', title='Upload File')
def save_file(file, object, name, is_logo, id=""): try: if is_logo: folder = current_app.config['LOGO_UPLOAD_FOLDER'] name = name.replace(" ", "_") + os.path.splitext(file.filename)[1] else: #if name.endswith(".csv"): # print("csv format catalog") # extract_molcules_info_from_csv(name) if name.endswith(".txt"): print("Txt format catalog") name = name.replace("txt", "smi") if name.endswith(".smi"): print("Smi format catalog") user_folder = str(current_user.id) + "_" if current_user.short_name and current_user.has_role("Vendor"): user_folder += current_user.short_name else: user_folder += "vendor" if current_user.has_role("Admin"): user_folder += current_user.username user_folder += "/" + str(id) + "/" folder = current_app.config['UPLOAD_FOLDER'] + user_folder file_dir = os.path.realpath(os.path.dirname(folder)) pathlib.Path(file_dir).mkdir(parents=True, exist_ok=True) file.stream.seek(0) # print(name) # print(secure_filename(name)) # print(os.path.join(file_dir, secure_filename(name))) print("Saving file to directory") file.save(os.path.join(file_dir, secure_filename(name))) try: if name.endswith(".csv") or name.endswith(".tsv"): print("delimited format catalog") process_delimited_file(name, file_dir, object) print("Saving json file") write_json_file(object, file_dir) return {"message": "Your job has been submitted!"}, 200 print("Saving json file") write_json_file(object, file_dir) except: object.delete_from_db() remove_job_folder(object.status_id) return {"message": "4: " + str(sys.exc_info()[0])}, 500 except: object.delete_from_db() remove_job_folder(object.status_id) return {"message": "3: " + str(sys.exc_info()[0])}, 500 if is_logo: return name else: str_mandatory_columns = FileFormatModel.find_all_mandatory_column_str() print("Mandatory : " + str_mandatory_columns) str_optional_columns = FileFormatModel.find_all_optional_column_str() print("Optional : " + str_optional_columns) return run_bash_script(user_folder, str_mandatory_columns, str_optional_columns, id, object) return None
def help_page(): file_format = FileFormatModel.find_all() return render_template('help.html', file_format=file_format, title='Help')