diff --git a/README.md b/README.md index c945b56..0e72d5e 100644 --- a/README.md +++ b/README.md @@ -31,18 +31,23 @@ Within the unpacked directory: For development, perform an _editable install_ instead at step 2 above, with `pip install -e .` -## import staff -This will require a CSV formatted file -Fields in order +## Import Staff +This can be done on the **Bulk Import** page. -```id, FirstName, LastName, Division, Department, Title``` +Staff information support the following columns of information: -Do not include a header row +```ID, First Name, Last Name, Division, Department, Title``` +*ID must be unique* -``` -sqlite3 database.db -sqlite> .mode csv -sqlite> .import ./test_data/.csv staffs -# verify it worked -sqlite> SELECT * from staffs; -``` +Currently we do not support less than 6 columns on the import. +If you don't have all the matching just create blank columns with the headers as needed. + +## Import Assets +This can be done on teh **Bulk Import** page. + +Assets currently support the following columns: + +```ID, Model, Status``` +*ID must be unique* + +Status is special because by default it will import as **Available** unless a column is specified for unique status tracking diff --git a/pyproject.toml b/pyproject.toml index 5638356..520ca5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,9 @@ requires-python = ">=3.10" dependencies = [ "flask >=2.2.2, <3", - "toml >=0.10.2, <1" + "toml >=0.10.2, <1", + "pandas==1.5.3" + ] [project.scripts] 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/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 ); 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