db: Give Albums a PlaylistState property
Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
38861bbabf
commit
3abf2d3cf7
20
db/album.py
20
db/album.py
|
@ -1,9 +1,9 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
#
|
||||
# Table: albums
|
||||
# +---------+----------+------+------+
|
||||
# | albumid | artistid | name | sort |
|
||||
# +---------+----------+------+------+
|
||||
# +---------+----------+-----------+------+------+
|
||||
# | albumid | artistid | plstateid | name | sort |
|
||||
# +---------+----------+-----------+------+------+
|
||||
#
|
||||
# Index: album_index
|
||||
# +-----------------------------+
|
||||
|
@ -14,6 +14,7 @@ from . import artist
|
|||
from . import disc
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
class Album(objects.Tag):
|
||||
def do_get_column(self, column):
|
||||
|
@ -24,6 +25,10 @@ class Album(objects.Tag):
|
|||
def artist(self):
|
||||
return artist.Artist(self.get_column("artistid"))
|
||||
|
||||
@GObject.Property
|
||||
def playlist_state(self):
|
||||
return state.PlaylistState(self.get_column("plstateid"))
|
||||
|
||||
def discs(self):
|
||||
cursor = execute(f"SELECT discid FROM discs "
|
||||
"WHERE albumid=?", [ self.rowid ])
|
||||
|
@ -38,18 +43,23 @@ class AlbumTable(objects.Table):
|
|||
execute("CREATE TABLE IF NOT EXISTS albums "
|
||||
"(albumid INTEGER PRIMARY KEY, "
|
||||
" artistid INTEGER, "
|
||||
" plstateid INTEGER NOT NULL, "
|
||||
" name TEXT, "
|
||||
" sort TEXT, "
|
||||
" FOREIGN KEY(artistid) REFERENCES artists(artistid), "
|
||||
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid), "
|
||||
" UNIQUE(artistid, name))")
|
||||
execute("CREATE INDEX IF NOT EXISTS artist_index "
|
||||
"ON albums(artistid, name)")
|
||||
|
||||
def do_insert(self, artist, name):
|
||||
return execute("INSERT INTO albums (artistid, name, sort) "
|
||||
"VALUES (?, ?, ?)", (int(artist), name, name.casefold()))
|
||||
plstate = state.Table.insert(random=False, loop=False)
|
||||
return execute("INSERT INTO albums (artistid, plstateid, name, sort) "
|
||||
"VALUES (?, ?, ?, ?)",
|
||||
[ int(artist), int(plstate), name, name.casefold() ])
|
||||
|
||||
def do_delete(self, album):
|
||||
state.Table.delete(album.playlist_state)
|
||||
return execute("DELETE FROM albums WHERE albumid=?", [ int(album) ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
|
|
|
@ -14,7 +14,7 @@ class TestAlbumTable(unittest.TestCase):
|
|||
|
||||
def test_album_table_init(self):
|
||||
self.assertIsInstance(db.album.Table, db.album.AlbumTable)
|
||||
db.execute("SELECT albumid,artistid,name,sort FROM albums")
|
||||
db.execute("SELECT albumid,artistid,plstateid,name,sort FROM albums")
|
||||
|
||||
def test_album_table_insert(self):
|
||||
db.album.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
@ -29,6 +29,7 @@ class TestAlbumTable(unittest.TestCase):
|
|||
self.assertEqual(album.name, "Test Album")
|
||||
self.assertEqual(album.sort, "test album")
|
||||
self.assertEqual(album.get_property("artist"), artist)
|
||||
self.assertIsInstance(album.playlist_state, db.state.PlaylistState)
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.album.Table.insert(artist, "Test Album")
|
||||
|
@ -36,9 +37,11 @@ class TestAlbumTable(unittest.TestCase):
|
|||
def test_album_table_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = db.album.Table.find(artist, "Test Album")
|
||||
state = album.playlist_state
|
||||
|
||||
db.album.Table.delete(album)
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "Test Album"))
|
||||
self.assertIsNone(db.state.Table.get(int(state)))
|
||||
|
||||
def test_album_table_get(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
|
|
Loading…
Reference in New Issue