1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-22 08:56:17 +00:00

Categorize messages for better alerts

This commit is contained in:
Óscar García Amor 2024-12-02 13:17:01 +01:00
parent 3911da475a
commit d71a2e04fd
No known key found for this signature in database
GPG Key ID: E18B2370D3D566EE
4 changed files with 64 additions and 56 deletions

View File

@ -46,10 +46,10 @@ def add_folder_post():
error = False error = False
name, path = map(request.form.get, ("name", "path")) name, path = map(request.form.get, ("name", "path"))
if name in (None, ""): if name in (None, ""):
flash("The name is required.") flash("The name is required.", "danger")
error = True error = True
if path in (None, ""): if path in (None, ""):
flash("The path is required.") flash("The path is required.", "danger")
error = True error = True
if error: if error:
return render_template("addfolder.html") return render_template("addfolder.html")
@ -57,10 +57,10 @@ def add_folder_post():
try: try:
FolderManager.add(name, path) FolderManager.add(name, path)
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return render_template("addfolder.html") 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")) return redirect(url_for("frontend.folder_index"))
@ -69,11 +69,11 @@ def add_folder_post():
def del_folder(id): def del_folder(id):
try: try:
FolderManager.delete(id) FolderManager.delete(id)
flash("Deleted folder") flash("Deleted folder", "success")
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
except Folder.DoesNotExist: except Folder.DoesNotExist:
flash("No such folder", "error") flash("No such folder", "danger")
return redirect(url_for("frontend.folder_index")) return redirect(url_for("frontend.folder_index"))
@ -90,10 +90,10 @@ def scan_folder(id=None):
DaemonClient(current_app.config["DAEMON"]["socket"]).scan(folders) DaemonClient(current_app.config["DAEMON"]["socket"]).scan(folders)
flash("Scanning started") flash("Scanning started")
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
except Folder.DoesNotExist: except Folder.DoesNotExist:
flash("No such folder", "error") flash("No such folder", "danger")
except DaemonUnavailableError: except DaemonUnavailableError:
flash("Can't start scan", "error") flash("Can't start scan", "danger")
return redirect(url_for("frontend.folder_index")) return redirect(url_for("frontend.folder_index"))

View File

@ -30,13 +30,13 @@ def resolve_and_inject_playlist(func):
try: try:
uid = uuid.UUID(uid) uid = uuid.UUID(uid)
except ValueError: except ValueError:
flash("Invalid playlist id") flash("Invalid playlist id", "warning")
return redirect(url_for("frontend.playlist_index")) return redirect(url_for("frontend.playlist_index"))
try: try:
playlist = Playlist[uid] playlist = Playlist[uid]
except Playlist.DoesNotExist: except Playlist.DoesNotExist:
flash("Unknown playlist") flash("Unknown playlist", "warning")
return redirect(url_for("frontend.playlist_index")) return redirect(url_for("frontend.playlist_index"))
return func(uid, playlist) return func(uid, playlist)
@ -64,9 +64,9 @@ def playlist_export(uid, playlist):
@resolve_and_inject_playlist @resolve_and_inject_playlist
def playlist_update(uid, playlist): def playlist_update(uid, playlist):
if playlist.user_id != request.user.id: 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"): elif not request.form.get("name"):
flash("Missing playlist name") flash("Missing playlist name", "danger")
else: else:
playlist.name = request.form.get("name") playlist.name = request.form.get("name")
playlist.public = request.form.get("public") in ( playlist.public = request.form.get("public") in (
@ -78,7 +78,7 @@ def playlist_update(uid, playlist):
"checked", "checked",
) )
playlist.save() playlist.save()
flash("Playlist updated.") flash("Playlist updated.", "success")
return playlist_details(str(uid)) return playlist_details(str(uid))
@ -87,9 +87,9 @@ def playlist_update(uid, playlist):
@resolve_and_inject_playlist @resolve_and_inject_playlist
def playlist_delete(uid, playlist): def playlist_delete(uid, playlist):
if playlist.user_id != request.user.id: 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: else:
playlist.delete_instance() playlist.delete_instance()
flash("Playlist deleted") flash("Playlist deleted", "success")
return redirect(url_for("frontend.playlist_index")) return redirect(url_for("frontend.playlist_index"))

