From: Chris Fulljames Date: Sat, 25 Jan 2025 19:36:52 +0000 (-0500) Subject: Add direct song links for easier sharing X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=f890276a80ad06aee5ca0560e9c246e6e43299fc;p=littlesongplace.git Add direct song links for easier sharing --- diff --git a/main.py b/main.py index 0f0b6cf..3efd180 100644 --- a/main.py +++ b/main.py @@ -427,17 +427,21 @@ def delete_song(songid): return redirect(request.referrer) -@app.get("/song//") +@app.get("/song//") def song(userid, songid): - try: - # Make sure values are valid integers - int(userid) - int(songid) - except ValueError: - app.logger.warning(f"Invalid song request: user: {userid}, song: {songid}") - abort(404) + if request.args.get("action", None) == "view": + try: + song = Song.by_id(songid) + if song.userid != userid: + abort(404) - return send_from_directory(DATA_DIR / "songs" / userid, songid + ".mp3") + return render_template( + "song.html", + song_list=render_template("song-list.html", songs=[song]), song=song) + except ValueError: + abort(404) + else: + return send_from_directory(DATA_DIR / "songs" / str(userid), str(songid) + ".mp3") @app.get("/songs") def songs(): diff --git a/templates/base.html b/templates/base.html index 79e3492..6bc47d9 100644 --- a/templates/base.html +++ b/templates/base.html @@ -5,6 +5,9 @@ + + {% block head %} + {% endblock %} diff --git a/templates/song-list.html b/templates/song-list.html index a6457a4..4cb9df9 100644 --- a/templates/song-list.html +++ b/templates/song-list.html @@ -4,7 +4,7 @@
-
{{ song.title }}
+ - diff --git a/templates/song.html b/templates/song.html new file mode 100644 index 0000000..d993b91 --- /dev/null +++ b/templates/song.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% block head %} + + +{% endblock %} + +{% block title %}{{ song.title }}{% endblock %} + +{% block body %} + +{{ song_list|safe }} + +{% endblock %} diff --git a/test/test_offline.py b/test/test_offline.py index 6af9202..728df53 100644 --- a/test/test_offline.py +++ b/test/test_offline.py @@ -500,3 +500,14 @@ def test_songs_by_user(client): assert songs[1]["username"] == "user1" # Song 3 not shown, by different user + +def test_single_song(client): + _create_user(client, "user1", "password", login=True) + _test_upload_song(client, b"Success", user="user1", title="song1", tags="tag") + + songs = _get_song_list_from_page(client, "/song/1/1?action=view") + + assert len(songs) == 1 + assert songs[0]["title"] == "song1" + assert songs[0]["username"] == "user1" +