From 5d9ac5e18bc290cdb7cf7a30028f6f8e575f87de Mon Sep 17 00:00:00 2001 From: Billy Date: Mon, 29 Apr 2024 05:36:05 -0400 Subject: [PATCH] inital commit --- .gitignore | 2 ++ app.py | 18 +++++++++++++++ generate_data.py | 12 ++++++++++ templates/main.html | 56 +++++++++++++++++++++++++++++++++++++++++++++ utils/__init__.py | 0 utils/sql_utils.py | 38 ++++++++++++++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 generate_data.py create mode 100644 templates/main.html create mode 100644 utils/__init__.py create mode 100644 utils/sql_utils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e5ac79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +__pycache__ \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..4289658 --- /dev/null +++ b/app.py @@ -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() \ No newline at end of file diff --git a/generate_data.py b/generate_data.py new file mode 100644 index 0000000..4e9b39d --- /dev/null +++ b/generate_data.py @@ -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}') \ No newline at end of file diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..a1fbc4f --- /dev/null +++ b/templates/main.html @@ -0,0 +1,56 @@ + + + + + + + Document + + + + + +

Assets

+ + + + + + + +
Asset tagSerial NumberModelCategory
+ + + + + + + + \ No newline at end of file diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/sql_utils.py b/utils/sql_utils.py new file mode 100644 index 0000000..a6cc7e0 --- /dev/null +++ b/utils/sql_utils.py @@ -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}') \ No newline at end of file