From 4e4a1acdaf9eb60013ece6ef6ce95ea792737ce6 Mon Sep 17 00:00:00 2001 From: Max Value Date: Mon, 30 Mar 2026 23:16:08 +0100 Subject: [PATCH] Built database read system and rebuild admin start --- director/__init__.py | 8 ++++ director/database/__init__.py | 12 ++++++ director/database/read.py | 23 +++++++++++ director/database/utils.py | 20 +++++++++ director/src/admin.ts | 25 +++++++++++ director/templates/admin.html | 57 ++++++++++++++++++++++++-- director/templates/admin/float.html | 2 + director/templates/admin/list.html | 18 -------- director/templates/admin/number.html | 4 +- director/templates/admin/select.html | 10 ----- director/templates/admin/text.html | 4 +- director/templates/admin/textarea.html | 6 +-- director/templates/admin/trigger.html | 4 ++ 13 files changed, 155 insertions(+), 38 deletions(-) create mode 100644 director/database/__init__.py create mode 100644 director/database/read.py create mode 100644 director/database/utils.py create mode 100644 director/src/admin.ts create mode 100644 director/templates/admin/float.html delete mode 100644 director/templates/admin/list.html delete mode 100644 director/templates/admin/select.html create mode 100644 director/templates/admin/trigger.html diff --git a/director/__init__.py b/director/__init__.py index 58b1e7a..9311785 100644 --- a/director/__init__.py +++ b/director/__init__.py @@ -2,6 +2,8 @@ from flask import Flask, render_template, send_from_directory from flask_socketio import SocketIO, emit import os +from director.database.read import read as database_read + app = Flask(__name__, instance_relative_config=False) socketio = SocketIO(app, logger=True, engineio_logger=True) @@ -9,12 +11,18 @@ socketio = SocketIO(app, logger=True, engineio_logger=True) def index(): return render_template("index.html") +@app.route("/admin", methods=['get']) +def admin(): + indexes, current = database_read() + return render_template("admin.html", indexes=indexes, current=current) + @app.route("/script/", methods=["get"]) def script(filename): return send_from_directory(os.path.join(app.root_path, "build"), filename) @app.route("/test") def test(): + return [database_read()] #emit("update", ["data__product", {"data":{"product":{"currentPrice":100}}}], json=True, namespace="/", broadcast=True) return "" diff --git a/director/database/__init__.py b/director/database/__init__.py new file mode 100644 index 0000000..efdbc2f --- /dev/null +++ b/director/database/__init__.py @@ -0,0 +1,12 @@ +""" + package to read and write to the database + + check if file exists and pragam check the database on init +""" + +from .utils import integrity_check +import sqlite3 +import os + +assert os.path.exists("./data/main.db"), "Database missing" +assert integrity_check(), "Database integrity error" diff --git a/director/database/read.py b/director/database/read.py new file mode 100644 index 0000000..8c5f012 --- /dev/null +++ b/director/database/read.py @@ -0,0 +1,23 @@ +import sqlite3 + +from .utils import list_tables + +def read(): + tables = list_tables() + + with sqlite3.connect("./data/main.db") as connection: + cursor = connection.cursor() + + data = {} + for table in tables: + cursor.execute(f"SELECT * FROM {table};") + + names = list(map(lambda x: x[0], cursor.description)) + rows = [dict(zip(names, row)) for row in cursor.fetchall()] + + if table == "indexes": + indexes = rows[0] + else: + data.update({table: rows}) + + return indexes, data diff --git a/director/database/utils.py b/director/database/utils.py new file mode 100644 index 0000000..f564caa --- /dev/null +++ b/director/database/utils.py @@ -0,0 +1,20 @@ +import sqlite3 + +def integrity_check(): + """ + check the database is ok and return true if it is + """ + with sqlite3.connect("./data/main.db") as connection: + cursor = connection.cursor() + integrity = cursor.execute('PRAGMA integrity_check') + + return integrity.fetchone()[0] == "ok" + +def list_tables(): + """ + get a list of all the tables in the database + """ + with sqlite3.connect("./data/main.db") as connection: + cursor = connection.cursor() + cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") + return [table[0] for table in cursor.fetchall()] diff --git a/director/src/admin.ts b/director/src/admin.ts new file mode 100644 index 0000000..f8d100a --- /dev/null +++ b/director/src/admin.ts @@ -0,0 +1,25 @@ +interface Body { + [index: string]: number; +} + +export function setupTrigger() { + // setup all trigger buttons to send post request on click + const el: HTMLCollectionOf = + document.getElementsByClassName("trigger"); + + for (let e of el) { + e.addEventListener("click", (event) => { + let time: number = Math.round(Date.now() / 1000); + let body: Body = {}; + + if (event.target instanceof Element) { + body[`${event.target.getAttribute('name')}`] = time; + + fetch(".", { + method: "POST", + body: JSON.stringify(body) + }); + } + }) + } +} diff --git a/director/templates/admin.html b/director/templates/admin.html index ab7de6b..5fdfac8 100644 --- a/director/templates/admin.html +++ b/director/templates/admin.html @@ -4,9 +4,60 @@ #} + + + -
- {% include "admin/list.html" %} - + {% for table in current %} +
+ {{ table }} + + {% for row in current[table] %} + + {# CREATE RADIO BUTTON FOR ROW #} + + {% if loop.index0 == indexes["int--" ~ table] %} + + {% else %} + + {% endif %} + + + {# CREATE FEILDS #} + +
+ {% for feild in row %} + + {# SELECT ELEMENT FROM TYPE #} + + {% set type, label = feild.split('--') %} + {% set value = row[feild] %} + {% set name = table ~ "~~" ~ feild %} + + {% if type == "textarea" %} + {% include "admin/textarea.html" %} + {% elif type == "text" %} + {% include "admin/text.html" %} + {% elif type == "number" %} + {% include "admin/number.html" %} + {% elif type == "float" %} + {% include "admin/float.html" %} + {% elif type == "trigger" %} + {% include "admin/trigger.html" %} + {% endif %} + +
+ {% endfor %} +

+ + {% endfor %} + +
+ {% endfor %} diff --git a/director/templates/admin/float.html b/director/templates/admin/float.html new file mode 100644 index 0000000..98a5c86 --- /dev/null +++ b/director/templates/admin/float.html @@ -0,0 +1,2 @@ +
+ diff --git a/director/templates/admin/list.html b/director/templates/admin/list.html deleted file mode 100644 index a5788c5..0000000 --- a/director/templates/admin/list.html +++ /dev/null @@ -1,18 +0,0 @@ -{# - - interates through the state variable and includes the relevant template for - each element. not tested more than double nested - -#} -{% for element in state %} - {% if element.tag == "textarea" %} - {% include "admin/textarea.html" %} - {% elif element.tag == "select" %} - {% include "admin/select.html" %} - {% elif element.tag == "text" %} - {% include "admin/text.html" %} - {% elif element.tag == "number" %} - {% include "admin/number.html" %} - {% endif %} -
-{% endfor %} diff --git a/director/templates/admin/number.html b/director/templates/admin/number.html index 6beaef2..8014eda 100644 --- a/director/templates/admin/number.html +++ b/director/templates/admin/number.html @@ -1,2 +1,2 @@ -
- +
+ diff --git a/director/templates/admin/select.html b/director/templates/admin/select.html deleted file mode 100644 index 0fe6770..0000000 --- a/director/templates/admin/select.html +++ /dev/null @@ -1,10 +0,0 @@ -
- {{ element.name }} - -
- {% block list %} - {% set state = element.elements %} - {% include "admin/list.html" %} - {% endblock %} -
-
diff --git a/director/templates/admin/text.html b/director/templates/admin/text.html index 3ea4709..cb4f369 100644 --- a/director/templates/admin/text.html +++ b/director/templates/admin/text.html @@ -1,3 +1,3 @@ -
- +
+ diff --git a/director/templates/admin/textarea.html b/director/templates/admin/textarea.html index f22eb50..e38b563 100644 --- a/director/templates/admin/textarea.html +++ b/director/templates/admin/textarea.html @@ -1,4 +1,4 @@ -
- diff --git a/director/templates/admin/trigger.html b/director/templates/admin/trigger.html new file mode 100644 index 0000000..208cb05 --- /dev/null +++ b/director/templates/admin/trigger.html @@ -0,0 +1,4 @@ +
+ -- 2.39.2