From: Chris Fulljames Date: Wed, 26 Feb 2025 01:30:40 +0000 (-0500) Subject: Add Random page X-Git-Url: https://littlesong.place/gitweb/gitweb.cgi?a=commitdiff_plain;h=aa6fba5cef76b28b73f13fcb406f0962503bf315;p=littlesongplace.git Add Random page --- diff --git a/main.py b/main.py index b07eb0e..2007410 100644 --- a/main.py +++ b/main.py @@ -567,7 +567,7 @@ def songs(): elif user: songs = Song.get_all_for_username(user) else: - songs = [] + songs = Song.get_random(50) return render_template("songs-by-tag.html", user=user, tag=tag, songs=songs, **colors) @@ -1088,6 +1088,12 @@ class Song: def get_latest(cls, count): return cls._from_db("select * from songs inner join users on songs.userid = users.userid order by songs.created desc limit ?", [count]) + @classmethod + def get_random(cls, count): + songs = cls._from_db("select * from songs inner join users on songs.userid = users.userid where songid in (select songid from songs order by random() limit ?)", [count]) + random.shuffle(songs) + return songs + @classmethod def get_for_playlist(cls, playlistid): return cls._from_db("""\ diff --git a/templates/base.html b/templates/base.html index 18cfc93..667651a 100644 --- a/templates/base.html +++ b/templates/base.html @@ -27,6 +27,7 @@