diff --git a/.gitignore b/.gitignore index 5fbb8aa..538d1da 100644 --- a/.gitignore +++ b/.gitignore @@ -111,4 +111,4 @@ database.db .DS_Store -config.toml +config.py diff --git a/example.config.py b/example.config.py new file mode 100644 index 0000000..f8ef878 --- /dev/null +++ b/example.config.py @@ -0,0 +1 @@ +SECRET_KEY = "" diff --git a/example.config.toml b/example.config.toml deleted file mode 100644 index b857db5..0000000 --- a/example.config.toml +++ /dev/null @@ -1,9 +0,0 @@ -[default] -SECRET_KEY = "SomethingNEAT" - - - - - -[development] -SECRET_KEY = "Pl3453Ch4ng3" \ No newline at end of file diff --git a/sql_commands b/sql_commands index 9d832be..4cec6f0 100644 --- a/sql_commands +++ b/sql_commands @@ -1,4 +1,4 @@ sqlite3 database.db sqlite> .mode csv -sqlite> .import ./test_data/convertcsv.csv +sqlite> .import ./test_data/convertcsv.csv staffs sqlite> SELECT * from staffs; diff --git a/src/invenflask/app.py b/src/invenflask/app.py index d6ccce6..a433d32 100644 --- a/src/invenflask/app.py +++ b/src/invenflask/app.py @@ -1,15 +1,16 @@ from flask import Flask -from flask import render_template, request, url_for, flash, redirect, abort +from flask import render_template, request, url_for, flash, redirect, abort, g from pathlib import Path import sqlite3 -import toml -config_path = Path.cwd().joinpath('config.toml') - +config_path = Path.cwd().joinpath('config.py') app = Flask(__name__) -app.config.from_file(str(config_path), load=toml.load) +app.config.from_pyfile(config_path) +print(config_path) + +print(app.config) def get_db_connection(): @@ -18,15 +19,32 @@ def get_db_connection(): return conn +def get_db(): + """Opens a new database connection if there is none yet for the + current application context. + """ + if not hasattr(g, 'sqlite_db'): + g.sqlite_db = get_db_connection() + return g.sqlite_db + + +@app.teardown_appcontext +def close_db(error): + """Closes the database again at the end of the request.""" + if hasattr(g, 'sqlite_db'): + g.sqlite_db.close() + + def get_asset(asset_id, action): - conn = get_db_connection() + conn = get_db() asset = conn.execute('SELECT * FROM assets WHERE id = ?', - (asset_id,)).fetchone() - conn.close() + (asset_id,)).fetchone + conn.commit() if action == "edit": if asset is None: abort(404) else: + print(asset) return asset if action == "create": if asset: @@ -35,24 +53,25 @@ def get_asset(asset_id, action): def get_staff(staff_id): - conn = get_db_connection() + conn = get_db() staff = conn.execute('SELECT * FROM staffs WHERE id = ?', (staff_id,)).fetchone() - conn.close() + conn.commit() return staff def get_checkout(asset_id): - conn = get_db_connection() + conn = get_db() asset = conn.execute('SELECT * FROM checkouts WHERE assetid = ?', (asset_id,)).fetchone() - conn.close() + conn.commit() return asset @app.route('/') def index(): - conn = get_db_connection() + + conn = get_db() assets = conn.execute('SELECT * FROM assets').fetchall() asset_total = conn.execute('SELECT COUNT(*) FROM assets').fetchone()[0] asset_types = conn.execute( @@ -61,7 +80,7 @@ def index(): asset_status = conn.execute( 'SELECT asset_status, COUNT(*) FROM assets GROUP BY asset_status' ).fetchall() - conn.close() + conn.commit() return render_template( 'index.html', assets=assets, asset_total=asset_total, asset_type=asset_types, asset_status=asset_status) @@ -80,13 +99,12 @@ def create_asset(): flash('Asset Type required') else: try: - conn = get_db_connection() + conn = get_db() conn.execute( 'INSERT INTO assets (id, asset_type, asset_status)' 'VALUES (?, ?, ?)', (id, asset_type, asset_status)) conn.commit() - conn.close() return redirect(url_for('status')) except sqlite3.IntegrityError: flash("Asset already exists") @@ -97,9 +115,9 @@ def create_asset(): @app.route('/status') def status(): - conn = get_db_connection() + conn = get_db() assets = conn.execute('SELECT * FROM assets').fetchall() - conn.close() + conn.commit() return render_template('status.html', assets=assets) @@ -110,11 +128,10 @@ def edit_asset(id): if request.method == 'POST': asset_type = request.form['asset_type'] asset_status = request.form['asset_status'] - conn = get_db_connection() + conn = get_db() conn.execute('UPDATE assets SET asset_type = ?, asset_status = ?' ' WHERE id = ?', (asset_type, asset_status, id)) conn.commit() - conn.close() return redirect(url_for('status')) return render_template('edit_asset.html', asset=asset) @@ -123,21 +140,20 @@ def edit_asset(id): @app.route('/delete//', methods=('POST',)) def delete(id): asset = get_asset(id, "edit") - conn = get_db_connection() + conn = get_db() conn.execute('DELETE FROM assets WHERE id = ?', (asset,)) conn.commit() - conn.close() flash('Asset "{}" was successfully deleted!'.format(id)) return redirect(url_for('index')) @app.route('/staff/', methods=('GET', 'POST')) def staff(): - conn = get_db_connection() + conn = get_db() if request.method == 'POST': pass staff = conn.execute('SELECT * FROM staffs').fetchall() - conn.close() + conn.commit() return render_template('staff.html', staffs=staff) @@ -147,7 +163,6 @@ def checkout(): asset_id = request.form['id'] accessory_id = request.form['accessoryid'] staff_id = request.form['staffid'] - if not asset_id: flash('Asset ID is required') elif not staff_id: @@ -158,14 +173,18 @@ def checkout(): elif get_asset(asset_id, 'edit') is False: flash("Asset does not exist. Please make it below") return redirect(url_for('create_asset')) - elif get_asset(asset_id, 'edit')['asset_status'] == 'damaged': - flash("Asset should not be checked out. Please choose another one") - return redirect(url_for('checkout')) + elif get_asset(asset_id, 'edit') is dict: + if get_asset(asset_id, 'edit')['asset_status'] == 'damaged': + flash("Asset should not be checked out. Please choose another one") + return redirect(url_for('checkout')) + else: + flash( + f"Something went wrong with {get_asset(asset_id, 'edit')}") else: staff_dept = get_staff(staff_id)['Department'] - flash(get_asset(asset_id, 'edit')['asset_status']) + # flash(get_asset(asset_id, 'edit')) try: - conn = get_db_connection() + conn = get_db() conn.execute( 'INSERT INTO checkouts (assetid, staffid, department) ' 'VALUES (?, ?, ?)', (asset_id, staff_id, staff_dept)) @@ -182,7 +201,6 @@ def checkout(): (accessory_id,)) conn.commit() - conn.close() flash('Asset Checkout Completed') return redirect(url_for('checkout')) except sqlite3.IntegrityError: @@ -202,11 +220,15 @@ def checkin(): elif get_asset(asset_id, "edit") is False: flash("Asset does not exist. Please make it below") return redirect(url_for('create_asset')) + elif get_checkout(asset_id) is None: + flash("Asset Not checked out") + return redirect(url_for('checkin')) else: asset_checkout = get_checkout(asset_id) + print(asset_checkout) staff_div = get_staff(asset_checkout['staffid'])['Division'] try: - conn = get_db_connection() + conn = get_db() conn.execute( 'INSERT INTO history (assetid, staffid, department, division, checkouttime) VALUES (?,?,?,?,?)', (asset_id, asset_checkout['staffid'], @@ -214,16 +236,16 @@ def checkin(): asset_checkout['timestamp'])) conn.execute( 'DELETE from checkouts WHERE assetid = ?', (asset_id,)) - if get_asset(asset_id, "edit")['asset_status'] == "damaged": - conn.execute( - 'UPDATE assets SET asset_status = ? WHERE id = ?', - ('damaged', asset_id)) - else: - conn.execute( - 'UPDATE assets SET asset_status = ? WHERE id = ?', - ('Available', asset_id)) + if get_asset(asset_id, 'edit') is dict: + if get_asset(asset_id, "edit")['asset_status'] == "damaged": + conn.execute( + 'UPDATE assets SET asset_status = ? WHERE id = ?', + ('damaged', asset_id)) + else: + conn.execute( + 'UPDATE assets SET asset_status = ? WHERE id = ?', + ('Available', asset_id)) conn.commit() - conn.close() flash('Asset checkin Completed') return redirect(url_for('checkout')) except sqlite3.IntegrityError as e: @@ -234,7 +256,7 @@ def checkin(): @app.route('/history') def history(): - conn = get_db_connection() + conn = get_db() assets = conn.execute('SELECT * FROM history').fetchall() - conn.close() + conn.commit() return render_template('history.html', assets=assets)