From: Chris Fulljames Date: Sun, 12 Jan 2025 17:25:18 +0000 (-0500) Subject: Allow filtering by user and tag together X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=c0144ccffba24be842dbe3ca2eeab96acdd86bf3;p=littlesongplace.git Allow filtering by user and tag together --- diff --git a/main.py b/main.py index 1aeea78..7783812 100644 --- a/main.py +++ b/main.py @@ -117,7 +117,7 @@ def users_profile(profile_username): # Get songs for current profile profile_userid = profile_data["userid"] - songs = Song.get_all_for_user(profile_userid) + songs = Song.get_all_for_userid(profile_userid) # Sanitize bio profile_bio = "" @@ -390,12 +390,23 @@ def song(userid, songid): return send_from_directory(DATA_DIR / "songs" / userid, songid + ".mp3") -@app.get("/songs-by-tag/") -def songs_by_tag(tag): - songs = Song.get_all_for_tag(tag) +@app.get("/songs") +def songs(): + tag = request.args.get("tag", None) + user = request.args.get("user", None) + + if tag and user: + songs = Song.get_all_for_username_and_tag(user, tag) + elif tag: + songs = Song.get_all_for_tag(tag) + elif user: + songs = Song.get_all_for_username(user) + else: + songs = [] return render_template( "songs-by-tag.html", + user=user, tag=tag, song_list=render_template("song-list.html", songs=songs)) @@ -465,12 +476,20 @@ class Song: return songs[0] @classmethod - def get_all_for_user(cls, userid): + def get_all_for_userid(cls, userid): return cls._from_db("select * from songs inner join users on songs.userid = users.userid where songs.userid = ? order by songs.created desc", [userid]) + @classmethod + def get_all_for_username(cls, username): + return cls._from_db("select * from songs inner join users on songs.userid = users.userid where users.username = ? order by songs.created desc", [username]) + + @classmethod + def get_all_for_username_and_tag(cls, username, tag): + return cls._from_db(f"select * from song_tags inner join songs on song_tags.songid = songs.songid inner join users on songs.userid = users.userid where (username = ? and tag = ?)", [username, tag]) + @classmethod def get_all_for_tag(cls, tag): - return cls._from_db("select * from song_tags inner join songs on song_tags.songid = songs.songid inner join users on songs.userid = users.userid where tag = ?", [tag]) + return cls._from_db(f"select * from song_tags inner join songs on song_tags.songid = songs.songid inner join users on songs.userid = users.userid where (tag = ?)", [tag]) @classmethod def _from_db(cls, query, args=()): diff --git a/static/styles.css b/static/styles.css index 818a1ab..27a6a42 100644 --- a/static/styles.css +++ b/static/styles.css @@ -8,6 +8,14 @@ div.navbar { gap: 10px; } +/* Filters on /songs page */ +.filter { + margin: 5px; +} +.filter-remove { + font-size: 12px; +} + /* Song Entry in Song List */ div.song-list { display: flex; diff --git a/templates/song-list.html b/templates/song-list.html index 728ffc0..1f9bd19 100644 --- a/templates/song-list.html +++ b/templates/song-list.html @@ -51,7 +51,7 @@
Tags: {% for tag in song.tags %} - {{ tag }} + {{ tag }} {% endfor %}
diff --git a/templates/songs-by-tag.html b/templates/songs-by-tag.html index 341de24..a8e3dba 100644 --- a/templates/songs-by-tag.html +++ b/templates/songs-by-tag.html @@ -1,10 +1,22 @@ {% extends "base.html" %} -{% block title %}Songs with Tag: {{ tag }}{% endblock %} +{% block title %}Songs{% endblock %} {% block body %} -

Songs tagged with '{{ tag }}'

+

Songs

+ +{% if user %} +
+ By: {{ user }} {% if tag %}(remove filter){% endif %} +
+{% endif %} + +{% if tag %} +
+ With tag: {{ tag }} {% if user %}(remove filter){% endif %} +
+{% endif %} {{ song_list|safe }}