]> littlesong.place Git - littlesongplace.git/commitdiff
Allow filtering by user and tag together
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 12 Jan 2025 17:25:18 +0000 (12:25 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 12 Jan 2025 17:25:18 +0000 (12:25 -0500)
main.py
static/styles.css
templates/song-list.html
templates/songs-by-tag.html

diff --git a/main.py b/main.py
index 1aeea789f216a26a1d0137ba4afbe872d11817bd..7783812bbc3545d177336cb8bc19455343061f05 100644 (file)
--- 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/<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=()):
index 818a1ab8529e22c328fbe7160cd52ead3c83a010..27a6a42a7043101be0a737985b1ce441da2becfc 100644 (file)
@@ -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;
index 728ffc07fd50d483b42972301de14de08b8e6ba9..1f9bd195e8353ec8e254205e511c1c275ea11694 100644 (file)
@@ -51,7 +51,7 @@
             <div class="song-tags">
                 Tags:
                 {% for tag in song.tags %}
-                <a href="/songs-by-tag/{{ tag }}">{{ tag }}</a>
+                <a href="/songs?user={{ song.username }}&tag={{ tag }}">{{ tag }}</a>
                 {% endfor %}
             </div>
         </div>
index 341de24e4184ad8b9194a39c9110312c3f2a4b0a..a8e3dbac7d0a7666f269be2dbd559632f710c3c7 100644 (file)
@@ -1,10 +1,22 @@
 {% extends "base.html" %}
 
-{% block title %}Songs with Tag: {{ tag }}{% endblock %}
+{% block title %}Songs{% endblock %}
 
 {% block body %}
 
-<h1 class="tag-name">Songs tagged with '{{ tag }}'</h1>
+<h1>Songs</h1>
+
+{% if user %}
+    <div class="filter">
+        By: {{ user }} {% if tag %}<a href="/songs?tag={{ tag }}" class="filter-remove">(remove filter)</a>{% endif %}
+    </div>
+{% endif %}
+
+{% if tag %}
+    <div class="filter">
+        With tag: {{ tag }} {% if user %}<a href="/songs?user={{ user }}" class="filter-remove">(remove filter)</a>{% endif %}
+    </div>
+{% endif %}
 
 {{ song_list|safe }}