From 6351bdb34381df075fc307f0871ca8ea16ab6533 Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 21 Feb 2023 19:52:50 -0800 Subject: [PATCH 1/5] feat: add ability to add staff and edit staff --- src/invenflask/app.py | 105 ++++++++++++++++++--- src/invenflask/templates/staff.html | 1 + src/invenflask/templates/staff_create.html | 49 ++++++++++ src/invenflask/templates/staff_edit.html | 44 +++++++++ 4 files changed, 188 insertions(+), 11 deletions(-) create mode 100644 src/invenflask/templates/staff_create.html create mode 100644 src/invenflask/templates/staff_edit.html diff --git a/src/invenflask/app.py b/src/invenflask/app.py index 2185db6..c4a429e 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -5,6 +5,7 @@ import os import pandas as pd from werkzeug.utils import secure_filename +import re config_path = Path.cwd().joinpath('config.py') @@ -18,7 +19,7 @@ os.makedirs(app.config['upload_folder'], exist_ok=True) print(config_path) -print(app.config) +# print(app.config) def get_db_connection(): @@ -122,14 +123,6 @@ def create_asset(): return render_template('create_asset.html') -@app.route('/status') -def status(): - conn = get_db() - assets = conn.execute('SELECT * FROM assets').fetchall() - conn.commit() - return render_template('status.html', assets=assets) - - @app.route('//edit/', methods=('GET', 'POST')) def edit_asset(id): @@ -150,7 +143,15 @@ def edit_asset(id): return render_template('edit_asset.html', asset=asset, asset_types=asset_types) -@app.route('/delete//', methods=('POST',)) +@app.route('/status') +def status(): + conn = get_db() + assets = conn.execute('SELECT * FROM assets').fetchall() + conn.commit() + return render_template('status.html', assets=assets) + + +@app.route('/delete/', methods=('POST',)) def delete(id): asset = get_asset(id, "edit") conn = get_db() @@ -160,7 +161,89 @@ def delete(id): return redirect(url_for('index')) -@app.route('/staff/', methods=('GET', 'POST')) +@app.route('/staff/create', methods=('GET', 'POST')) +def create_staff(): + staff_div = "--" + staff_dept = "--" + if request.method == 'POST': + conn = get_db() + staff_id = request.form['staffid'] + # auto_gen = request.form['staffgen'] + first_name = request.form['firstname'] + last_name = request.form['lastname'] + staff_title = request.form['title'] + if request.form['division']: + staff_div = request.form['division'] + if request.form['department']: + staff_dept = request.form['department'] + + # if not staff_id and auto_gen != 'on': + if not staff_id: + flash('Staff ID or Auto Generate is required') + elif not first_name or not last_name: + flash('Missing Name information') + elif not staff_title: + flash('Missing Staff Title') + else: + # if auto_gen == 'on': + # last_staff = conn.execute( + # 'SELECT id FROM staffs order by length(id) desc, id desc limit 1' + # ).fetchone()['id'] + # conn.commit() + # print(re.split("(\d+)", last_staff)) + # next_staff_id = int(re.split("(\d+)", last_staff)[1]) + 1 + # print(next_staff_id) + # staff_id = "SE" + str(next_staff_id) + # print(staff_id) + try: + conn = get_db() + conn.execute( + 'INSERT INTO staffs (id, first_name, last_name, division, department, title)' + 'VALUES(?,?,?,?,?,?)', + (staff_id, first_name, last_name, + staff_div, staff_dept, staff_title)) + conn.commit() + return redirect(url_for('staff')) + except sqlite3.IntegrityError: + flash("Staff already exists") + return redirect(url_for('create_staff')) + + return render_template('staff_create.html') + + +@app.route('/staff/delete//', methods=('POST',)) +def staff_delete(id): + staff = get_staff(id) + conn = get_db() + conn.execute('DELETE FROM staffs WHERE id = ?', (staff,)) + conn.commit() + flash('Staff "{}" was successfully deleted!'.format(id)) + return redirect(url_for('staff')) + + +@app.route('/staff/edit//', methods=('GET', 'POST')) +def staff_edit(id): + if request.method == 'POST': + staff_id = id + first_name = request.form['firstname'] + last_name = request.form['lastname'] + staff_title = request.form['title'] + staff_div = request.form['division'] + staff_dept = request.form['department'] + + conn = get_db() + conn.execute( + 'UPDATE staffs SET first_name = ?, last_name = ?, division = ?, department = ?, title = ? where id = ?', + (first_name, last_name, staff_div, staff_dept, staff_title, staff_id)) + conn.commit() + return redirect(url_for('staff')) + if request.method == 'GET': + conn = get_db() + staff = get_staff(id) + return render_template('staff_edit.html', staff=staff) + + +@app.route('/staff', methods=('GET', 'POST')) def staff(): conn = get_db() if request.method == 'POST': diff --git a/src/invenflask/templates/staff.html b/src/invenflask/templates/staff.html index c163274..c9f9914 100644 --- a/src/invenflask/templates/staff.html +++ b/src/invenflask/templates/staff.html @@ -21,6 +21,7 @@ {{ staff.division }} {{ staff.department }} {{ staff.title }} + {% endfor %} diff --git a/src/invenflask/templates/staff_create.html b/src/invenflask/templates/staff_create.html new file mode 100644 index 0000000..fe59d3c --- /dev/null +++ b/src/invenflask/templates/staff_create.html @@ -0,0 +1,49 @@ +{% extends 'base.html' %} + +{% block content %} +

