inital commit
This commit is contained in:
commit
5d9ac5e18b
|
|
@ -0,0 +1,2 @@
|
|||
.venv
|
||||
__pycache__
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
from flask import Flask, redirect, render_template, request
|
||||
import utils.sql_utils as sql_utils
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
connection = sql_utils.connect_database('ubuntu-mariadb.home','admin','password','inventory')
|
||||
|
||||
@app.route('/asset_list')
|
||||
def asset_list():
|
||||
data = sql_utils.read_query(connection,'select * from assets;')
|
||||
return data
|
||||
|
||||
@app.route("/")
|
||||
def default():
|
||||
return render_template('main.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import utils.sql_utils as sql_utils
|
||||
import string, random
|
||||
|
||||
connection = sql_utils.connect_database('ubuntu-mariadb.home','admin','password','inventory')
|
||||
|
||||
alphabet = string.ascii_uppercase + string.digits
|
||||
|
||||
for i in range(0,100):
|
||||
serial = "".join(random.choices(alphabet, k=12))
|
||||
query = f'insert into assets (serial) values ("{serial}")'
|
||||
sql_utils.send_query(connection, query)
|
||||
print(f'Inserting: {serial}')
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<style>
|
||||
td {
|
||||
outline: black 1px solid;
|
||||
margin: 2px;
|
||||
padding: 2px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<h1>Assets</h1>
|
||||
<table id="asset_table">
|
||||
<tr>
|
||||
<td>Asset tag</td>
|
||||
<td>Serial Number</td>
|
||||
<td>Model</td>
|
||||
<td>Category</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<button type="button" onclick="get_assets()">get assets</button>
|
||||
|
||||
<script>
|
||||
async function get_assets() {
|
||||
for (let element of document.getElementsByClassName('asset_data')) {
|
||||
element.remove();
|
||||
}
|
||||
let table = document.getElementById("asset_table")
|
||||
await fetch('/asset_list')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
for (let entry of data) {
|
||||
let tr = document.createElement('tr');
|
||||
tr.classList.add('asset_data');
|
||||
for (let datum of entry) {
|
||||
let td = document.createElement('td');
|
||||
td.innerHTML = datum;
|
||||
tr.appendChild(td);
|
||||
}
|
||||
table.appendChild(tr);
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
def connect_database(host, username, password, database):
|
||||
connection = None
|
||||
try:
|
||||
connection = mysql.connector.connect(
|
||||
host=host,
|
||||
user=username,
|
||||
passwd=password,
|
||||
database=database
|
||||
)
|
||||
print("SQL connection established")
|
||||
except Error as err:
|
||||
print(f'Error: {err}')
|
||||
|
||||
return connection
|
||||
|
||||
def send_query(connection, query):
|
||||
cursor = connection.cursor()
|
||||
result = None
|
||||
try:
|
||||
cursor.execute(query)
|
||||
connection.commit()
|
||||
return result
|
||||
except Error as err:
|
||||
print(f'Error: {err}')
|
||||
|
||||
def read_query(connection, query):
|
||||
cursor = connection.cursor()
|
||||
result = None
|
||||
try:
|
||||
cursor.execute(query)
|
||||
result = cursor.fetchall()
|
||||
connection.commit()
|
||||
return result
|
||||
except Error as err:
|
||||
print(f'Error: {err}')
|
||||
Loading…
Reference in New Issue