From d71a2e04fd2e1fad2789ca86fe331f80135bf245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20Garc=C3=ADa=20Amor?= Date: Mon, 2 Dec 2024 13:17:01 +0100 Subject: [PATCH] Categorize messages for better alerts --- supysonic/frontend/folder.py | 20 ++++----- supysonic/frontend/playlist.py | 14 +++---- supysonic/frontend/user.py | 74 ++++++++++++++++++--------------- supysonic/templates/layout.html | 12 +++--- 4 files changed, 64 insertions(+), 56 deletions(-) diff --git a/supysonic/frontend/folder.py b/supysonic/frontend/folder.py index d7f1a3c..60d9d83 100644 --- a/supysonic/frontend/folder.py +++ b/supysonic/frontend/folder.py @@ -46,10 +46,10 @@ def add_folder_post(): error = False name, path = map(request.form.get, ("name", "path")) if name in (None, ""): - flash("The name is required.") + flash("The name is required.", "danger") error = True if path in (None, ""): - flash("The path is required.") + flash("The path is required.", "danger") error = True if error: return render_template("addfolder.html") @@ -57,10 +57,10 @@ def add_folder_post(): try: FolderManager.add(name, path) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return render_template("addfolder.html") - flash(f"Folder '{name}' created. You should now run a scan") + flash(f"Folder '{name}' created. You should now run a scan", "success") return redirect(url_for("frontend.folder_index")) @@ -69,11 +69,11 @@ def add_folder_post(): def del_folder(id): try: FolderManager.delete(id) - flash("Deleted folder") + flash("Deleted folder", "success") except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") except Folder.DoesNotExist: - flash("No such folder", "error") + flash("No such folder", "danger") return redirect(url_for("frontend.folder_index")) @@ -90,10 +90,10 @@ def scan_folder(id=None): DaemonClient(current_app.config["DAEMON"]["socket"]).scan(folders) flash("Scanning started") except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") except Folder.DoesNotExist: - flash("No such folder", "error") + flash("No such folder", "danger") except DaemonUnavailableError: - flash("Can't start scan", "error") + flash("Can't start scan", "danger") return redirect(url_for("frontend.folder_index")) diff --git a/supysonic/frontend/playlist.py b/supysonic/frontend/playlist.py index 2dcedfd..dfb03a7 100644 --- a/supysonic/frontend/playlist.py +++ b/supysonic/frontend/playlist.py @@ -30,13 +30,13 @@ def resolve_and_inject_playlist(func): try: uid = uuid.UUID(uid) except ValueError: - flash("Invalid playlist id") + flash("Invalid playlist id", "warning") return redirect(url_for("frontend.playlist_index")) try: playlist = Playlist[uid] except Playlist.DoesNotExist: - flash("Unknown playlist") + flash("Unknown playlist", "warning") return redirect(url_for("frontend.playlist_index")) return func(uid, playlist) @@ -64,9 +64,9 @@ def playlist_export(uid, playlist): @resolve_and_inject_playlist def playlist_update(uid, playlist): if playlist.user_id != request.user.id: - flash("You're not allowed to edit this playlist") + flash("You're not allowed to edit this playlist", "danger") elif not request.form.get("name"): - flash("Missing playlist name") + flash("Missing playlist name", "danger") else: playlist.name = request.form.get("name") playlist.public = request.form.get("public") in ( @@ -78,7 +78,7 @@ def playlist_update(uid, playlist): "checked", ) playlist.save() - flash("Playlist updated.") + flash("Playlist updated.", "success") return playlist_details(str(uid)) @@ -87,9 +87,9 @@ def playlist_update(uid, playlist): @resolve_and_inject_playlist def playlist_delete(uid, playlist): if playlist.user_id != request.user.id: - flash("You're not allowed to delete this playlist") + flash("You're not allowed to delete this playlist", "danger") else: playlist.delete_instance() - flash("Playlist deleted") + flash("Playlist deleted", "success") return redirect(url_for("frontend.playlist_index")) diff --git a/supysonic/frontend/user.py b/supysonic/frontend/user.py index 1e329b3..bdafa23 100644 --- a/supysonic/frontend/user.py +++ b/supysonic/frontend/user.py @@ -37,10 +37,10 @@ def me_or_uuid(f, arg="uid"): try: user = UserManager.get(uid) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return redirect(url_for("frontend.index")) except User.DoesNotExist: - flash("No such user", "error") + flash("No such user", "danger") return redirect(url_for("frontend.index")) if kwargs: @@ -111,7 +111,7 @@ def update_clients(uid, user): ) prefs.save() - flash("Clients preferences updated.") + flash("Clients preferences updated.", "success") return user_profile(uid, user) @@ -121,10 +121,10 @@ def change_username_form(uid): try: user = UserManager.get(uid) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return redirect(url_for("frontend.index")) except User.DoesNotExist: - flash("No such user", "error") + flash("No such user", "danger") return redirect(url_for("frontend.index")) return render_template("change_username.html", user=user) @@ -136,20 +136,20 @@ def change_username_post(uid): try: user = UserManager.get(uid) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return redirect(url_for("frontend.index")) except User.DoesNotExist: - flash("No such user", "error") + flash("No such user", "danger") return redirect(url_for("frontend.index")) username = request.form.get("user") if username in ("", None): - flash("The username is required") + flash("The username is required", "danger") return render_template("change_username.html", user=user) if user.name != username: try: User.get(name=username) - flash("This name is already taken") + flash("This name is already taken", "danger") return render_template("change_username.html", user=user) except User.DoesNotExist: pass @@ -163,7 +163,7 @@ def change_username_post(uid): user.name = username user.admin = admin user.save() - flash(f"User '{username}' updated.") + flash(f"User '{username}' updated.", "success") else: flash(f"No changes for '{username}'.") @@ -198,16 +198,16 @@ def change_password_post(uid, user): if user.id == request.user.id: current = request.form.get("current") if not current: - flash("The current password is required") + flash("The current password is required", "danger") error = True new, confirm = map(request.form.get, ("new", "confirm")) if not new: - flash("The new password is required") + flash("The new password is required", "danger") error = True if new != confirm: - flash("The new password and its confirmation don't match") + flash("The new password and its confirmation don't match", "danger") error = True if not error: @@ -217,10 +217,10 @@ def change_password_post(uid, user): else: UserManager.change_password2(user.name, new) - flash("Password changed") + flash("Password changed", "success") return redirect(url_for("frontend.user_profile", uid=uid)) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return change_password_form(uid, user) @@ -240,22 +240,22 @@ def add_user_post(): args.pop, ("user", "passwd", "passwd_confirm"), (None,) * 3 ) if not name: - flash("The name is required.") + flash("The name is required.", "danger") error = True if not passwd: - flash("Please provide a password.") + flash("Please provide a password.", "danger") error = True elif passwd != passwd_confirm: - flash("The passwords don't match.") + flash("The passwords don't match.", "danger") error = True if not error: try: UserManager.add(name, passwd, **args) - flash(f"User '{name}' successfully added") + flash(f"User '{name}' successfully added", "success") return redirect(url_for("frontend.user_index")) except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") return add_user_form() @@ -265,11 +265,11 @@ def add_user_post(): def del_user(uid): try: UserManager.delete(uid) - flash("Deleted user") + flash("Deleted user", "success") except ValueError as e: - flash(str(e), "error") + flash(str(e), "danger") except User.DoesNotExist: - flash("No such user", "error") + flash("No such user", "danger") return redirect(url_for("frontend.user_index")) @@ -279,12 +279,15 @@ def del_user(uid): def lastfm_reg(uid, user): token = request.args.get("token") if not token: - flash("Missing LastFM auth token") + flash("Missing LastFM auth token", "warning") return redirect(url_for("frontend.user_profile", uid=uid)) lfm = LastFm(current_app.config["LASTFM"], user) status, error = lfm.link_account(token) - flash(error if not status else "Successfully linked LastFM account") + if not status: + flash(error, "danger") + else: + flash("Successfully linked LastFM account", "success") return redirect(url_for("frontend.user_profile", uid=uid)) @@ -294,7 +297,7 @@ def lastfm_reg(uid, user): def lastfm_unreg(uid, user): lfm = LastFm(current_app.config["LASTFM"], user) lfm.unlink_account() - flash("Unlinked LastFM account") + flash("Unlinked LastFM account", "success") return redirect(url_for("frontend.user_profile", uid=uid)) @@ -303,12 +306,15 @@ def lastfm_unreg(uid, user): def listenbrainz_reg(uid, user): token = request.args.get("token") if not token: - flash("Missing ListenBrainz auth token") + flash("Missing ListenBrainz auth token", "warning") return redirect(url_for("frontend.user_profile", uid=uid)) lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user) status, error = lbz.link_account(token) - flash(error if not status else "Successfully linked ListenBrainz account") + if not status: + flash(error, "danger") + else: + flash("Successfully linked ListenBrainz account", "success") return redirect(url_for("frontend.user_profile", uid=uid)) @@ -318,7 +324,7 @@ def listenbrainz_reg(uid, user): def listenbrainz_unreg(uid, user): lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user) lbz.unlink_account() - flash("Unlinked ListenBrainz account") + flash("Unlinked ListenBrainz account", "success") return redirect(url_for("frontend.user_profile", uid=uid)) @@ -335,10 +341,10 @@ def login(): name, password = map(request.form.get, ("user", "password")) error = False if not name: - flash("Missing user name") + flash("Missing user name", "danger") error = True if not password: - flash("Missing password") + flash("Missing password", "danger") error = True if not error: @@ -346,13 +352,13 @@ def login(): if user: logger.info("Logged user %s (IP: %s)", name, request.remote_addr) session["userid"] = str(user.id) - flash("Logged in!") + flash("Logged in!", "success") return redirect(return_url) else: logger.error( "Failed login attempt for user %s (IP: %s)", name, request.remote_addr ) - flash("Wrong username or password") + flash("Wrong username or password", "danger") return render_template("login.html") @@ -360,5 +366,5 @@ def login(): @frontend.route("/user/logout") def logout(): session.clear() - flash("Logged out!") + flash("Logged out!", "success") return redirect(url_for("frontend.login")) diff --git a/supysonic/templates/layout.html b/supysonic/templates/layout.html index 3d2a7b0..35c965e 100644 --- a/supysonic/templates/layout.html +++ b/supysonic/templates/layout.html @@ -97,14 +97,16 @@
- {% if get_flashed_messages() %} -