You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

102 lines
4.9 KiB

from nicegui import ui
import signal
import os
from datetime import datetime, timedelta
from montydb.types.objectid import ObjectId
from fastapi.responses import RedirectResponse
def main(userid, db):
collection = db.users
async def delete_user():
result = await dialog
if result:
db.users.delete_one({"_id":ObjectId(userid)})
os.kill(os.getpid(), signal.SIGUSR1)
await ui.run_javascript('history.back()', respond=False)
def register_payment(month, flag):
if flag:
collection.update_one(
{"_id": ObjectId(userid)},
{"$push": {"pagamenti": month.strftime('%Y-%m-%d %H:%M:%S')}}
)
else:
collection.update_one(
{"_id": ObjectId(userid)},
{"$pull": {"pagamenti": month.strftime('%Y-%m-%d %H:%M:%S')}}
)
os.kill(os.getpid(), signal.SIGUSR1)
page.refresh()
def onSignal(signum, frame):
page.refresh()
signal.signal(signal.SIGUSR1, onSignal)
# Calculate last start date
start_date = datetime(datetime.now().year - (datetime.now().month < 8), 9, 1)
# Create a list for the next 12 months
next_12_months = [(start_date + timedelta(days=i*32)).replace(day=1) for i in range(10)]
@ui.refreshable
def page():
try:
result = collection.find_one({"_id":ObjectId(userid)})
except Exception as e:
print(e)
return RedirectResponse('/')
title.set_text(f'{result["nome"]} {result["cognome"][0].upper() if result["cognome"] else None}.')
with ui.grid(columns=1):
with ui.row():
ui.label("Informazioni").classes('text-lg').style("font-weight:700")
ui.icon("edit", size="sm").on("click", lambda: ui.open(f'/edit/{userid}/0/0'))
ui.icon("delete", size="sm").on("click", delete_user)
ui.label(f'{result["nome"]}').classes('text-lg')
ui.label(f'{result["cognome"]}').classes('text-lg')
ui.label(f'{result["telefono"]}').classes('text-lg')
if 'instagram' in result:
ui.label(f'{result["instagram"]}').classes('text-lg')
ui.label(f'{str(db.sedi.find_one({"id":result["sede"]})["nome"]).capitalize()}').classes('text-lg')
ui.label(f'{str(db.livelli.find_one({"id":result["livello"]})["nome"]).capitalize()}').classes('text-lg')
with ui.grid(columns=2).classes("w-full items-center"):
ui.label("Mese").classes('text-lg').style("font-weight:700")
with ui.grid(columns=2).style("text-align:center").classes("place-items-center"):
ui.label(text="frequentato").style('font-size:0.5rem;font-weight:700')
for month in next_12_months:
presenze = [datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S') for timestamp in result['presenze']]
start, end = month.replace(day=1), (month + timedelta(days=32)).replace(day=1) - timedelta(seconds=1)
timestamp_in_month = any(start <= timestamp <= end for timestamp in presenze)
ui.label(month.strftime("%B").capitalize()).classes('text-lg')
with ui.grid(columns=2).style("text-align:center").classes("place-items-center"):
ui.icon("done").classes('text-lg') if timestamp_in_month else ui.label(" ")
if month.strftime('%Y-%m-%d %H:%M:%S') in result["pagamenti"]:
ui.button(color="primary", text= "PAGATO", on_click= lambda dt=month: register_payment(dt, False)).style('font-size:0.5rem;font-weight:700')
else:
if timestamp_in_month:
ui.button(color="negative", text= "NON PAGATO", on_click= lambda dt=month: register_payment(dt, True)).style('font-size:0.4rem;font-weight:700;white-space: nowrap;')
else:
ui.button(text= "PAGA", on_click= lambda dt=month: register_payment(dt, True)).style('font-size:0.5rem;font-weight:700;').props('outline')
## FRONTEND
with ui.dialog() as dialog, ui.card():
ui.label('Sicuro di voler cancellare l\'utente?')
with ui.row().classes("w-full"):
ui.column().classes("grow")
ui.button('', on_click=lambda: dialog.submit(True))
ui.button('No', on_click=lambda: dialog.submit(False))
ui.column().classes("grow")
with ui.row().classes("w-full"):
ui.icon("chevron_left", size="lg").on("click", lambda: ui.run_javascript('history.back()', respond=False))
with ui.row().classes("w-full"):
with ui.column().classes("grow"):
title = ui.label().classes('text-xl').style("font-weight:700;margin-top: 10px")
with ui.column():
ui.image('assets/logo.svg').style('width:50px;margin:auto auto')
page()