From be505fa504144853c2443b5fd7eaa84d404360b6 Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Sat, 11 Feb 2023 10:08:53 -0800 Subject: [PATCH 1/4] refactor: normalize schema for staffs table --- src/invenflask/schema.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/invenflask/schema.sql b/src/invenflask/schema.sql index deacf0c..1ddad4d 100644 --- a/src/invenflask/schema.sql +++ b/src/invenflask/schema.sql @@ -15,11 +15,11 @@ CREATE UNIQUE INDEX asset_id on assets (id); CREATE TABLE staffs( id text NOT NULL, - FirstName text NOT NULL, - LastName text NOT NULL, - Division text NOT NULL, - Department text NOT NULL, - Title text NOT NULL + first_name text NOT NULL, + last_name text NOT NULL, + division text NOT NULL, + department text NOT NULL, + title text NOT NULL ); From 3f2e623d90b3c37098b2b2b687c4abe1cd2bc3b4 Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Sat, 11 Feb 2023 10:10:18 -0800 Subject: [PATCH 2/4] feat: bulk imports support staff and assets --- src/invenflask/app.py | 82 ++++++++++++++----- src/invenflask/templates/base.html | 4 +- .../templates/bulk_create_verify.html | 33 -------- .../{bulk_assets.html => bulk_import.html} | 5 ++ .../templates/bulk_import_verify.html | 80 ++++++++++++++++++ src/invenflask/templates/staff.html | 22 ++--- 6 files changed, 159 insertions(+), 67 deletions(-) delete mode 100644 src/invenflask/templates/bulk_create_verify.html rename src/invenflask/templates/{bulk_assets.html => bulk_import.html} (56%) create mode 100644 src/invenflask/templates/bulk_import_verify.html diff --git a/src/invenflask/app.py b/src/invenflask/app.py index 50b6393..54248d4 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -270,8 +270,8 @@ def history(): return render_template('history.html', assets=assets) -@app.route('/bulk_asset', methods=('GET', 'POST')) -def bulk_asset(): +@app.route('/bulk_import', methods=('GET', 'POST')) +def bulk_import(): if request.method == 'POST': uploaded_file = request.files.get('file') print(uploaded_file) @@ -280,9 +280,11 @@ def bulk_asset(): uploaded_file.save(os.path.join( app.config['upload_folder'], data_filename)) session['uploaded_data_file_path'] = file_path - return redirect(url_for('showData')) + form_type = request.form['select_type'] + + return redirect(url_for('showData', form_type=form_type)) if request.method == "GET": - return render_template('bulk_assets.html') + return render_template('bulk_import.html') @app.route('/show_data', methods=["GET", "POST"]) @@ -297,30 +299,47 @@ def showData(): uploaded_df_html = uploaded_df.to_html() if request.method == "GET": headers = pd.read_csv(data_file_path, nrows=1).columns.tolist() - + form_type = request.args.get('form_type') return render_template( - 'bulk_create_verify.html', data_var=uploaded_df_html, - headers_list=headers) + 'bulk_import_verify.html', data_var=uploaded_df_html, + headers_list=headers, form_type=form_type) if request.method == "POST": - asset_id_field = request.form['asset_id'] - asset_type_field = request.form['asset_type'] - asset_status_field = request.form['asset_status'] - - parseCSV( - data_file_path, asset_id_field, - asset_type_field, asset_status_field) - return redirect(url_for('status')) - - -def parseCSV(filePath, asset_id, asset_type, asset_status): - # CVS Column Names - col_names = ['Model', 'FA_Number'] + form_type = request.args.get('form_type') + if form_type == 'assets': + asset_id_field = request.form['asset_id'] + asset_type_field = request.form['asset_type'] + asset_status_field = request.form['asset_status'] + + parseCSV_assets( + data_file_path, asset_id_field, + asset_type_field, asset_status_field) + return redirect(url_for('status')) + elif form_type == 'staff': + first_name = request.form['first_name'] + last_name = request.form['last_name'] + staff_id = request.form['staff_id'] + division = request.form['division'] + department = request.form['department'] + title = request.form['title'] + parseCSV_staff( + data_file_path, first_name, last_name, staff_id, + division, department, title) + + return redirect(url_for('staff')) + # First Name Last Name Staff ID Division Department Title + + +def parseCSV_assets(filePath, asset_id, asset_type, asset_status): # Use Pandas to parse the CSV file - csvData = pd.read_csv(filePath, names=col_names, header=1) + csvData = pd.read_csv(filePath, header=0) # Loop through the Rows print("PARSING DATA") + print(asset_status) for i, row in csvData.iterrows(): + if asset_status != 'Available': + asset_status == row[asset_status] + try: conn = get_db() conn.execute( @@ -333,3 +352,24 @@ def parseCSV(filePath, asset_id, asset_type, asset_status): flash("Asset upload failed import") return redirect(url_for('create_asset')) return redirect(url_for('status')) + + +def parseCSV_staff( + filePath, first_name, last_name, staff_id, division, department, title): + # Use Pandas to parse the CSV file + csvData = pd.read_csv(filePath, header=0) + # Loop through the Rows + + for i, row in csvData.iterrows(): + try: + conn = get_db() + conn.execute( + 'INSERT INTO staffs (id, first_name, last_name, division, department, title)' + 'VALUES(?,?,?,?,?,?)', + (row[staff_id], row[first_name], row[last_name], + row[division], row[department], row[title])) + conn.commit() + except sqlite3.IntegrityError: + flash("Asset upload failed import") + return redirect(url_for('create_asset')) + return redirect(url_for('status')) diff --git a/src/invenflask/templates/base.html b/src/invenflask/templates/base.html index 3f30899..50689ea 100644 --- a/src/invenflask/templates/base.html +++ b/src/invenflask/templates/base.html @@ -2,7 +2,7 @@ - {% block title %} {% endblock %}- AssetCheckout + {% block title %} {% endblock %}AssetCheckout