db: Attach release information to albums
This lets us sort albums by original release date, so if an artist releases multiple albums in a year we can sort by month too. Additionally, this helps us better handle albums with the same name that were released in different years. Implements: #40 (Store the full date in the year table) Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
050a930376
commit
0b818bc067
|
@ -1,4 +1,5 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import datetime
|
||||
import lib
|
||||
import pathlib
|
||||
|
||||
|
@ -47,10 +48,10 @@ def find_playlist(plstateid):
|
|||
return None
|
||||
|
||||
def make_fake_track(trackno, length, title, path, lib="/a/b/c", art="Test Artist",
|
||||
alb="Test Album", disk=1, subtitle=None, yeer=2021):
|
||||
alb="Test Album", disk=1, subtitle=None, yeer=2021, mnth=3, dy=18):
|
||||
lib = library.Table.find(pathlib.Path(lib))
|
||||
art = artist.Table.find(art, art)
|
||||
alb = art.find_album(alb)
|
||||
alb = art.find_album(alb, datetime.date(yeer, mnth, dy))
|
||||
disk = alb.find_disc(disk, subtitle)
|
||||
dec = decade.Table.find((yeer // 10) * 10)
|
||||
yeer = dec.find_year(yeer)
|
||||
|
|
22
db/album.py
22
db/album.py
|
@ -18,10 +18,14 @@ class Album(playlist.ParentPlaylist):
|
|||
def __init__(self, row):
|
||||
playlist.ParentPlaylist.__init__(self, row, "media-optical-cd-audio")
|
||||
self._name = row["name"]
|
||||
self._release = row["release"]
|
||||
|
||||
@GObject.Property
|
||||
def name(self): return self._name
|
||||
|
||||
@GObject.Property
|
||||
def release(self): return self._release
|
||||
|
||||
def delete(self): Table.delete(self)
|
||||
|
||||
def find_disc(self, number, subtitle):
|
||||
|
@ -41,24 +45,26 @@ class AlbumTable(playlist.ChildModel):
|
|||
"(albumid INTEGER PRIMARY KEY, "
|
||||
" artistid INTEGER, "
|
||||
" plstateid INTEGER NOT NULL, "
|
||||
" release DATE NOT NULL, "
|
||||
" name TEXT, "
|
||||
" sort TEXT, "
|
||||
" FOREIGN KEY(artistid) REFERENCES artists(artistid), "
|
||||
" FOREIGN KEY(plstateid) REFERENCES playlist_states(plstateid), "
|
||||
" UNIQUE(artistid, name))")
|
||||
" UNIQUE(artistid, release, name))")
|
||||
sql.execute("CREATE INDEX IF NOT EXISTS artist_index ON albums(artistid, name)")
|
||||
|
||||
def do_factory(self, row):
|
||||
return Album(row)
|
||||
|
||||
def do_insert(self, plstate, artist, name):
|
||||
return sql.execute("INSERT INTO albums (artistid, plstateid, name, sort) "
|
||||
"VALUES (?, ?, ?, ?)",
|
||||
[ artist.rowid, plstate.rowid, name, name.casefold() ])
|
||||
def do_insert(self, plstate, artist, name, release):
|
||||
return sql.execute("INSERT INTO albums (artistid, plstateid, release, name, sort) "
|
||||
"VALUES (?, ?, ?, ?, ?)",
|
||||
[ artist.rowid, plstate.rowid, release, name, name.casefold() ])
|
||||
|
||||
def do_lookup(self, artist, name):
|
||||
return sql.execute("SELECT * FROM albums WHERE (artistid=? AND name=?)",
|
||||
[ artist.rowid, name ])
|
||||
def do_lookup(self, artist, name, release):
|
||||
return sql.execute("SELECT * FROM albums "
|
||||
"WHERE (artistid=? AND name=? AND release=?)",
|
||||
[ artist.rowid, name, release ])
|
||||
|
||||
|
||||
Table = AlbumTable()
|
||||
|
|
|
@ -24,7 +24,7 @@ class Artist(playlist.ParentPlaylist):
|
|||
|
||||
def delete(self): Table.delete(self)
|
||||
|
||||
def find_album(self, name): return self.find_child(name)
|
||||
def find_album(self, name, release): return self.find_child(name, release)
|
||||
|
||||
def get_child_table(self): return album.Table
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import datetime
|
||||
import db
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
@ -16,27 +17,29 @@ class TestAlbum(unittest.TestCase):
|
|||
|
||||
def test_init(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
self.assertIsInstance(album, db.playlist.ParentPlaylist)
|
||||
self.assertEqual(album.get_property("name"), "Test Album")
|
||||
self.assertEqual(album.get_property("icon-name"), "media-optical-cd-audio")
|
||||
self.assertEqual(album.get_property("release"), datetime.date(2021, 3, 18))
|
||||
self.assertEqual(album.get_child_table(), db.disc.Table)
|
||||
|
||||
def test_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
album.delete()
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "Test Album"))
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "Test Album",
|
||||
datetime.date(2021, 3, 18)))
|
||||
|
||||
def test_find_disc(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
disc = album.find_disc(1, None)
|
||||
self.assertIsInstance(disc, db.disc.Disc)
|
||||
|
||||
def test_tracks(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
album.connect("track-added", self.track_added)
|
||||
self.assertEqual(album.get_n_tracks(), 0)
|
||||
self.assertEqual(album.get_tracks(), [ ])
|
||||
|
@ -66,21 +69,22 @@ class TestAlbumTable(unittest.TestCase):
|
|||
self.assertEqual(table.order, "sort")
|
||||
|
||||
self.assertIsInstance(db.album.Table, db.album.AlbumTable)
|
||||
db.sql.execute("SELECT albumid,artistid,plstateid,name,sort FROM albums")
|
||||
db.sql.execute("SELECT albumid,artistid,plstateid,release,name,sort FROM albums")
|
||||
|
||||
def test_insert(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
|
||||
self.assertIsInstance(album, db.album.Album)
|
||||
self.assertEqual(album._name, "Test Album")
|
||||
self.assertEqual(album._rowkey, "albumid")
|
||||
|
||||
with self.assertRaises(sqlite3.IntegrityError):
|
||||
db.album.Table.insert(artist, "Test Album")
|
||||
db.album.Table.insert(artist, "Test Album",
|
||||
datetime.date(2021, 3, 18))
|
||||
|
||||
def test_lookup(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
self.assertEqual(db.album.Table.lookup(artist, "Test Album"), album)
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "none"))
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
self.assertEqual(db.album.Table.lookup(artist, "Test Album", datetime.date(2021, 3, 18)), album)
|
||||
self.assertIsNone(db.album.Table.lookup(artist, "none", datetime.date(1, 1, 1)))
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import datetime
|
||||
import db
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
@ -28,7 +29,7 @@ class TestArtist(unittest.TestCase):
|
|||
|
||||
def test_find_album(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
self.assertIsInstance(album, db.album.Album)
|
||||
|
||||
def test_tracks(self):
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import datetime
|
||||
import db
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
@ -16,7 +17,8 @@ class TestDisc(unittest.TestCase):
|
|||
|
||||
def make_disc(self, artist, album, discno, subtitle):
|
||||
artist = db.artist.Table.find(artist, artist)
|
||||
return artist.find_album(album).find_disc(discno, subtitle)
|
||||
album = artist.find_album(album, datetime.date(2021, 3, 18))
|
||||
return album.find_disc(discno, subtitle)
|
||||
|
||||
def test_init(self):
|
||||
disc = self.make_disc("Test Artist", "Test Album", 1, "")
|
||||
|
@ -26,7 +28,7 @@ class TestDisc(unittest.TestCase):
|
|||
|
||||
def test_delete(self):
|
||||
artist = db.artist.Table.find("Test Artist", "Test Artist")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
disc = album.find_disc(1, "")
|
||||
disc.delete()
|
||||
self.assertIsNone(db.disc.Table.lookup(album, 1))
|
||||
|
@ -84,7 +86,7 @@ class TestDiscTable(unittest.TestCase):
|
|||
|
||||
def test_insert(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
disc = db.disc.Table.insert(album, 1, "subtitle")
|
||||
|
||||
self.assertIsInstance(disc, db.disc.Disc)
|
||||
|
@ -97,7 +99,7 @@ class TestDiscTable(unittest.TestCase):
|
|||
|
||||
def test_lookup(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
disc = album.find_disc(1, None)
|
||||
self.assertEqual(db.disc.Table.lookup(album, 1), disc)
|
||||
self.assertIsNone(db.disc.Table.lookup(album, "none"))
|
||||
|
|
|
@ -71,7 +71,7 @@ class TestTrackTable(unittest.TestCase):
|
|||
def test_insert(self):
|
||||
library = db.library.Table.find(pathlib.Path("/a/b/c"))
|
||||
artist = db.artist.Table.find("Test Artist", "test artist")
|
||||
album = artist.find_album("Test Album")
|
||||
album = artist.find_album("Test Album", datetime.date(2021, 3, 18))
|
||||
disc = album.find_disc(1, None)
|
||||
decade = db.decade.Table.find(2020)
|
||||
year = decade.find_year(2021)
|
||||
|
|
|
@ -24,7 +24,7 @@ class FileTask(Task):
|
|||
|
||||
with metadata.Metadata(self.filepath) as meta:
|
||||
artist = db.artist.Table.find(meta.artist(), meta.artistsort())
|
||||
album = artist.find_album(meta.album())
|
||||
album = artist.find_album(meta.album(), meta.release())
|
||||
disc = album.find_disc(meta.discnumber(), meta.discsubtitle())
|
||||
decade = db.decade.Table.find(meta.decade())
|
||||
year = decade.find_year(meta.year())
|
||||
|
|
|
@ -42,7 +42,8 @@ class TestScannerFileTask(unittest.TestCase):
|
|||
artist = db.artist.Table.lookup("Test Artist")
|
||||
self.assertIsNotNone(artist)
|
||||
|
||||
album = db.album.Table.lookup(artist, "Test Album")
|
||||
album = db.album.Table.lookup(artist, "Test Album",
|
||||
datetime.date(2019, 2, 1))
|
||||
self.assertIsNotNone(album)
|
||||
|
||||
self.assertIsNotNone(db.disc.Table.lookup(album, 1))
|
||||
|
|
Loading…
Reference in New Issue