Native gui: ongoing work, cables and fuses

This commit is contained in:
2026-09-13 17:19:37 +02:00
parent d72147c012
commit 2250fb9743
15 changed files with 2064 additions and 54 deletions
+70 -1
View File
@@ -1,6 +1,7 @@
import MySQLdb
import secrets
class Database():
class Database:
def __init__(self, hostname, dbname):
self.hostname = hostname
@@ -48,6 +49,10 @@ class Database():
def is_connected(self):
return self.conn.open
def get_timestamp(self):
self.cur.execute("SELECT CURRENT_TIMESTAMP")
return self.cur.fetchone()[0]
def last_error(self):
if not self.error:
return(0, '')
@@ -56,6 +61,70 @@ class Database():
self.error = None
return(code, text)
def load_enum(self, table, column):
"""
TODO this is only an example! perhaps other keys needed
Load an enum directly from a database table and provide it as a
dictionary for use in, for example, selection lists.
"""
sql = (
"SELECT TRIM(TRAILING ')' FROM SUBSTRING(column_type,6)) "
"FROM information_schema.columns "
"WHERE table_schema=DATABASE() AND table_name=%s AND column_name=%s AND data_type='enum'"
)
self.cur.execute(sql, (table, column))
row = self.cur.fetchone()
if not row:
return {}
return {
i: _(value.strip("'"))
for i, value in enumerate(row[0].split(','), 1)
}
def lock_acquire(self, table, key, userid):
token = secrets.token_hex(8)
# first try new lock
sql = (
"INSERT INTO recordlock (lock_table, lock_key, token, userid)"
"VALUES (%s, %s, %s, %s)"
)
try:
self.cur.execute(sql, (table, key, token, userid))
self.conn.commit()
return token
except self.conn.IntegrityError as e:
self.conn.rollback()
if e.errno != 1062:
raise
# lazy locking: lock exists but is old, so we use it
sql = (
"UPDATE recordlock "
"SET token=%s, userid=%s,"
" created_at=CURRENT_TIMESTAMP, last_seen=CURRENT_TIMESTAMP "
"WHERE lock_table=%s AND lock_key=%s AND last_seen<CURRENT_TIMESTAMP-INTERVAL 5 MINUTE"
)
self.cur.execute(sql, (token, userid, table, key))
self.conn.commit()
if self.cur.rowcount == 1:
return token
return None
def lock_refresh(self, token):
sql = (
"UPDATE recordlock "
"SET last_seen=current_timestamp() "
"WHERE token=%s"
)
self.cur.execute(sql, (token,))
self.conn.commit()
return self.cur.rowcount == 1
def lock_release(self, token):
sql = "DELETE FROM recordlock WHERE token=%s"
self.cur.execute(sql, (token,))
self.conn.commit()
return self.cur.rowcount == 1
def execute_write(self, sql, values=(), expected_rows=None):
"""
Execute an INSERT, UPDATE, or DELETE statement.