From: Chris Fulljames Date: Sat, 15 Feb 2025 19:07:22 +0000 (-0500) Subject: Add playlist editor X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=e03e30f5d090b3f2d66783edf985ee2f8ff1094b;p=littlesongplace.git Add playlist editor --- diff --git a/main.py b/main.py index dad5977..aee0141 100644 --- a/main.py +++ b/main.py @@ -179,7 +179,7 @@ def users_profile(profile_username): user_accolor=profile_data["accolor"], playlists=plist_data, songs=songs, - song_list=render_template("song-list.html", songs=songs)) + song_list=render_template("song-list.html", songs=songs, is_profile_song_list=True)) @app.post("/edit-profile") def edit_profile(): @@ -727,13 +727,13 @@ def create_playlist(): flash_and_log(f"Created playlist {name}", "success") return redirect(request.referrer) -@app.post("/delete-playlist/") +@app.get("/delete-playlist/") def delete_playlist(playlistid): if not "userid" in session: abort(401) # Make sure playlist exists - plist_data = query_db("select * from playlists where playlistid = ?", args=[playlistid]) + plist_data = query_db("select * from playlists where playlistid = ?", args=[playlistid], one=True) if not plist_data: abort(404) @@ -746,7 +746,7 @@ def delete_playlist(playlistid): get_db().commit() flash_and_log(f"Deleted playlist {plist_data['name']}", "success") - return redirect(request.referrer) + return redirect(f"/users/{session['username']}") @app.post("/append-to-playlist") def append_to_playlist(): @@ -770,7 +770,7 @@ def append_to_playlist(): songid = request.form["songid"] # Make sure song exists - song_data = query_db("select * from songs where songid = ?", args=[songid]) + song_data = query_db("select * from songs where songid = ?", args=[songid], one=True) if not song_data: abort(404) @@ -786,7 +786,9 @@ def append_to_playlist(): query_db("update playlists set updated = ? where playlistid = ?", args=[timestamp, playlistid]) get_db().commit() - return {"status": "ok"} + flash_and_log(f"Added '{song_data['title']}' to {plist_data['name']}", "success") + + return redirect(request.referrer) @app.post("/edit-playlist/") def edit_playlist_post(playlistid): @@ -794,7 +796,7 @@ def edit_playlist_post(playlistid): abort(401) # Make sure playlist exists - plist_data = query_db("select * from playlists where playlistid = ?", args=[playlistid]) + plist_data = query_db("select * from playlists where playlistid = ?", args=[playlistid], one=True) if not plist_data: abort(404) @@ -802,32 +804,30 @@ def edit_playlist_post(playlistid): if session["userid"] != plist_data["userid"]: abort(401) - private = request.form["type"] == "private" - # Make sure all songs are valid - songids = [] - for field, value in request.form.items(): - if field.startswith("position-"): - try: - position = int(field[len("position-"):]) - songid = int(value) - except ValueError: - abort(400) - - song_data = query_db("select * from songs where songid = ?", args=[songid]) - if not song_data: - abort(400) - - # Song is valid, add to list - songids.append((position, songid)) + try: + songids = [int(s) for s in request.form["songids"].split(",")] + except ValueError: + # Invalid songid(s) + abort(400) + + for songid in songids: + song_data = query_db("select * from songs where songid = ?", args=[songid]) + if not song_data: + abort(400) # All songs valid - delete old songs query_db("delete from playlist_songs where playlistid = ?", args=[playlistid]) # Re-add songs with new positions - for position, songid in songids: + for position, songid in enumerate(songids): query_db("insert into playlist_songs (playlistid, position, songid) values (?, ?, ?)", args=[playlistid, position, songid]) + # Update private, name + private = int(request.form["type"] == "private") + name = request.form["name"] + query_db("update playlists set private = ?, name = ? where playlistid = ?", [private, name, playlistid]) + get_db().commit() flash_and_log("Playlist updated", "success") @@ -855,7 +855,10 @@ def playlists(playlistid): return render_template( "playlist.html", name=plist_data["name"], + playlistid=plist_data["playlistid"], + userid=plist_data["userid"], username=plist_data["username"], + songs=songs, song_list=render_template("song-list.html", songs=songs)) def flash_and_log(msg, category=None): @@ -922,6 +925,7 @@ def get_db(): db = getattr(g, '_database', None) if db is None: db = g._database = sqlite3.connect(DATA_DIR / "database.db") + db.cursor().execute("PRAGMA foreign_keys = ON") # Get current version user_version = query_db("pragma user_version", one=True)[0] diff --git a/schema_update.sql b/schema_update.sql index 44b610c..3d065f2 100644 --- a/schema_update.sql +++ b/schema_update.sql @@ -6,7 +6,7 @@ CREATE TABLE playlists ( name TEXT NOT NULL, private INTEGER NOT NULL, - FOREIGN KEY(userid) REFERENCES users(userid) + FOREIGN KEY(userid) REFERENCES users(userid) ON DELETE CASCADE ); CREATE INDEX playlists_by_userid ON playlists(userid); @@ -16,8 +16,8 @@ CREATE TABLE playlist_songs ( songid INTEGER NOT NULL, PRIMARY KEY(playlistid, position), - FOREIGN KEY(playlistid) REFERENCES playlists(playlistid), - FOREIGN KEY(songid) REFERENCES songs(songid) + FOREIGN KEY(playlistid) REFERENCES playlists(playlistid) ON DELETE CASCADE, + FOREIGN KEY(songid) REFERENCES songs(songid) ON DELETE CASCADE ); CREATE INDEX playlist_songs_by_playlist ON playlist_songs(playlistid); diff --git a/static/styles.css b/static/styles.css index 130bc63..54aeb80 100644 --- a/static/styles.css +++ b/static/styles.css @@ -65,7 +65,15 @@ input[type=text], input[type=password], input[type=url] { border: 0px; border-radius: 5px; padding: 8px; - margin: 10px; +} + +select { + border: none; + background-color: var(--purple); + color: var(--yellow); + border-radius: 5px; + font-size: 16px; + padding: 8px; } div.main { @@ -197,6 +205,10 @@ input[type=file] { border: solid 2px var(--purple); } +.profile-bio { + margin: 10px 0px; +} + .profile-action { margin-bottom: 20px; } @@ -209,6 +221,24 @@ input[type=file] { margin: 10px; } +.playlist-list-entry { + box-shadow: 0px 0px 5px 0px; + border-radius: 10px; + padding: 10px; + margin: 10px 0px; +} + +.draggable-song { + box-shadow: 0px 0px 5px 0px; + border-radius: 10px; + padding: 5px 10px; + margin: 10px 0px; + display: flex; + flex-direction: row; + align-items: center; + gap: 10px; +} + /* Coloris Color Picker */ .clr-field button { diff --git a/templates/playlist.html b/templates/playlist.html index e298333..f2f622a 100644 --- a/templates/playlist.html +++ b/templates/playlist.html @@ -2,12 +2,137 @@ {% block title %}{{ name }}{% endblock %} -{% block body %} +{% block body -%}

{{ name }}

Playlist by {{ username }}

+{% if session["userid"] == userid -%} +

+ + +

+ + + +{%- endif %} + {{ song_list|safe }} -{% endblock %} +{% if session["userid"] == userid -%} + + +{%- endif %} + +{%- endblock %} diff --git a/templates/profile.html b/templates/profile.html index 37f6ba0..f9ae7f5 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -145,7 +145,7 @@
Cancel - +