From a1bb4fd78d315751dc98613c6fe40c0bbb9636cd Mon Sep 17 00:00:00 2001 From: Billy Date: Thu, 2 May 2024 17:38:39 -0400 Subject: [PATCH] Started implementing actual function for add.html Working on sending data to the backend and actually doing something with it. --- app.py | 62 ++++++++++- generate_data.py => generate_asset_data.py | 0 generate_card_data.py | 18 ++++ static/add.js | 119 ++++++++++++++++----- templates/adding.html | 18 ++-- templates/main.html | 49 ++++++++- utils/sql_utils.py | 8 +- 7 files changed, 233 insertions(+), 41 deletions(-) rename generate_data.py => generate_asset_data.py (100%) create mode 100644 generate_card_data.py diff --git a/app.py b/app.py index 6f3735d..1ad466d 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ app = Flask(__name__) connection = sql_utils.connect_database('ubuntu-mariadb.home','admin','password','inventory') +### main.html paths @app.route('/user_search') def user_search(): data = request.args.get('data') @@ -47,8 +48,67 @@ left join users u on u.assigned_asset=a.asset_id data = sql_utils.read_query(connection,query) return data +@app.route('/card_list') +def card_list(): + query = ''' +select c.card_number,c.display_number,u.name assigned_user +from access_cards c +left join users u on u.assigned_card=c.card_number +''' + data = sql_utils.read_query(connection,query) + return data +@app.route('/user_list') +def user_list(): + query = ''' +select * from users; +''' + data = sql_utils.read_query(connection,query) + return data + +### add.html paths + +@app.route("/add_asset", methods=['POST']) +def add_asset(): + data = request.json + query = f''' + insert into assets (asset_id,serial,model,category) values + (%(tag)s, %(serial)s, %(model)s, %(category)s) + ''' + result = sql_utils.send_query(connection,query,data) + if result: + return "success" + else: + return "failure" + +@app.route("/add_user",methods=['POST']) +def add_user(): + data = request.json + query = f''' + insert into users (id_num,name,assigned_asset,assigned_card) values + (%(id)s, %(name)s, %(asset)s, %(card)s) + ''' + result = sql_utils.send_query(connection,query,data) + if result: + return "success" + else: + return "failure" + +@app.route("/add_card") +def add_card(): + return None +@app.route("/modify_asset") +def modify_asset(): + return None +@app.route("/modify_user") +def modify_user(): + return None +@app.route("/modify_card") +def modify_card(): + return None + +### Pages @app.route('/add') -def add_data(): +def add_page(): return render_template('adding.html') @app.route("/") diff --git a/generate_data.py b/generate_asset_data.py similarity index 100% rename from generate_data.py rename to generate_asset_data.py diff --git a/generate_card_data.py b/generate_card_data.py new file mode 100644 index 0000000..6997f7c --- /dev/null +++ b/generate_card_data.py @@ -0,0 +1,18 @@ +import utils.sql_utils as sql_utils +import string, random + +connection = sql_utils.connect_database('ubuntu-mariadb.home','admin','password','inventory') + +cards = [] +for i in range(0,100): + num = random.randint(0,100) + if not num in cards: + cards.append(num) + +for index,value in enumerate(cards): + query = f''' +insert into access_cards (card_number,display_number) values ({index+1},{value}) +''' + sql_utils.send_query(connection,query) + +print("done") \ No newline at end of file diff --git a/static/add.js b/static/add.js index 16dad60..a5a3245 100644 --- a/static/add.js +++ b/static/add.js @@ -1,35 +1,106 @@ -function add_asset() { +async function add_asset() { let serial = document.getElementById("add-asset-serial"); let tag = document.getElementById("add-asset-tag"); let category = document.getElementById("add-asset-category"); let model = document.getElementById("add-asset-model"); - if (tag.value == "") { - tag = null; - } else { - tag = tag.value; - document.getElementById("add-asset-tag").value = ""; - } if (serial.value == "") { - alert('Serial field cannot be empty!') + alert("Add asset error:\nThe seiral field cannot be empty!"); return; - } else { - serial = serial.value; - document.getElementById("add-asset-serial").value = ""; - } - if (category.value == "N/A") { - category = null; - } else { - category = category.value; - document.getElementById("add-asset-category").value = "N/A"; - } - if (model.value == "N/A") { - model = null; - } else { - model = model.value; - document.getElementById("add-asset-model").value = "N/A"; } - console.log(serial,tag,category,model); + let serial_data = (serial.value == "") ? null : serial.value; + let tag_data = (tag.value == "") ? null : tag.value; + let category_data = (category.value == "N/A") ? null : category.value; + let model_data = (model.value == "N/A") ? null : model.value; + let data = { + "tag": tag_data, + "serial": serial_data, + "category": category_data, + "model": model_data + } + + await fetch("/add_asset", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }) + .then(response => response.text()) + .then(data => { + console.log(data); + }) + + serial.value = ""; + tag.value = ""; + category.value = "N/A"; + model.value = "N/A"; +} + +async function add_user() { + let id = document.getElementById("add-user-id"); + let username = document.getElementById("add-user-name"); + let asset = document.getElementById("add-assigned-asset"); + let card = document.getElementById("add-assigned-card"); + + if (username.value == "") { + alert("Add user error:\nUser name field cannot be null!"); + return; + } + + let id_data = (id.value == "") ? null : id.value; + let username_data = (username.value == "") ? null : username.value; + let asset_data = (asset.value == "") ? null : asset.value; + let card_data = (card.value == "") ? null : card.value; + + let data = { + "id": id_data, + "name": username_data, + "asset": asset_data, + "card": card_data + } + + + await fetch("/add_user",{ + method: "POST", + headers: { + "Content-Type":"application/json", + }, + body: JSON.stringify(data), + }) + .then(response => response.text()) + .then(data => { + console.log(data); + }) + + id.value = ""; + username.value = ""; + asset.value = ""; + card.value = ""; +} + +function add_card() { + let display = document.getElementById("add-display-number"); + let card = document.getElementById("add-card-number"); + + if (card.value == "") { + alert("Add card error:\nCard number field cannot be null"); + return; + } + console.log( + (display.value == "") ? null : display.value, + (card.value == "") ? null : card.value + ) + display.value = ""; + card.value = ""; +} + +function post_data_format(argsObject) { + args = [] + for (let property in argsObject) { + args.push(`${property}=${argsObject[property]}`) + } + return "?" + args.join('&'); } \ No newline at end of file diff --git a/templates/adding.html b/templates/adding.html index 32f9a0e..bd88b6d 100644 --- a/templates/adding.html +++ b/templates/adding.html @@ -16,11 +16,15 @@ display: inline-block; vertical-align: top; } + a { + display:block; + } + Search Data

