db: Convert the DiscTable into a playlist.ChildModel
Implements: Issue #11 (Cache database items fields) Implements: Issue #14 (Convert Tables into Gio.ListModels) Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
737d135d41
commit
44a002ecac
|
@ -26,11 +26,11 @@ def make_fake_track(trackno, length, title, path, lib="/a/b/c", art="Test Artist
|
|||
length, title, pathlib.Path(path))
|
||||
|
||||
def reset():
|
||||
mods = [ artist, album, genre, decade, year, library, state ]
|
||||
mods = [ artist, album, disc, genre, decade, year, library, state ]
|
||||
|
||||
for mod in mods: mod.Table.reset()
|
||||
|
||||
mods = [ disc, track, user ]
|
||||
mods = [ track, user ]
|
||||
|
||||
for mod in mods: mod.Table.drop()
|
||||
for mod in mods: mod.Table.do_create()
|
||||
|
|
88
db/disc.py
88
db/disc.py
|
@ -5,73 +5,57 @@
|
|||
# | discid | albumid | number | subtitle |
|
||||
# +--------+---------+--------+----------+
|
||||
from gi.repository import GObject
|
||||
from .sql import execute
|
||||
from . import album
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import playlist
|
||||
from . import sql
|
||||
|
||||
class Disc(objects.Row):
|
||||
def __gt__(self, rhs): return self.number > rhs.number
|
||||
def __lt__(self, rhs): return self.number < rhs.number
|
||||
def __str__(self):
|
||||
if self.subtitle:
|
||||
return f"{self.album.name}: {self.subtitle}"
|
||||
return f"{self.album.name}"
|
||||
|
||||
def do_get_column(self, column):
|
||||
return execute(f"SELECT {column} FROM discs "
|
||||
"WHERE discid=?", [ self.rowid ])
|
||||
class Disc(playlist.Playlist):
|
||||
def __init__(self, row):
|
||||
playlist.Playlist.__init__(self, row)
|
||||
self._number = row["number"]
|
||||
self._subtitle = row["subtitle"]
|
||||
|
||||
@GObject.Property
|
||||
def album(self):
|
||||
return album.Table.get(self.get_column("albumid"))
|
||||
|
||||
@GObject.Property
|
||||
def number(self):
|
||||
return self.get_column("number")
|
||||
|
||||
@GObject.Property
|
||||
def playlist_state(self):
|
||||
return state.Table.get(self.get_column("plstateid"))
|
||||
def name(self):
|
||||
if self._subtitle:
|
||||
return f"{self._number}: {self._subtitle}"
|
||||
return f"Disc {self._number}"
|
||||
|
||||
@GObject.Property
|
||||
def subtitle(self):
|
||||
return self.get_column("subtitle")
|
||||
return self._subtitle if self._subtitle else ""
|
||||
|
||||
|
||||
class DiscTable(objects.Table):
|
||||
class DiscTable(playlist.ChildModel):
|
||||
def __init__(self):
|
||||
objects.Table.__init__(self, "discs", Disc)
|
||||
playlist.ChildModel.__init__(self, "discs", "albumid", "number")
|
||||
|
||||
def do_create(self):
|
||||
execute("CREATE TABLE IF NOT EXISTS discs "
|
||||
"(discid INTEGER PRIMARY KEY, "
|
||||
" albumid INTEGER, "
|
||||
" plstateid INTEGER NOT NULL, "
|
||||
" number INTEGER, "
|
||||
" subtitle TEXT, "
|
||||
" FOREIGN KEY(albumid) REFERENCES albums(albumid), "
|
||||
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid), "
|
||||
" UNIQUE(albumid, number))")
|
||||
sql.execute("CREATE TABLE IF NOT EXISTS discs "
|
||||
"(discid INTEGER PRIMARY KEY, "
|
||||
" albumid INTEGER, "
|
||||
" plstateid INTEGER NOT NULL, "
|
||||
" number INTEGER, "
|
||||
" subtitle TEXT, "
|
||||
" FOREIGN KEY(albumid) REFERENCES albums(albumid), "
|
||||
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid), "
|
||||
" UNIQUE(albumid, number))")
|
||||
|
||||
def do_insert(self, album, number, subtitle):
|
||||
subtitle = subtitle if subtitle and len(subtitle) > 0 else None
|
||||
plstate = state.Table.insert(random=False, loop=False)
|
||||
return execute("INSERT INTO discs (albumid, plstateid, number, subtitle) "
|
||||
"VALUES (?, ?, ?, ?)",
|
||||
[ album.rowid, plstate.rowid, number, subtitle ])
|
||||
def do_factory(self, row):
|
||||
return Disc(row)
|
||||
|
||||
def do_delete(self, disc):
|
||||
state.Table.delete(disc.playlist_state)
|
||||
return execute("DELETE FROM discs WHERE discid=?", [ int(disc) ])
|
||||
def do_insert(self, plstate, album, number, subtitle):
|
||||
return sql.execute("INSERT INTO discs (albumid, plstateid, number, subtitle) "
|
||||
"VALUES (?, ?, ?, ?)",
|
||||
[ album.rowid, plstate.rowid, number, subtitle ])
|
||||
|
||||
def do_get(self, rowid):
|
||||
return execute("SELECT discid FROM discs "
|
||||
"WHERE discid=?", [ rowid ])
|
||||
def do_lookup(self, album, number):
|
||||
return sql.execute("SELECT * FROM discs WHERE (albumid=? AND number=?)",
|
||||
[ album.rowid, number ])
|
||||
|
||||
def do_lookup(self, album, number, subtitle=None):
|
||||
return execute("SELECT discid FROM discs "
|
||||
"WHERE (albumid=? AND number=?)", [ album.rowid, number ])
|
||||
def find(self, album, number, subtitle):
|
||||
if (res := self.lookup(album, number)):
|
||||
return res
|
||||
return self.insert(album, number, subtitle)
|
||||
|
||||
|
||||
Table = DiscTable()
|
||||
|
|
|
@ -4,72 +4,67 @@ import sqlite3
|
|||
import unittest
|
||||
from gi.repository import GObject
|
||||
|
||||
class TestDisc(unittest.TestCase):
|
||||
def setUp(self):
|
||||
db.reset()
|
||||
|
||||
def make_disc(self, artist, album, discno, subtitle):
|
||||
artist = db.artist.Table.find(artist, artist)
|
||||
return db.disc.Table.find(artist.find_album(album), discno, subtitle)
|
||||
|
||||
def test_init(self):
|
||||
disc = self.make_disc("Test Artist", "Test Album", 1, "")
|
||||
self.assertIsInstance(disc, db.playlist.Playlist)
|
||||
self.assertEqual(disc._number, 1)
|
||||
|
||||
def test_subtitle(self):
|
||||
disc = self.make_disc("Test Artist", "Test Album", 1, "Test Subtitle")
|
||||
self.assertEqual(disc._subtitle, "Test Subtitle")
|
||||
self.assertEqual(disc.get_property("subtitle"), "Test Subtitle")
|
||||
self.assertEqual(disc.get_property("name"), "1: Test Subtitle")
|
||||
|
||||
def test_subtitle_none(self):
|
||||
disc = self.make_disc("Test Artist", "Test Album", 1, None)
|
||||
self.assertEqual(disc._subtitle, None)
|
||||
self.assertEqual(disc.get_property("subtitle"), "")
|
||||
self.assertEqual(disc.get_property("name"), "Disc 1")
|
||||
|
||||
def test_subtitle_len_0(self):
|
||||
disc = self.make_disc("Test Artist", "Test Album", 1, "")
|
||||
self.assertEqual(disc._subtitle, "")
|
||||
self.assertEqual(disc.get_property("subtitle"), "")
|
||||
self.assertEqual(disc.get_property("name"), "Disc 1")
|
||||
|
||||
|
||||
class TestDiscTable(unittest.TestCase):
|
||||
def setUp(self):
|
||||
db.reset()
|
||||
|
||||
def test_disc_table_init(self):
|
||||
def test_init(self):
|
||||
table = db.disc.DiscTable()
|
||||
self.assertIsInstance(table, db.playlist.ChildModel)
|
||||
self.assertEqual(table.table, "discs")
|
||||
self.assertEqual(table.parent, "albumid")
|
||||
self.assertEqual(table.order, "number")
|
||||
|
||||
self.assertIsInstance(db.disc.Table, db.disc.DiscTable)
|
||||
db.sql.execute("SELECT discid,albumid,plstateid,number,subtitle FROM discs")
|
||||
|
||||
def test_disc_table_insert(self):
|
||||
def test_insert(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
disc = db.disc.Table.insert(album, 1, "subtitle")
|
||||
|
||||
self.assertIsInstance(disc, db.disc.Disc)
|
||||
self.assertIsInstance(disc, db.objects.Row)
|
||||
|
||||
self.assertEqual(disc.album, album)
|
||||
self.assertEqual(disc.number, 1)
|
||||
self.assertEqual(disc.subtitle, "subtitle")
|
||||
self.assertEqual(str(disc), "Test Album: subtitle")
|
||||
self.assertIsInstance(disc.playlist_state, db.state.PlaylistState)
|
||||
|
||||
disc2 = db.disc.Table.insert(album, 2, None)
|
||||
self.assertEqual(disc2.subtitle, None)
|
||||
self.assertEqual(str(disc2), "Test Album")
|
||||
|
||||
disc3 = db.disc.Table.insert(album, 3, "")
|
||||
self.assertEqual(disc3.subtitle, None)
|
||||
self.assertEqual(str(disc3), "Test Album")
|
||||
self.assertEqual(disc._number, 1)
|
||||
self.assertEqual(disc._subtitle, "subtitle")
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.disc.Table.insert(album, 1, "subtitle")
|
||||
|
||||
def test_disc_table_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
disc = db.disc.Table.find(album, 1, "subtitle")
|
||||
state = disc.playlist_state
|
||||
|
||||
db.disc.Table.delete(disc)
|
||||
self.assertIsNone(db.disc.Table.lookup(album, 1))
|
||||
self.assertIsNone(db.state.Table.get(state.rowid))
|
||||
|
||||
def test_disc_table_get(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
disc = db.disc.Table.insert(album, 1, None)
|
||||
self.assertEqual(db.disc.Table.get(1), disc)
|
||||
self.assertIsNone(db.disc.Table.get(2))
|
||||
|
||||
def test_disc_table_lookup(self):
|
||||
def test_lookup(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
disc = db.disc.Table.insert(album, 1, None)
|
||||
self.assertEqual(db.disc.Table.lookup(album, 1), disc)
|
||||
self.assertIsNone(db.disc.Table.lookup(album, "none"))
|
||||
|
||||
def test_disc_compare(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
disc1 = db.disc.Table.insert(album, 1, "subtitle")
|
||||
disc2 = db.disc.Table.insert(album, 2, "subtitle")
|
||||
|
||||
self.assertTrue(disc1 < disc2)
|
||||
self.assertTrue(disc2 > disc1)
|
||||
|
||||
self.assertFalse(disc1 > disc2)
|
||||
self.assertFalse(disc2 < disc1)
|
||||
|
|
|
@ -43,7 +43,7 @@ class Track(objects.Row):
|
|||
|
||||
@GObject.Property
|
||||
def disc(self):
|
||||
return disc.Disc(self.get_column("discid"))
|
||||
return disc.Table.get(self.get_column("discid"))
|
||||
|
||||
@GObject.Property
|
||||
def decade(self):
|
||||
|
@ -124,7 +124,7 @@ class TrackTable(objects.Table):
|
|||
return execute("INSERT INTO tracks (libraryid, artistid, albumid, discid, "
|
||||
"decadeid, yearid, number, length, title, path) "
|
||||
"VALUES (?,?,?,?,?,?,?,?,?,?)",
|
||||
[ library.rowid, artist.rowid, album.rowid, int(disc),
|
||||
[ library.rowid, artist.rowid, album.rowid, disc.rowid,
|
||||
decade.rowid, year.rowid, number, length, title, str(path) ])
|
||||
|
||||
def do_delete(self, track):
|
||||
|
|
Loading…
Reference in New Issue