From aa6fba5cef76b28b73f13fcb406f0962503bf315 Mon Sep 17 00:00:00 2001 From: Chris Fulljames Date: Tue, 25 Feb 2025 20:30:40 -0500 Subject: [PATCH] Add Random page --- main.py | 8 +++++++- templates/base.html | 1 + todo.txt | 2 -- 3 files changed, 8 insertions(+), 3 deletions(-) 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 @@