Assets

Stuff pertaining to laptops and ipads. @@ -66,7 +70,7 @@

- +

@@ -99,11 +103,11 @@

- +

- +

@@ -121,7 +125,7 @@

- +

@@ -131,7 +135,7 @@

Modify asset

- +

@@ -164,7 +168,7 @@

Modify user

- +

@@ -190,7 +194,7 @@

- +

diff --git a/templates/main.html b/templates/main.html index b291dc9..185b57e 100644 --- a/templates/main.html +++ b/templates/main.html @@ -11,18 +11,24 @@ margin: 2px; padding: 2px; } + .col { width: 32%; display: inline-block; vertical-align: top; } + a { + display: block; + } + Add/Modify data

Assets

+ @@ -32,7 +38,18 @@
Asset tagAssigned to
- +
+
+

Users

+ + + + + + + + +
ID NumberNameAssigned assetAssigned card

User info search

@@ -67,7 +84,7 @@ diff --git a/utils/sql_utils.py b/utils/sql_utils.py index a6cc7e0..645ae3a 100644 --- a/utils/sql_utils.py +++ b/utils/sql_utils.py @@ -16,15 +16,15 @@ def connect_database(host, username, password, database): return connection -def send_query(connection, query): +def send_query(connection, query, parameters): cursor = connection.cursor() - result = None try: - cursor.execute(query) + cursor.execute(query, parameters) connection.commit() - return result + return True except Error as err: print(f'Error: {err}') + return False def read_query(connection, query): cursor = connection.cursor()