From 3e73ce06504837cfddca1fa1a2e310ca0303a485 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 20 Oct 2023 11:31:22 -0400 Subject: [PATCH] db: Reload the New Tracks playlist at midnight The New Tracks playlist shows tracks that have been added within the past week. We should automatically reload it a few seconds after midnight to keep it up to date as tracks drop off the list. Implements: #58 ("Reload New Tracks playlist at midnight") Signed-off-by: Anna Schumaker --- emmental/db/playlists.py | 7 +++++++ tests/db/test_playlists.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/emmental/db/playlists.py b/emmental/db/playlists.py index 51fb699..aaa4477 100644 --- a/emmental/db/playlists.py +++ b/emmental/db/playlists.py @@ -1,7 +1,9 @@ # Copyright 2022 (c) Anna Schumaker """A custom Gio.ListModel for working with playlists.""" +import datetime import sqlite3 from gi.repository import GObject +from .. import alarm from . import playlist from . import tracks @@ -57,6 +59,11 @@ class Table(playlist.Table): def __init__(self, sql: GObject.TYPE_PYOBJECT, **kwargs): """Initialize the Playlists Table.""" super().__init__(sql=sql, system_tracks=False, **kwargs) + alarm.set_alarm(datetime.time(hour=0, minute=0, second=5), + self.__at_midnight) + + def __at_midnight(self) -> None: + self.new_tracks.reload_tracks() def __move_user_trackid(self, playlist: Playlist, trackid: int, *, offset: int) -> bool: diff --git a/tests/db/test_playlists.py b/tests/db/test_playlists.py index 87799d9..a4a43e9 100644 --- a/tests/db/test_playlists.py +++ b/tests/db/test_playlists.py @@ -1,5 +1,6 @@ # Copyright 2022 (c) Anna Schumaker """Tests our playlist Gio.ListModel.""" +import datetime import pathlib import unittest.mock import emmental.db @@ -326,6 +327,18 @@ class TestSystemPlaylists(tests.util.TestCase): pathlib.Path("/a/b/1.ogg"), self.medium, self.year) + def test_midnight_alarm(self): + """Test playlist maintenance run every night at midnight.""" + with unittest.mock.patch.object(self.table.new_tracks, + "reload_tracks") as mock_reload: + self.table._Table__at_midnight() + mock_reload.assert_called() + + with unittest.mock.patch("emmental.alarm.set_alarm") as mock_set: + table2 = emmental.db.playlists.Table(self.sql) + mock_set.assert_called_with(datetime.time(second=5), + table2._Table__at_midnight) + def test_collection(self): """Test the Collection playlist.""" self.assertIsInstance(self.table.collection,