]> OzVa Git service - delta-velorum/commitdiff
Built database read system and rebuild admin start
authorMax Value <greenwoodw50@gmail.com>
Mon, 30 Mar 2026 22:16:08 +0000 (23:16 +0100)
committerMax Value <greenwoodw50@gmail.com>
Mon, 30 Mar 2026 22:16:08 +0000 (23:16 +0100)
13 files changed:
director/__init__.py
director/database/__init__.py [new file with mode: 0644]
director/database/read.py [new file with mode: 0644]
director/database/utils.py [new file with mode: 0644]
director/src/admin.ts [new file with mode: 0644]
director/templates/admin.html
director/templates/admin/float.html [new file with mode: 0644]
director/templates/admin/list.html [deleted file]
director/templates/admin/number.html
director/templates/admin/select.html [deleted file]
director/templates/admin/text.html
director/templates/admin/textarea.html
director/templates/admin/trigger.html [new file with mode: 0644]

index 58b1e7af8f7ea49f0ab43e1531252eccf0610879..9311785278deb8890cc4ca559da5ca1475d17eb4 100644 (file)
@@ -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/<path:filename>", 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 (file)
index 0000000..efdbc2f
--- /dev/null
@@ -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 (file)
index 0000000..8c5f012
--- /dev/null
@@ -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 (file)
index 0000000..f564caa
--- /dev/null
@@ -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 (file)
index 0000000..f8d100a
--- /dev/null
@@ -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<Element> =
+               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)
+                               });
+                       }
+               })
+       }
+}
index ab7de6b2862022da1ffc2b6c807623df21a06aa5..5fdfac82e755fe7a5e206f0de63b5590e7a79743 100644 (file)
@@ -4,9 +4,60 @@
 
 #}
 <html>
+       <head>
+               <script type="module" defer>
+
+import { setupTrigger } from "/script/admin.js";
+setupTrigger();
+
+               </script>
+       </head>
        <body>
-               <form>
-                       {% include "admin/list.html" %}
-               <form>
+               {% for table in current %}
+                       <details>
+                               <summary>{{ table }}</summary>
+                               <form>
+                                       {% for row in current[table] %}
+
+                                               {# CREATE RADIO BUTTON FOR ROW #}
+
+                                               {% if loop.index0 == indexes["int--" ~ table] %}
+                                                       <input type="radio" name="{{ "indexes~~int--" ~ table }}" value="{{ loop.index0 }}" checked="checked" >
+                                               {% else %}
+                                                       <input type="radio" name="{{ "indexes~~int--" ~ table }}" value="{{ loop.index0 }}" >
+                                               {% endif %}
+
+
+                                               {# CREATE FEILDS #}
+
+                                               <div style="display: inline-block; vertical-align: middle; border: 1px solid gray;">
+                                                       {% 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 %}
+
+                                                               <br>
+                                                       {% endfor %}
+                                               </div><br>
+
+                                       {% endfor %}
+                               </form>
+                       </details>
+               {% endfor %}
        </body>
 </html>
diff --git a/director/templates/admin/float.html b/director/templates/admin/float.html
new file mode 100644 (file)
index 0000000..98a5c86
--- /dev/null
@@ -0,0 +1,2 @@
+<label>{{ label }}</label><br>
+<input type="number" name="{{ name }}" value="{{ value }}" step="0.01" />
diff --git a/director/templates/admin/list.html b/director/templates/admin/list.html
deleted file mode 100644 (file)
index a5788c5..0000000
+++ /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 %}
-       <br>
-{% endfor %}
index 6beaef2b105d33e206e0f1ae0fd4dd976fa1d566..8014eda105d6043071364283c751e9efe0410e0a 100644 (file)
@@ -1,2 +1,2 @@
-<label>{{ element.name }}</label><br>
-<input type="number" name="{{ element.name }}" value="{{ element.text }}" />
+<label>{{ label }}</label><br>
+<input type="number" name="{{ name }}" value="{{ value }}" />
diff --git a/director/templates/admin/select.html b/director/templates/admin/select.html
deleted file mode 100644 (file)
index 0fe6770..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<details>
-    <summary>{{ element.name }}</summary>
-       <input type="radio" name="{{ element.name }}" value="soup" checked />
-       <div style="display: inline-block; vertical-align: middle;">
-               {% block list %}
-                       {% set state = element.elements %}
-                       {% include "admin/list.html" %}
-               {% endblock %}
-       </div>
-</details>
index 3ea4709c4d30fd5b158ee7564f6c67c4ecc068d4..cb4f369e19270528019fc30004e58b8a702b3a93 100644 (file)
@@ -1,3 +1,3 @@
-<label>{{ element.name }}</label><br>
-<input type="text" name="{{ element.name }}" value="{{ element.text }}" />
+<label>{{ label }}</label><br>
+<input type="text" name="{{ name }}" value="{{ value }}" />
 
index f22eb50e2ee5c499296d4148575076afbe0b516b..e38b563273865ec42bcce01a83acdb73890988cd 100644 (file)
@@ -1,4 +1,4 @@
-<label>{{ element.name }}</label><br>
-<textarea name="{{ element.name }}">
-       {{- element.default -}}
+<label>{{ label }}</label><br>
+<textarea name="{{ name }}">
+       {{- value -}}
 </textarea>
diff --git a/director/templates/admin/trigger.html b/director/templates/admin/trigger.html
new file mode 100644 (file)
index 0000000..208cb05
--- /dev/null
@@ -0,0 +1,4 @@
+<noscript style="color: red;">Trigger will not function without JS</noscript><br>
+<button type="button" name="{{ name }}" value="{{ label }}" class="trigger">
+       {{ label }}
+</button>