async def admin_delete_node(request: Request, node_short: str = Form(...), admin: User = Depends(get_current_admin)): node = Node.find_one({"short": node_short}) if node: await Node.delete_one({"short": node_short}) else: flash(request, f"L'argomento {node_short} non esiste.") return RedirectResponse(url="/", status_code=303)
async def admin_toggle_block(request: Request, user_email: EmailStr = Form(...), admin: User = Depends(get_current_admin)): user = await User.find_one({"email": user_email}) if user: await User.find_one_and_set( filter={"email": user_email}, set={"is_blocked": (False if user.is_blocked else True)}) else: flash(request, f"L'utente {user_email} non esiste.", "warning") return RedirectResponse(url="/", status_code=303)
async def admin_create_group(request: Request, short: str = Form(...), admin: User = Depends(get_current_admin)): group = await Group.find_one({"short": short}) if group: flash(request, f"Il gruppo {short} esiste già.", "warning") else: await Group.insert_one({ "short": short, "nodes": [], }) return RedirectResponse(url="/", status_code=303)
async def login_post(request: Request, login_form: LoginForm = Depends()): try: user: Optional[User] = await User.find_one({"email": login_form.email}) assert user.authenticate(login_form.password) request.session["authenticated_email"] = user.email except (ValidationError, KeyError, AttributeError, AssertionError): try: del request.session["authenticated_email"] except KeyError: pass flash(request, "Utente non riconosciuto.", "warning") return RedirectResponse(url="/", status_code=303)
async def admin_create_node(request: Request, node_short: str = Form(...), admin: User = Depends(get_current_admin)): node = await Node.find_one({"short": node_short}) print("Node is:", node) if node: flash(request, f"L'argomento {node_short} già esiste.", "warning") else: await Node.insert_one({ "short": node_short, "contents": [], }) return RedirectResponse(url="/", status_code=303)
async def signup_post(request: Request, signup_form: SignupForm = Depends(SignupForm)): user: Optional[User] = await User.find_one({"email": signup_form.email}) if user is None: await User.insert_one({ "email": signup_form.email, "password_hash": get_password_hash(signup_form.password), "username": "", "groups": [], }) flash(request, "Utente creato con successo.", "success") else: flash(request, "Non è stato possibile creare l'utente. Forse un duplicato?", "warning") return RedirectResponse(url="/", status_code=303)
async def admin_create_user(request: Request, user_email: EmailStr = Form(...), admin: User = Depends(get_current_admin)): user = await User.find_one({"email": user_email}) if user: flash(request, f"L'utente {user_email} esiste già.", "warning") else: await User.insert_one({ "email": user_email, "password_hash": get_password_hash("pass"), "username": "", "groups": [], }) return RedirectResponse(url="/", status_code=303)
async def user_change_password(request: Request, current_user: User = Depends(get_current_user), email: str = Form(""), old_password: str = Form(...), password: str = Form(...), password_confirmation: str = Form(...)): try: assert current_user.email == email assert current_user.authenticate(old_password) assert password == password_confirmation await User.find_one_and_set( filter={"email": current_user.email}, set={"password_hash": get_password_hash(password)}, ) except: flash(request, "Non è stato possibile modificare la password.", "warning") return RedirectResponse(url="/", status_code=303)