from nicegui import ui import signal import os from datetime import datetime, timedelta from montydb.database import MontyDatabase class Manager: def __init__(self): self.current_time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(seconds=1) self.current_index = 0 def main(self, db: MontyDatabase, sede, livello): users = db.users lezioni = db.lezioni lezioniCursore = lezioni.find({"livello": livello,"sede": sede}).sort([("data", 1)]) lezioniArray = [datetime.strptime(l["data"],"%Y-%m-%d %H:%M:%S") for l in lezioniCursore] for i, l in enumerate(lezioniArray): if l > self.current_time: self.current_index = i self.current_time = l break def registra_presenza(id, flag): current_day = self.current_time.replace(hour=0, minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S') if flag: users.update_one( {"_id": id}, {"$push": {"presenze": current_day}} ) else: users.update_one( {"_id": id}, {"$pull": {"presenze": current_day}} ) os.kill(os.getpid(), signal.SIGUSR1) @ui.refreshable def time_selector(): def set_time(isNext): if isNext: self.current_index += 1 else: self.current_index -= 1 self.current_time = lezioniArray[self.current_index] time_selector.refresh() tabellaUtenti.refresh() with ui.row().classes("w-full items-center"): ui.column().classes("grow") with ui.column(): if self.current_index > 0: ui.icon("arrow_left",size="md").on("click", lambda: set_time(False)) date_label = self.current_time.strftime('%d %B, %Y') ui.label(date_label).style("margin:auto auto;font-weight:600").classes('text-md') with ui.column(): if self.current_index +1 in range(0,len(lezioniArray)): ui.icon("arrow_right",size="md").on("click", lambda: set_time(True)) ui.column().classes("grow") @ui.refreshable def tabellaUtenti(): rows = users.find({"sede": sede, "livello": livello}) current_day = self.current_time.replace(hour=0, minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S') current_month = self.current_time.replace(day=1, hour=0, minute=0, second=0, microsecond=0).strftime('%Y-%m-%d %H:%M:%S') for row in rows: with ui.row().classes("w-full"): with ui.column().classes("grow"): ui.label(f"{row['nome']} {row['cognome'][0].upper() if row['cognome'] else None}.").style('color:'+( "red" if not current_month in row['pagamenti'] and current_day in row['presenze'] else "black") ).classes('text-md').on("click", lambda row_id=row['_id']: ui.open(f'/user/{row_id}')) with ui.column(): if current_day in row["presenze"]: ui.button(color="primary", text= "presente", on_click= lambda row_id=row['_id']: registra_presenza(row_id, False)) else: ui.button(color="secondary", text= "assente", on_click= lambda row_id=row['_id']: registra_presenza(row_id, True)).style("min-width:100px") ui.separator() def onSignal(signum, frame): print("Signal") tabellaUtenti.refresh() signal.signal(signal.SIGUSR1, onSignal) liv = db.livelli.find_one({"id":livello}) sed = db.sedi.find_one({"id":sede}) ## FRONTEND with ui.row().classes("w-full"): ui.icon("chevron_left", size="lg").on("click", lambda: ui.open("/")) with ui.row().classes("w-full"): with ui.column().classes("grow"): ui.label(f"{str(liv['nome']).capitalize()} a {str(sed['nome']).capitalize()}").classes('text-xl').style("font-weight:700;margin-top: 10px") with ui.column(): ui.image('assets/logo.svg').style('width:70px;margin:auto auto') time_selector() tabellaUtenti() with ui.row().classes("w-full items-center content-center"): ui.button("AGGIUNGI ALLIEV*", icon="add", on_click=lambda: ui.open(f"/edit/new/{sede}/{livello}")).props('outline rounded').classes('shadow-lg').style("margin:auto;padding:10px 20px;font-weight:600")