]> littlesong.place Git - littlesongplace.git/commitdiff
Add direct song links for easier sharing
authorChris Fulljames <christianfulljames@gmail.com>
Sat, 25 Jan 2025 19:36:52 +0000 (14:36 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sat, 25 Jan 2025 19:36:52 +0000 (14:36 -0500)
main.py
templates/base.html
templates/song-list.html
templates/song.html [new file with mode: 0644]
test/test_offline.py

diff --git a/main.py b/main.py
index 0f0b6cfdd46930b13dfe1a0ee6e25ce7133c6497..3efd180ce9d72adf4f2d9f05a1a18e0efc20623c 100644 (file)
--- a/main.py
+++ b/main.py
@@ -427,17 +427,21 @@ def delete_song(songid):
 
     return redirect(request.referrer)
 
-@app.get("/song/<userid>/<songid>")
+@app.get("/song/<int:userid>/<int:songid>")
 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():
index 79e3492e9c1e630b5a868dbeadb4ee646ac8f353..6bc47d9f5d7e2d2cc2dd63825e74d7e87e7dfe32 100644 (file)
@@ -5,6 +5,9 @@
         <link rel="stylesheet" href="/static/styles.css">
         <script src="/static/player.js"></script>
         <meta name="viewport" content="width=device-width, initial-scale=1">
+        <!-- Page-specific head fields -->
+        {% block head %}
+        {% endblock %}
     </head>
     <body>
 
index a6457a4f5ad839f70afe9b5f04065590d3c0e581..4cb9df929477328d245d418f5b146d09e261e309 100644 (file)
@@ -4,7 +4,7 @@
         <div class="song-main">
             <div class="song-info">
                 <!-- Song Title -->
-                <div class="song-title">{{ song.title }}</div>
+                <div class="song-title"><a href="/song/{{ song.userid }}/{{ song.songid }}?action=view">{{ song.title }}</a></div>
 
                 <!-- Separator -->
                 -
diff --git a/templates/song.html b/templates/song.html
new file mode 100644 (file)
index 0000000..d993b91
--- /dev/null
@@ -0,0 +1,14 @@
+{% extends "base.html" %}
+
+{% block head %}
+<meta property="og:title" content="{{ song.title }}" />
+<meta property="og:description" content="Song by {{ song.username }}" />
+{% endblock %}
+
+{% block title %}{{ song.title }}{% endblock %}
+
+{% block body %}
+
+{{ song_list|safe }}
+
+{% endblock %}
index 6af920268bb92a78a4739867a1dd3ba1b2fa3efb..728df53a38b4f4a9af4b1e7eae959a02e5578d39 100644 (file)
@@ -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"
+