View File

@ -37,10 +37,10 @@ def me_or_uuid(f, arg="uid"):
try: try:
user = UserManager.get(uid) user = UserManager.get(uid)
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
except User.DoesNotExist: except User.DoesNotExist:
flash("No such user", "error") flash("No such user", "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
if kwargs: if kwargs:
@ -111,7 +111,7 @@ def update_clients(uid, user):
) )
prefs.save() prefs.save()
flash("Clients preferences updated.") flash("Clients preferences updated.", "success")
return user_profile(uid, user) return user_profile(uid, user)
@ -121,10 +121,10 @@ def change_username_form(uid):
try: try:
user = UserManager.get(uid) user = UserManager.get(uid)
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
except User.DoesNotExist: except User.DoesNotExist:
flash("No such user", "error") flash("No such user", "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
return render_template("change_username.html", user=user) return render_template("change_username.html", user=user)
@ -136,20 +136,20 @@ def change_username_post(uid):
try: try:
user = UserManager.get(uid) user = UserManager.get(uid)
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
except User.DoesNotExist: except User.DoesNotExist:
flash("No such user", "error") flash("No such user", "danger")
return redirect(url_for("frontend.index")) return redirect(url_for("frontend.index"))
username = request.form.get("user") username = request.form.get("user")
if username in ("", None): if username in ("", None):
flash("The username is required") flash("The username is required", "danger")
return render_template("change_username.html", user=user) return render_template("change_username.html", user=user)
if user.name != username: if user.name != username:
try: try:
User.get(name=username) 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) return render_template("change_username.html", user=user)
except User.DoesNotExist: except User.DoesNotExist:
pass pass
@ -163,7 +163,7 @@ def change_username_post(uid):
user.name = username user.name = username
user.admin = admin user.admin = admin
user.save() user.save()
flash(f"User '{username}' updated.") flash(f"User '{username}' updated.", "success")
else: else:
flash(f"No changes for '{username}'.") flash(f"No changes for '{username}'.")
@ -198,16 +198,16 @@ def change_password_post(uid, user):
if user.id == request.user.id: if user.id == request.user.id:
current = request.form.get("current") current = request.form.get("current")
if not current: if not current:
flash("The current password is required") flash("The current password is required", "danger")
error = True error = True
new, confirm = map(request.form.get, ("new", "confirm")) new, confirm = map(request.form.get, ("new", "confirm"))
if not new: if not new:
flash("The new password is required") flash("The new password is required", "danger")
error = True error = True
if new != confirm: 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 error = True
if not error: if not error:
@ -217,10 +217,10 @@ def change_password_post(uid, user):
else: else:
UserManager.change_password2(user.name, new) UserManager.change_password2(user.name, new)
flash("Password changed") flash("Password changed", "success")
return redirect(url_for("frontend.user_profile", uid=uid)) return redirect(url_for("frontend.user_profile", uid=uid))
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return change_password_form(uid, user) return change_password_form(uid, user)
@ -240,22 +240,22 @@ def add_user_post():
args.pop, ("user", "passwd", "passwd_confirm"), (None,) * 3 args.pop, ("user", "passwd", "passwd_confirm"), (None,) * 3
) )
if not name: if not name:
flash("The name is required.") flash("The name is required.", "danger")
error = True error = True
if not passwd: if not passwd:
flash("Please provide a password.") flash("Please provide a password.", "danger")
error = True error = True
elif passwd != passwd_confirm: elif passwd != passwd_confirm:
flash("The passwords don't match.") flash("The passwords don't match.", "danger")
error = True error = True
if not error: if not error:
try: try:
UserManager.add(name, passwd, **args) 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")) return redirect(url_for("frontend.user_index"))
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
return add_user_form() return add_user_form()
@ -265,11 +265,11 @@ def add_user_post():
def del_user(uid): def del_user(uid):
try: try:
UserManager.delete(uid) UserManager.delete(uid)
flash("Deleted user") flash("Deleted user", "success")
except ValueError as e: except ValueError as e:
flash(str(e), "error") flash(str(e), "danger")
except User.DoesNotExist: except User.DoesNotExist:
flash("No such user", "error") flash("No such user", "danger")
return redirect(url_for("frontend.user_index")) return redirect(url_for("frontend.user_index"))
@ -279,12 +279,15 @@ def del_user(uid):
def lastfm_reg(uid, user): def lastfm_reg(uid, user):
token = request.args.get("token") token = request.args.get("token")
if not token: if not token:
flash("Missing LastFM auth token") flash("Missing LastFM auth token", "warning")
return redirect(url_for("frontend.user_profile", uid=uid)) return redirect(url_for("frontend.user_profile", uid=uid))
lfm = LastFm(current_app.config["LASTFM"], user) lfm = LastFm(current_app.config["LASTFM"], user)
status, error = lfm.link_account(token) 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)) return redirect(url_for("frontend.user_profile", uid=uid))
@ -294,7 +297,7 @@ def lastfm_reg(uid, user):
def lastfm_unreg(uid, user): def lastfm_unreg(uid, user):
lfm = LastFm(current_app.config["LASTFM"], user) lfm = LastFm(current_app.config["LASTFM"], user)
lfm.unlink_account() lfm.unlink_account()
flash("Unlinked LastFM account") flash("Unlinked LastFM account", "success")
return redirect(url_for("frontend.user_profile", uid=uid)) return redirect(url_for("frontend.user_profile", uid=uid))
@ -303,12 +306,15 @@ def lastfm_unreg(uid, user):
def listenbrainz_reg(uid, user): def listenbrainz_reg(uid, user):
token = request.args.get("token") token = request.args.get("token")
if not token: if not token:
flash("Missing ListenBrainz auth token") flash("Missing ListenBrainz auth token", "warning")
return redirect(url_for("frontend.user_profile", uid=uid)) return redirect(url_for("frontend.user_profile", uid=uid))
lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user) lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user)
status, error = lbz.link_account(token) 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)) return redirect(url_for("frontend.user_profile", uid=uid))
@ -318,7 +324,7 @@ def listenbrainz_reg(uid, user):
def listenbrainz_unreg(uid, user): def listenbrainz_unreg(uid, user):
lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user) lbz = ListenBrainz(current_app.config["LISTENBRAINZ"], user)
lbz.unlink_account() lbz.unlink_account()
flash("Unlinked ListenBrainz account") flash("Unlinked ListenBrainz account", "success")
return redirect(url_for("frontend.user_profile", uid=uid)) return redirect(url_for("frontend.user_profile", uid=uid))
@ -335,10 +341,10 @@ def login():
name, password = map(request.form.get, ("user", "password")) name, password = map(request.form.get, ("user", "password"))
error = False error = False
if not name: if not name:
flash("Missing user name") flash("Missing user name", "danger")
error = True error = True
if not password: if not password:
flash("Missing password") flash("Missing password", "danger")
error = True error = True
if not error: if not error:
@ -346,13 +352,13 @@ def login():
if user: if user:
logger.info("Logged user %s (IP: %s)", name, request.remote_addr) logger.info("Logged user %s (IP: %s)", name, request.remote_addr)
session["userid"] = str(user.id) session["userid"] = str(user.id)
flash("Logged in!") flash("Logged in!", "success")
return redirect(return_url) return redirect(return_url)
else: else:
logger.error( logger.error(
"Failed login attempt for user %s (IP: %s)", name, request.remote_addr "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") return render_template("login.html")
@ -360,5 +366,5 @@ def login():
@frontend.route("/user/logout") @frontend.route("/user/logout")
def logout(): def logout():
session.clear() session.clear()
flash("Logged out!") flash("Logged out!", "success")
return redirect(url_for("frontend.login")) return redirect(url_for("frontend.login"))

View File

@ -97,14 +97,16 @@
</nav> </nav>
<div class="container"> <div class="container">
{% if get_flashed_messages() %} {% with messages = get_flashed_messages(with_categories=true) %}
<div class="alert alert-info alert-dismissible fade show" role="alert"> {% if messages %}
{% for message in get_flashed_messages() %} {% for category, message in messages %}
{{ message }}<br /> <div class="alert alert-{% if category == "message" %}info{% else %}{{ category }}{% endif %} alert-dismissible fade show" role="alert">
{% endfor %} {{ message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div> </div>
{% endfor %}
{% endif %} {% endif %}
{% endwith %}
{% block body %}{% endblock %} {% block body %}{% endblock %}