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.
163 lines
6.7 KiB
163 lines
6.7 KiB
from nicegui import ui, app |
|
from datetime import datetime, timedelta |
|
from montydb import MontyClient |
|
from montydb.types.objectid import ObjectId |
|
import locale |
|
from pages import manager, user |
|
import signal |
|
import os |
|
|
|
from fastapi.responses import RedirectResponse |
|
|
|
passwords = {'admin': [0,'sw1ng3rs',0, "Matteo Benedetto"], 'user': [1,'pass',1, "Pippo"]} |
|
# Set the locale to Italian |
|
locale.setlocale(locale.LC_ALL, 'it_IT.utf8') |
|
|
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif') |
|
############# |
|
|
|
# Create a connection |
|
conn = MontyClient("db") |
|
|
|
# Access your database and collection |
|
db = conn.letsswing |
|
|
|
|
|
# Routes |
|
@ui.page('/manager/{sede}/{livello}', title="Let's Swing Admin", favicon="assets/favicon.ico") |
|
def manager_page(sede:int, livello:int) -> None: |
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif') |
|
############# |
|
manage = manager.Manager() |
|
manage.main(db, sede, livello) |
|
|
|
# Routes |
|
@ui.page('/', title="Let's Swing Admin", favicon="assets/favicon.ico") |
|
def index_page() -> None: |
|
## LOGIN CHECK |
|
if not app.storage.user.get('authenticated', False): |
|
return RedirectResponse('/login') |
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif') |
|
############# |
|
|
|
def choose(sede, livello): |
|
if int(livello) < 0: |
|
page.refresh(sede) |
|
else: |
|
ui.open(f"/manager/{sede}/{livello}") |
|
@ui.refreshable |
|
def page(sede): |
|
if int(sede) <0: |
|
results = db.sedi.find() |
|
ui.button(on_click=lambda: (app.storage.user.clear(), ui.open('/login')), icon='logout').props('outline round') |
|
ui.row().classes("w-full") |
|
with ui.column().classes("w-full h-screen place-items-center items-center").style(""): |
|
ui.row().classes("grow") |
|
for result in results: |
|
ui.button(text=str(result['nome']).capitalize(),on_click=lambda id=result['id']:choose(id, -1)) |
|
ui.row().classes("grow") |
|
else: |
|
results = db.livelli.find() |
|
|
|
with ui.row().classes("w-full"): |
|
ui.icon("chevron_left", size="lg").on("click", lambda: page.refresh(-1)) |
|
with ui.column().classes("w-full h-screen place-items-center items-center"): |
|
ui.row().classes("grow") |
|
for result in results: |
|
ui.button(text=str(result['nome']).capitalize(),on_click=lambda id=result['id']:choose(sede, id)) |
|
ui.row().classes("grow") |
|
|
|
## FRONTEND |
|
page(-1) |
|
|
|
|
|
@ui.page('/user/{userid}', title="Let's Swing Admin", favicon="assets/favicon.ico") |
|
def user_page(userid: str) -> None: |
|
|
|
## LOGIN CHECK |
|
if not app.storage.user.get('authenticated', False): |
|
return RedirectResponse('/login') |
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif') |
|
############# |
|
|
|
user.main(userid, db) |
|
|
|
@ui.page('/edit/{userid}/{sede}/{livello}', title="Let's Swing Admin", favicon="assets/favicon.ico") |
|
def edit_page(userid: str, sede: int, livello: int) -> None: |
|
|
|
## LOGIN CHECK |
|
if not app.storage.user.get('authenticated', False): |
|
return RedirectResponse('/login') |
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif') |
|
############# |
|
if userid!= "new": |
|
result = db.users.find_one({"_id":ObjectId(userid)}) |
|
|
|
|
|
async def save(): |
|
data_submit = {k: v.value for k, v in data.items()} |
|
if userid == "new": |
|
data_submit['pagamenti']=[] |
|
data_submit['presenze']=[] |
|
db.users.insert_one(data_submit) |
|
else: |
|
print(data_submit) |
|
db.users.update_one( |
|
{"_id": ObjectId(userid)}, |
|
{"$set": data_submit} |
|
) |
|
os.kill(os.getpid(), signal.SIGUSR1) |
|
await ui.run_javascript('history.back()', respond=False) |
|
|
|
ui.icon("chevron_left", size="lg").on("click", lambda: ui.run_javascript('history.back()', respond=False)) |
|
with ui.column().classes("w-full h-screen place-items-center items-center").style(""): |
|
data = {} |
|
ui.row().classes("grow") |
|
data['nome'] = ui.input(value=str(result['nome']).capitalize() if userid != "new" else None, placeholder="Nome").props('outline').classes('w-full') |
|
data['cognome'] = ui.input(value=str(result['cognome']).capitalize() if userid != "new" else None, placeholder="Cognome").props('outline').classes('w-full') |
|
data['telefono'] = ui.input(value=str(result['telefono']).capitalize() if userid != "new" else None, placeholder="Telefono").props('outline').classes('w-full') |
|
data['email'] = ui.input(value=str(result['email']).capitalize() if userid != "new" else None, placeholder="email").props('outline').classes('w-full') |
|
options = {o['id']: str(o['nome']).capitalize() for o in db.sedi.find()} |
|
data['sede'] = ui.select(options, value=result['sede'] if userid != "new" else sede).props('outline').classes('w-full') |
|
options = {o['id']: str(o['nome']).capitalize() for o in db.livelli.find()} |
|
data['livello'] = ui.select(options, value=result['livello'] if userid != "new" else livello).props('outline').classes('w-full') |
|
ui.button(text="Salva", on_click=save) |
|
ui.row().classes("grow") |
|
|
|
@ui.page('/login',title="Let's Swing Admin", favicon="assets/favicon.ico") |
|
def login() -> None: |
|
|
|
#styling |
|
ui.colors(primary='black', secondary="lightgray") |
|
ui.query('body').style(f'font-family: sans-serif;background-color:black') |
|
############# |
|
|
|
def try_login() -> None: # local function to avoid passing username and password as arguments |
|
if passwords.get(username.value)[1] == password.value: |
|
app.storage.user.update({'username': username.value, 'authenticated': True, 'id' : passwords.get(username.value)[0], "level": passwords.get(username.value)[2], "name": passwords.get(username.value)[3]}) |
|
ui.open('/') |
|
else: |
|
ui.notify('username o password errata', color='negative') |
|
|
|
if app.storage.user.get('authenticated', False): |
|
return RedirectResponse('/') |
|
with ui.card().classes('absolute-center items-center'): |
|
ui.image('assets/logo.svg') |
|
ui.label('Let\'s Swing Admin').classes('text-2xl') |
|
username = ui.input('Username').on('keydown.enter', try_login).classes('w-full') |
|
password = ui.input('Password', password=True, password_toggle_button=True).on('keydown.enter', try_login) |
|
ui.button('Log in', on_click=try_login) |
|
|
|
|
|
# Run main loop |
|
ui.run(host="0.0.0.0", storage_secret='THIS_NEEDS_TO_BE_CHANGED')
|
|
|