db: Have Playlists use the child_set as the children base model
I combine this with the table's Filter object to show playlists matching the current search query. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
c0c516fb70
commit
17b2a82e20
|
@ -3,7 +3,6 @@
|
|||
import pathlib
|
||||
import sqlite3
|
||||
from gi.repository import GObject
|
||||
from gi.repository import Gtk
|
||||
from .media import Medium
|
||||
from .. import format
|
||||
from . import playlist
|
||||
|
@ -23,12 +22,8 @@ class Album(playlist.Playlist):
|
|||
"""Initialize an Album object."""
|
||||
super().__init__(**kwargs)
|
||||
self.add_children(self.table.sql.media,
|
||||
Gtk.CustomFilter.new(self.__match_medium),
|
||||
self.table.get_mediumids(self))
|
||||
|
||||
def __match_medium(self, medium: Medium) -> bool:
|
||||
return self.has_medium(medium) and len(medium.name) > 0
|
||||
|
||||
def add_medium(self, medium: Medium) -> None:
|
||||
"""Add a Medium to this Album."""
|
||||
self.add_child(medium)
|
||||
|
|
|
@ -19,7 +19,6 @@ class Artist(playlist.Playlist):
|
|||
"""Initialize an Artist object."""
|
||||
super().__init__(**kwargs)
|
||||
self.add_children(self.table.sql.albums,
|
||||
Gtk.CustomFilter.new(self.has_album),
|
||||
self.table.get_albumids(self))
|
||||
|
||||
def add_album(self, album: Album) -> None:
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
"""A custom Gio.ListModel for working with decades."""
|
||||
import sqlite3
|
||||
from gi.repository import GObject
|
||||
from gi.repository import Gtk
|
||||
from .years import Year
|
||||
from . import playlist
|
||||
from . import tracks
|
||||
|
@ -17,7 +16,6 @@ class Decade(playlist.Playlist):
|
|||
"""Initialize a Decade object."""
|
||||
super().__init__(**kwargs)
|
||||
self.add_children(self.table.sql.years,
|
||||
Gtk.CustomFilter.new(self.has_year),
|
||||
self.table.get_yearids(self))
|
||||
|
||||
def add_year(self, year: Year) -> None:
|
||||
|
|
|
@ -50,13 +50,11 @@ class Playlist(table.Row):
|
|||
self.table.remove_track(self, track)
|
||||
return True
|
||||
|
||||
def add_children(self, child_table: table.Table,
|
||||
child_filter: Gtk.Filter,
|
||||
child_keys: set | None = None) -> None:
|
||||
def add_children(self, child_table: table.Table, child_keys: set) -> None:
|
||||
"""Create a FilterListModel for this playlist's children."""
|
||||
child_keys = set() if child_keys is None else child_keys
|
||||
self.child_set = table.TableSubset(child_table, keys=child_keys)
|
||||
self.children = Gtk.FilterListModel.new(child_table, child_filter)
|
||||
self.children = Gtk.FilterListModel.new(self.child_set,
|
||||
child_table.get_filter())
|
||||
self.children.set_incremental(True)
|
||||
|
||||
def do_update(self, column: str) -> bool:
|
||||
|
|
|
@ -72,23 +72,14 @@ class TestAlbumObject(tests.util.TestCase):
|
|||
self.assertListEqual(self.album.get_media(), [1, 2, 3])
|
||||
mock.assert_called_with(self.album)
|
||||
|
||||
def test_media_model(self):
|
||||
"""Test getting a Gio.ListModel representing this Album's media."""
|
||||
def test_children(self):
|
||||
"""Test the Album's 'children' model is set up properly."""
|
||||
self.assertIsInstance(self.album.child_set,
|
||||
emmental.db.table.TableSubset)
|
||||
self.assertIsInstance(self.album.children, Gtk.FilterListModel)
|
||||
self.assertIsInstance(self.album.children.get_filter(),
|
||||
Gtk.CustomFilter)
|
||||
self.assertEqual(self.album.children.get_model(), self.sql.media)
|
||||
|
||||
album = self.table.create("Test Album", "Album Artist", "2023-03")
|
||||
medium = self.sql.media.create(album, "Test Medium", number=1)
|
||||
|
||||
self.assertFalse(self.album.children.get_filter().match(medium))
|
||||
|
||||
self.album.add_medium(medium)
|
||||
self.assertTrue(self.album.children.get_filter().match(medium))
|
||||
|
||||
medium.name = ""
|
||||
self.assertFalse(self.album.children.get_filter().match(medium))
|
||||
self.assertEqual(self.album.children.get_filter(),
|
||||
self.sql.media.get_filter())
|
||||
self.assertEqual(self.album.child_set.table, self.sql.media)
|
||||
|
||||
|
||||
class TestAlbumTable(tests.util.TestCase):
|
||||
|
|
|
@ -49,12 +49,11 @@ class TestArtistObject(tests.util.TestCase):
|
|||
|
||||
def test_children(self):
|
||||
"""Test that Albums have been added as Artist playlist children."""
|
||||
self.assertIsInstance(self.artist.children, Gtk.FilterListModel)
|
||||
self.assertIsInstance(self.artist.children.get_filter(),
|
||||
Gtk.CustomFilter)
|
||||
self.assertIsInstance(self.artist.child_set,
|
||||
emmental.db.table.TableSubset)
|
||||
self.assertEqual(self.artist.children.get_model(), self.sql.albums)
|
||||
self.assertIsInstance(self.artist.children, Gtk.FilterListModel)
|
||||
self.assertEqual(self.artist.children.get_filter(),
|
||||
self.sql.albums.get_filter())
|
||||
self.assertEqual(self.artist.child_set.table, self.sql.albums)
|
||||
|
||||
|
||||
|
|
|
@ -52,17 +52,12 @@ class TestDecadeObject(tests.util.TestCase):
|
|||
|
||||
def test_years_model(self):
|
||||
"""Test getting a Gio.ListModel representing a Decade's years."""
|
||||
self.assertIsInstance(self.decade.child_set,
|
||||
emmental.db.table.TableSubset)
|
||||
self.assertIsInstance(self.decade.children, Gtk.FilterListModel)
|
||||
self.assertIsInstance(self.decade.children.get_filter(),
|
||||
Gtk.CustomFilter)
|
||||
self.assertEqual(self.decade.children.get_model(), self.sql.years)
|
||||
|
||||
year = self.sql.years.create(2023)
|
||||
self.decade.add_year(year)
|
||||
self.assertTrue(self.decade.children.get_filter().match(year))
|
||||
|
||||
year = self.sql.years.create(1988)
|
||||
self.assertFalse(self.decade.children.get_filter().match(year))
|
||||
self.assertEqual(self.decade.children.get_filter(),
|
||||
self.sql.years.get_filter())
|
||||
self.assertEqual(self.decade.child_set.table, self.sql.years)
|
||||
|
||||
|
||||
class TestDecadeTable(tests.util.TestCase):
|
||||
|
|
|
@ -68,9 +68,8 @@ class TestPlaylistRow(unittest.TestCase):
|
|||
self.assertIsNone(self.playlist.child_set)
|
||||
self.assertIsNone(self.playlist.children)
|
||||
|
||||
filter = Gtk.Filter()
|
||||
table = emmental.db.table.Table(None)
|
||||
self.playlist.add_children(table, filter)
|
||||
self.playlist.add_children(table, set())
|
||||
|
||||
self.assertIsInstance(self.playlist.child_set,
|
||||
emmental.db.table.TableSubset)
|
||||
|
@ -78,13 +77,15 @@ class TestPlaylistRow(unittest.TestCase):
|
|||
self.assertSetEqual(self.playlist.child_set.keyset.keys, set())
|
||||
|
||||
self.assertIsInstance(self.playlist.children, Gtk.FilterListModel)
|
||||
self.assertEqual(self.playlist.children.get_filter(), filter)
|
||||
self.assertEqual(self.playlist.children.get_model(), table)
|
||||
self.assertEqual(self.playlist.children.get_filter(),
|
||||
table.get_filter())
|
||||
self.assertEqual(self.playlist.children.get_model(),
|
||||
self.playlist.child_set)
|
||||
self.assertTrue(self.playlist.children.get_incremental())
|
||||
|
||||
playlist2 = emmental.db.playlist.Playlist(table=self.table,
|
||||
propertyid=2, name="Plist2")
|
||||
playlist2.add_children(table, filter, {1, 2, 3})
|
||||
playlist2.add_children(table, {1, 2, 3})
|
||||
self.assertSetEqual(playlist2.child_set.keyset.keys, {1, 2, 3})
|
||||
|
||||
def test_parent(self):
|
||||
|
@ -114,7 +115,7 @@ class TestPlaylistRow(unittest.TestCase):
|
|||
"""Test adding a child playlist to the playlist."""
|
||||
table = emmental.db.table.Table(None)
|
||||
child = tests.util.table.MockRow(table=table, number=1)
|
||||
self.playlist.add_children(table, Gtk.Filter())
|
||||
self.playlist.add_children(table, set())
|
||||
|
||||
self.playlist.add_child(child)
|
||||
self.assertIn(child, self.playlist.child_set)
|
||||
|
@ -148,7 +149,7 @@ class TestPlaylistRow(unittest.TestCase):
|
|||
"""Test the playlist has_child() function."""
|
||||
table = emmental.db.table.Table(None)
|
||||
child = tests.util.table.MockRow(table=table, number=1)
|
||||
self.playlist.add_children(table, Gtk.Filter())
|
||||
self.playlist.add_children(table, set())
|
||||
|
||||
self.assertFalse(self.playlist.has_child(child))
|
||||
self.playlist.add_child(child)
|
||||
|
@ -179,7 +180,7 @@ class TestPlaylistRow(unittest.TestCase):
|
|||
"""Test removing a child playlist from the playlist."""
|
||||
table = emmental.db.table.Table(None)
|
||||
child = tests.util.table.MockRow(table=table, number=1)
|
||||
self.playlist.add_children(table, Gtk.Filter())
|
||||
self.playlist.add_children(table, set())
|
||||
|
||||
self.playlist.add_child(child)
|
||||
self.playlist.remove_child(child)
|
||||
|
|
Loading…
Reference in New Issue