commit ce1b09ea0e8ac68edf14f46fe6b5f138bc1ca8cb Author: enne2 Date: Thu Sep 14 23:26:31 2023 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/letsswing.db b/letsswing.db new file mode 100644 index 0000000..9ff6574 Binary files /dev/null and b/letsswing.db differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..3248667 --- /dev/null +++ b/main.py @@ -0,0 +1,72 @@ +from nicegui import ui +import sqlite3 +import signal +import os + + + + +def dict_factory(cursor, row): + d = {} + for idx, col in enumerate(cursor.description): + d[col[0]] = row[idx] + return d + +def read_data(query): + try: + cursor.execute(query) + rows = cursor.fetchall() + return rows + except sqlite3.Error as e: + ui.notify("{e}") + return None + +def registra_presenza(id, flag): + + try: + cursor.execute("UPDATE users SET presenze = ? WHERE id = ?", (1 if flag==True else 0, id) ) + conn.commit() + + finally: + tabellaUtenti.refresh() + os.kill(os.getpid(), signal.SIGUSR1) + +@ui.refreshable +def tabellaUtenti(): + rows = read_data("SELECT u.*, (SELECT data FROM pagamenti WHERE user_id=u.id AND date(data) = '2021-05-31' ORDER BY id DESC LIMIT 1) as pagamenti FROM users as u;") + for row in rows: + with ui.row().classes("w-full"): + with ui.column().classes("grow"): + ui.label(f"{row['nome']} {row['cognome'][0].upper()}.").style('color:'+("black" if row['pagamenti'] else "red")) + with ui.column(): + if row["presenze"] == 1: + ui.button(color="primary", text= "presente", on_click= lambda: registra_presenza(row["id"], False)) + else: + ui.button(color="secondary", text= "assente", on_click= lambda: registra_presenza(row["id"], True)) + + ui.separator() + +def onSignal(signum, frame): + tabellaUtenti.refresh() + +signal.signal(signal.SIGUSR1, onSignal) + +# START + +conn = sqlite3.connect('letsswing.db') +conn.row_factory = dict_factory +cursor = conn.cursor() + + + +with ui.row().classes("w-full"): + with ui.column().classes("grow"): + ui.label("Beginner a matera") + with ui.column(): + ui.icon("thumb_up") + +tabellaUtenti() + + +ui.run(host="0.0.0.0") +