Anna Schumaker
bbb248f665
To represent each disc of an album. Discs may have different subtitles that we want to display (or there may not be a subtitle at all). Casting a Disc to a string either appends the subtitle to the album name or returns the album name directly. This is intended to be used by the ColumnView to display album names Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
# Copyright 2021 (c) Anna Schumaker.
|
|
#
|
|
# Table: discs
|
|
# +--------+---------+--------+----------+
|
|
# | discid | albumid | number | subtitle |
|
|
# +--------+---------+--------+----------+
|
|
from gi.repository import GObject
|
|
from . import album
|
|
from . import execute
|
|
from . import objects
|
|
|
|
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"{str(self.album)}: {self.subtitle}"
|
|
return f"{str(self.album)}"
|
|
|
|
def do_get_column(self, column):
|
|
return execute(f"SELECT {column} FROM discs "
|
|
"WHERE discid=?", [ self.rowid ])
|
|
|
|
@GObject.Property
|
|
def album(self):
|
|
return album.Album(self.get_column("albumid"))
|
|
|
|
@GObject.Property
|
|
def number(self):
|
|
return self.get_column("number")
|
|
|
|
@GObject.Property
|
|
def subtitle(self):
|
|
return self.get_column("subtitle")
|
|
|
|
|
|
class DiscTable(objects.Table):
|
|
def __init__(self):
|
|
objects.Table.__init__(self, "discs", Disc)
|
|
|
|
def do_create(self):
|
|
execute("CREATE TABLE IF NOT EXISTS discs "
|
|
"(discid INTEGER PRIMARY KEY, "
|
|
" albumid INTEGER, "
|
|
" number INTEGER, "
|
|
" subtitle TEXT, "
|
|
" FOREIGN KEY(albumid) REFERENCES albums(albumid), "
|
|
" UNIQUE(albumid, number))")
|
|
|
|
def do_insert(self, album, number, subtitle):
|
|
subtitle = subtitle if subtitle and len(subtitle) > 0 else None
|
|
return execute("INSERT INTO discs (albumid, number, subtitle) "
|
|
"VALUES (?, ?, ?)", (int(album), number, subtitle))
|
|
|
|
def do_delete(self, disc):
|
|
return execute("DELETE FROM discs WHERE discid=?", [ int(disc) ])
|
|
|
|
def do_get(self, rowid):
|
|
return execute("SELECT discid FROM discs "
|
|
"WHERE discid=?", [ rowid ])
|
|
|
|
def do_lookup(self, album, number, subtitle=None):
|
|
return execute("SELECT discid FROM discs "
|
|
"WHERE (albumid=? AND number=?)", [ int(album), number ])
|
|
|
|
|
|
Table = DiscTable()
|