def update_graphic_json_config_with_ui_changes(): config_information_dict = request.get_json() page_id = config_information_dict[PAGE_ID] graphic_dict = graphic_component_dict_to_graphic_dict( config_information_dict[CONFIG_DICT]) graphic_filename = config_information_dict[GRAPHIC_PATH] # sanitizing the string so it is valid filename if config_information_dict[GRAPHIC_STATUS] in [NEW, COPY]: # Given a graphic title from the user input, make a valid json filename graphic_filename_no_ext = sanitize_string(graphic_dict[GRAPHIC_TITLE]) if os.path.exists( os.path.join( current_app.config[CONFIG_FILE_FOLDER], f"{graphic_filename_no_ext}.json", )): i = 0 while os.path.exists( os.path.join( current_app.config[CONFIG_FILE_FOLDER], f"{graphic_filename_no_ext}_{i}.json", )): i += 1 graphic_filename = f"{graphic_filename_no_ext}_{i}.json" else: graphic_filename = f"{graphic_filename_no_ext}.json" # make sure we are not overwriting something main_config_dict = load_main_config_dict_if_exists(current_app) page_dict = main_config_dict[AVAILABLE_PAGES][page_id] graphic_list = page_dict.get(GRAPHIC_CONFIG_FILES, []) graphic_list.append(graphic_filename) page_dict[GRAPHIC_CONFIG_FILES] = graphic_list save_main_config_dict(main_config_dict) graphic_filepath = os.path.join(current_app.config[CONFIG_FILE_FOLDER], graphic_filename) # remove and write new file to trigger file watcher and refresh flask app if os.path.exists(graphic_filepath): os.remove(graphic_filepath) with open(graphic_filepath, "w") as fout: json.dump(graphic_dict, fout, indent=4) return ( json.dumps({ "success": True, GRAPHIC_PATH: graphic_filename }), 200, { "ContentType": "application/json" }, )
def csv_backend_file_upload(upload_form, csvfile): table_name = sanitize_string(upload_form.get(DATA_SOURCE)) username = upload_form.get(USERNAME) notes = upload_form.get(NOTES) df = LocalCSVHandler.load_df_from_csv(csvfile) data_inventory = LocalCSVDataInventory( data_sources={MAIN_DATA_SOURCE: { DATA_SOURCE_TYPE: table_name }}) data_inventory.delete_data_source() data_inventory.write_data_upload_to_backend(uploaded_data_df=df, filename=csvfile.filename, username=username, notes=notes)
def sql_backend_file_to_table_upload(upload_form, csvfiles): """ :param upload_form: flask request form :param csvfiles: flask request files list :return: None. Raises a validation error if there are problems with the formatting """ table_name = sanitize_string(upload_form.get(DATA_SOURCE)) username = upload_form.get(USERNAME) notes = upload_form.get(NOTES) csv_sql_creator = CreateTablesFromCSVs( current_app.config[SQLALCHEMY_DATABASE_URI]) data = validate_wizard_upload_submission(table_name=table_name, csvfiles=csvfiles, csv_sql_creator=csv_sql_creator) ( upload_id, upload_time, table_name, ) = csv_sql_creator.create_and_fill_new_sql_table_from_df( table_name, data, REPLACE) # remove any existing metadata for this table name and write a new row SqlDataInventory.remove_metadata_rows_for_table_name(table_name) SqlDataInventory.write_upload_metadata_row( upload_id=upload_id, upload_time=upload_time, table_name=table_name, active=True, username=username, notes=notes, ) # Generate a new models.py # update the metadata to include all tables in the db csv_sql_creator.meta.reflect() # write the database schema to models.py generator = CodeGenerator(csv_sql_creator.meta, noinflect=True) # Write the generated model code to the specified file or standard output models_filepath = os.path.join(APP_DEPLOY_DATA, "models.py") # remove and write new file to trigger file watcher and refresh flask app if os.path.exists(models_filepath): os.remove(models_filepath) with io_open(os.path.join(models_filepath), "w", encoding="utf-8") as outfile: generator.render(outfile)
def test_sanitize_string(): test_string = r"this! i:/\s*$. test 42" sanitized = r"this_is_test_42" assert sanitize_string(test_string) == sanitized