From 064ff5f53408d4c342f9bf099f59868459d27d06 Mon Sep 17 00:00:00 2001 From: Billy Date: Sat, 4 May 2024 11:15:14 -0400 Subject: [PATCH] Did some more of the modify fields --- app.py | 62 +++++++++++++++++++++++++++++--- static/add.js | 87 ++++++++++++++++++++++++++++++++++++++++++--- templates/main.html | 8 +++++ 3 files changed, 148 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 7ce79b1..7938c10 100644 --- a/app.py +++ b/app.py @@ -137,12 +137,66 @@ def add_card(): else: return "failure" -@app.route("/modify_asset") +@app.route("/modify_asset", methods=['POST']) def modify_asset(): - return None -@app.route("/modify_user") + lookup = {'tag':'asset_id', + 'serial':'serial', + 'model':'model', + 'category':'category'} + + segments = [] + parameters = [] + data = request.json + + for entry in data: + if entry == 'tag': + continue + if data[entry]: + segments.append(f'{lookup[entry]}=%s') + parameters.append(data[entry]) + + parameters.append(data['tag']) + query = f''' +update assets +SET {','.join(segments)} +WHERE asset_id = %s +''' + result = sql_utils.send_query(connection,query,parameters) + if result: + return "fuck you" + else: + return "fuck you too" + +@app.route("/modify_user", methods=['POST']) def modify_user(): - return None + lookup = {'id':'id_num', + 'name':'name', + 'asset':'assigned_asset', + 'card':'assigned_card'} + + segments = [] + parameters = [] + data = request.json + + for entry in data: + if entry == 'tag': + continue + if data[entry]: + segments.append(f'{lookup[entry]}=%s') + parameters.append(data[entry]) + + parameters.append(data['id']) + query = f''' +update users +SET {','.join(segments)} +WHERE id_num = %s +''' + result = sql_utils.send_query(connection,query,parameters) + if result: + return "fuck you" + else: + return "fuck you too" + @app.route("/modify_card") def modify_card(): return None diff --git a/static/add.js b/static/add.js index 470069f..ed11e5a 100644 --- a/static/add.js +++ b/static/add.js @@ -132,12 +132,89 @@ async function add_card() { await get_next_card(); } -function post_data_format(argsObject) { - args = [] - for (let property in argsObject) { - args.push(`${property}=${argsObject[property]}`) +async function modify_asset() { + let serial = document.getElementById("modify-asset-serial"); + let tag = document.getElementById("modify-asset-tag"); + let category = document.getElementById("modify-asset-category"); + let model = document.getElementById("modify-asset-model"); + + if (tag.value == "") { + alert("Modify asset error:\nThe tag field cannot be empty!"); + return; } - return "?" + args.join('&'); + + 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("/modify_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 modify_user() { + let id = document.getElementById("modify-user-id"); + let username = document.getElementById("modify-user-name"); + let asset = document.getElementById("modify-assigned-asset"); + let card = document.getElementById("modify-assigned-card"); + + if (id.value == "") { + alert("modify user error:\nUser ID 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") ? null : asset.value; + let card_data = (card.value == "null") ? null : card.value; + + let data = { + "id": id_data, + "name": username_data, + "asset": asset_data, + "card": card_data + } + + + await fetch("/modify_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 = ""; + + await populate_data(); } async function populate_available_assets() { diff --git a/templates/main.html b/templates/main.html index 185b57e..cc4a043 100644 --- a/templates/main.html +++ b/templates/main.html @@ -29,6 +29,7 @@

Assets

+ @@ -142,6 +143,13 @@ }); } + function clear_asset_list() { + let elements = document.getElementsByClassName("asset_data"); + while (elements[0]) { + elements[0].remove(); + } + } + async function get_assets() { let elements = document.getElementsByClassName("asset_data"); while (elements[0]) {
Asset tag