]> littlesong.place Git - littlesongplace.git/commitdiff
Hide jam songs before end date
authorChris Fulljames <christianfulljames@gmail.com>
Sun, 13 Apr 2025 13:10:55 +0000 (09:10 -0400)
committerChris Fulljames <christianfulljames@gmail.com>
Sun, 13 Apr 2025 13:10:55 +0000 (09:10 -0400)
src/littlesongplace/songs.py
src/littlesongplace/static/styles.css
src/littlesongplace/templates/playlist.html
src/littlesongplace/templates/profile.html
src/littlesongplace/templates/song-list.html
src/littlesongplace/templates/song-macros.html
test/test_jams.py

index 107dbcdf67b46a235f6385abfed7c2c9caafce6d..3478460ade3e876ca427869e56fd2c82bb66b051 100644 (file)
@@ -30,6 +30,7 @@ class Song:
     tags: list[str]
     collaborators: list[str]
     user_has_pfp: bool
+    hidden: bool
 
     def json(self):
         return json.dumps(vars(self))
@@ -161,48 +162,50 @@ def get_for_event(eventid):
         """,
         [eventid])
 
+def _get_song_info_list(table, column, songid):
+    rows = db.query(
+            f"SELECT ({column}) FROM {table} WHERE songid = ?", [songid])
+    return [r[column] for r in rows if r[column]]
+
 def _from_db(query, args=()):
     songs_data = db.query(query, args)
-    tags, collabs = _get_info_for_songs(songs_data)
     songs = []
     for sd in songs_data:
-        song_tags = [t["tag"] for t in tags[sd["songid"]] if t["tag"]]
-        song_collabs = [c["name"] for c in collabs[sd["songid"]] if c["name"]]
-        created = datetime.fromisoformat(sd["created"]).astimezone().strftime("%Y-%m-%d")
-        has_pfp = users.user_has_pfp(sd["userid"])
+        songid = sd["songid"]
+        song_tags = _get_song_info_list("song_tags", "tag", songid)
+        song_collabs = _get_song_info_list("song_collaborators", "name", songid)
+
+        # Song is hidden if it was submitted to an event that hasn't ended yet
+        hidden = False
+        if sd["eventid"]:
+            event_row = db.query(
+                    "SELECT * FROM jam_events WHERE eventid = ?",
+                    [sd["eventid"]],
+                    one=True)
+            if event_row and event_row["enddate"]:
+                enddate = datetime.fromisoformat(event_row["enddate"]).astimezone()
+                hidden = datetime.now().astimezone() < enddate
+
+        created = (
+                datetime.fromisoformat(sd["created"])
+                .astimezone()
+                .strftime("%Y-%m-%d"))
+
         songs.append(Song(
-            sd["songid"],
-            sd["userid"],
-            sd["threadid"],
-            sd["username"],
-            sd["title"],
-            sanitize_user_text(sd["description"]),
-            created,
-            song_tags,
-            song_collabs,
-            has_pfp
+            songid=sd["songid"],
+            userid=sd["userid"],
+            threadid=sd["threadid"],
+            username=sd["username"],
+            title=sd["title"],
+            description=sanitize_user_text(sd["description"]),
+            created=created,
+            tags=song_tags,
+            collaborators=song_collabs,
+            user_has_pfp=users.user_has_pfp(sd["userid"]),
+            hidden=hidden,
         ))
     return songs
 
-def _get_info_for_songs(songs):
-    tags = {}
-    collabs = {}
-    for song in songs:
-        songid = song["songid"]
-        tags[songid] = db.query(
-            """
-            select (tag) from song_tags
-            where songid = ?
-            """,
-            [songid])
-        collabs[songid] = db.query(
-            """
-            select (name) from song_collaborators
-            where songid = ?
-            """,
-            [songid])
-    return tags, collabs
-
 @bp.get("/edit-song")
 def edit_song():
     if not "userid" in session:
index 9be28283e7bc216511cca05412d7be707b82da30..74c3b10e1d8d61a6a59418ad83865ab0a1020266 100644 (file)
@@ -263,7 +263,7 @@ input[type=file] {
     margin: 10px 0px;
 }
 
-.playlist-type {
+.visibility-indicator {
     opacity: 50%;
 }
 
index f7dff237f20ed29c28dc387bc1efc9ab62506421..f7a8a60c8d00eaba0d9d39c364f395b2a9a00ab2 100644 (file)
@@ -15,7 +15,7 @@
 <p>
 Playlist by <a href="/users/{{ username }}" class="profile-link">{{ username }}</a>
 {% if session["userid"] == userid -%}
-<span class="playlist-type">
+<span class="visibility-indicator">
 [{% if private %}Private{% else %}Public{% endif %}]
 </span>
 {%- endif %}
index 43747f935744d5ec4b87892d8c04e7f7ee0495db..5240e8949d17c062e407a23a3de7f16cade9f8c2 100644 (file)
             <a class="playlist-name" href="/playlists/{{ plist['playlistid'] }}">{{ plist['name'] }}</a>
             <!-- Only show playlist type to owner, everyone else only sees public playlists -->
             {% if session["userid"] == userid -%}
-            <span class="playlist-type">
+            <span class="visibility-indicator">
             [{% if plist['private'] %}Private{% else %}Public{% endif %}]
             </span>
             {%- endif %}
index 19784a4ea28760c0c691d3951fa4bd963f5375e5..20831c1c60b1feb4357c283fcd1e577938c2a5d5 100644 (file)
@@ -9,6 +9,7 @@
 
     <div class="song-list-songs">
         {% for song in songs %}
+        {%- if not (song.hidden and g.uiserid != song.userid) -%}
         <div class="song" data-song="{{ song.json() }}">
             <div class="song-main">
                 <div class="song-list-pfp-container">
@@ -34,6 +35,7 @@
             </div>
             {{ song_details(song, current_user_playlists) | indent(8) }}
         </div>
+        {%- endif -%}
         {% endfor %}
     </div>
 </div>
index 97114401aec657a804b966151fcd5878a04de52a..ad20a82fedda4c097000578afc4c6548074a8801 100644 (file)
 {% macro song_info(song) %}
 <div class="song-info">
     <!-- Song Title -->
-    <div class="song-title"><a href="/song/{{ song.userid }}/{{ song.songid }}?action=view">{{ song.title }}</a></div>
+    <div class="song-title"><a href="/song/{{ song.userid }}/{{ song.songid }}?action=view">{{ song.title }}</a>
+    {%- if song.hidden %}<span class="visibility-indicator">[Hidden]</span>{% endif -%}
+    </div>
 
     <!-- Separator -->
-    <div class="song-info-sep">
-        -
-    </div>
+    <div class="song-info-sep"> - </div>
+
 
     <!-- Song Artist(s) -->
     {{ song_artist(song) | indent(4) }}
index 855186ecc0e300dda6fb6eba31bfbeff4f2b2b2d..e2191159667250e4133d5c0dc3ea7162c005649f 100644 (file)
@@ -4,6 +4,11 @@ import pytest
 
 from .utils import create_user, upload_song
 
+# Shared timestamps
+today = datetime.now(timezone.utc)
+yesterday = (today - timedelta(days=1)).isoformat()
+tomorrow = (today + timedelta(days=1)).isoformat()
+
 @pytest.fixture
 def user(client):
     create_user(client, "user", login=True)
@@ -218,10 +223,6 @@ def _assert_appear_in_order(page, values):
         last_index = index
 
 def _create_past_present_future_events(client, jam):
-    today = datetime.now(timezone.utc)
-    yesterday = (today - timedelta(days=1)).isoformat()
-    tomorrow = (today + timedelta(days=1)).isoformat()
-
     _create_event(client, jam, "PastJam", yesterday, yesterday)
     _create_event(client, jam, "OngoingJam", yesterday, tomorrow)
     _create_event(client, jam, "UpcomingJam", tomorrow, tomorrow)
@@ -274,7 +275,30 @@ def test_jam_events_sorted_on_jam_info_page(client, user, jam):
 # Song Submissions #############################################################
 
 def test_submit_song_to_event(client, user, jam, event):
+    # Song always visible to owner
+    upload_song(client, b"Success", eventid=event)
+    response = client.get(f"/jams/{jam}/events/{event}")
+    assert b"song title" in response.data
+
+def test_submitted_song_hidden_before_enddate(client, user, jam, event):
+    response = client.post(
+            f"/jams/{jam}/events/{event}/update",
+            data=_get_event_data(enddate=tomorrow),
+            follow_redirects=True)
     upload_song(client, b"Success", eventid=event)
+    client.get("/logout")  # Log out to test public visibility
+
+    response = client.get(f"/jams/{jam}/events/{event}")
+    assert b"song title" not in response.data
+
+def test_submitted_song_visible_after_enddate(client, user, jam, event):
+    response = client.post(
+            f"/jams/{jam}/events/{event}/update",
+            data=_get_event_data(enddate=yesterday),
+            follow_redirects=True)
+    upload_song(client, b"Success", eventid=event)
+    client.get("/logout")  # Log out to test public visibility
+
     response = client.get(f"/jams/{jam}/events/{event}")
     assert b"song title" in response.data