{% block title %} Create Staff {% endblock %}

+
+ +
+ + +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+{% endblock %} \ No newline at end of file diff --git a/src/invenflask/templates/staff_edit.html b/src/invenflask/templates/staff_edit.html new file mode 100644 index 0000000..90bc327 --- /dev/null +++ b/src/invenflask/templates/staff_edit.html @@ -0,0 +1,44 @@ +{% extends 'base.html' %} + +{% block content %} +

{% block title %} Edit {{ staff.id }} {% endblock %}

+
+ +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + + +
+
+
+ +
+{% endblock %} \ No newline at end of file From e5d620ae7176ccd034d1001e31e527c5a4195faa Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 21 Feb 2023 19:54:19 -0800 Subject: [PATCH 2/5] refactor: staff create route matches standard --- src/invenflask/app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/invenflask/app.py b/src/invenflask/app.py index c4a429e..fd04a6c 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -162,7 +162,7 @@ def delete(id): @app.route('/staff/create', methods=('GET', 'POST')) -def create_staff(): +def staff_create(): staff_div = "--" staff_dept = "--" if request.method == 'POST': @@ -206,7 +206,7 @@ def create_staff(): return redirect(url_for('staff')) except sqlite3.IntegrityError: flash("Staff already exists") - return redirect(url_for('create_staff')) + return redirect(url_for('staff_create')) return render_template('staff_create.html') From 6361da99992f22fe7ab3dc4e800e5722c0c65c31 Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 21 Feb 2023 20:06:57 -0800 Subject: [PATCH 3/5] chore: add create staff to nav bar --- src/invenflask/templates/base.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/invenflask/templates/base.html b/src/invenflask/templates/base.html index f01fdd4..b184223 100644 --- a/src/invenflask/templates/base.html +++ b/src/invenflask/templates/base.html @@ -35,9 +35,10 @@ Check In Check Out Asset Status - History + Create Staff Staff Bulk Import + History From a9be8fb317c8f5732a19f5ae09a7f63db9a663ed Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 21 Feb 2023 20:07:24 -0800 Subject: [PATCH 4/5] fix: check that staff id doesn't already exist --- src/invenflask/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/invenflask/app.py b/src/invenflask/app.py index fd04a6c..eab5662 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -184,6 +184,8 @@ def staff_create(): flash('Missing Name information') elif not staff_title: flash('Missing Staff Title') + elif staff_id == get_staff(staff_id)['id']: + flash('Staff ID already exists. Try again.') else: # if auto_gen == 'on': # last_staff = conn.execute( From 00fd6dbf466d6d8b4fafe3f0713f06de17102dc3 Mon Sep 17 00:00:00 2001 From: Daniel Rahamim Date: Tue, 21 Feb 2023 20:23:28 -0800 Subject: [PATCH 5/5] style: remove unused import and linting --- src/invenflask/app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/invenflask/app.py b/src/invenflask/app.py index eab5662..ac27936 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -5,7 +5,6 @@ import os import pandas as pd from werkzeug.utils import secure_filename -import re config_path = Path.cwd().joinpath('config.py') @@ -232,7 +231,7 @@ def staff_edit(id): staff_title = request.form['title'] staff_div = request.form['division'] staff_dept = request.form['department'] - + conn = get_db() conn.execute( 'UPDATE staffs SET first_name = ?, last_name = ?, division = ?, department = ?, title = ? where id = ?',