No db calls when servicing data() except for caching

This commit is contained in:
Keith Edmunds 2025-03-08 21:30:37 +00:00
parent 85493de179
commit 963da0b5d0
3 changed files with 32 additions and 23 deletions

View File

@ -109,7 +109,7 @@ class NoteColours(dbtables.NoteColoursTable):
@staticmethod
def get_colour(
session: Session, text: str, foreground: bool = False
) -> Optional[str]:
) -> str:
"""
Parse text and return background (foreground if foreground==True) colour
string if matched, else None
@ -117,7 +117,7 @@ class NoteColours(dbtables.NoteColoursTable):
"""
if not text:
return None
return ""
match = False
for rec in NoteColours.get_all(session):
@ -138,10 +138,10 @@ class NoteColours(dbtables.NoteColoursTable):
if match:
if foreground:
return rec.foreground
return rec.foreground or ""
else:
return rec.colour
return None
return ""
def invalidate_cache(self) -> None:
"""Invalidate dogpile cache"""

View File

@ -439,6 +439,12 @@ class RowAndTrack:
self.row_number = playlist_row.row_number
self.track_id = playlist_row.track_id
# Playlist display data
self.row_fg: Optional[str] = None
self.row_bg: Optional[str] = None
self.note_fg: Optional[str] = None
self.note_bg: Optional[str] = None
# Collect track data if there's a track
if playlist_row.track_id:
self.artist = playlist_row.track.artist

View File

@ -70,8 +70,11 @@ class PlaylistModel(QAbstractTableModel):
database.
"""
def __init__(self, playlist_id: int, is_template: bool,) -> None:
def __init__(
self,
playlist_id: int,
is_template: bool,
) -> None:
super().__init__()
log.debug("PlaylistModel.__init__()")
@ -182,12 +185,13 @@ class PlaylistModel(QAbstractTableModel):
# Header row
if self.is_header_row(row):
# Check for specific header colouring
with db.Session() as session:
note_background = NoteColours.get_colour(session, rat.note)
if note_background:
return QBrush(QColor(note_background))
else:
return QBrush(QColor(Config.COLOUR_NOTES_PLAYLIST))
if rat.row_bg is None:
with db.Session() as session:
rat.row_bg = NoteColours.get_colour(session, rat.note)
if rat.row_bg:
return QBrush(QColor(rat.row_bg))
else:
return QBrush(QColor(Config.COLOUR_NOTES_PLAYLIST))
# Unreadable track file
if file_is_unreadable(rat.path):
return QBrush(QColor(Config.COLOUR_UNREADABLE))
@ -219,10 +223,11 @@ class PlaylistModel(QAbstractTableModel):
return QBrush(QColor(Config.COLOUR_BITRATE_OK))
if column == Col.NOTE.value:
if rat.note:
with db.Session() as session:
note_background = NoteColours.get_colour(session, rat.note)
if note_background:
return QBrush(QColor(note_background))
if rat.note_bg is None:
with db.Session() as session:
rat.note_bg = NoteColours.get_colour(session, rat.note)
if rat.note_bg:
return QBrush(QColor(rat.note_bg))
return QBrush()
@ -1575,13 +1580,11 @@ class PlaylistModel(QAbstractTableModel):
if not track_id:
return ""
playdates = Playdates.last_playdates(session, track_id)
return (
"<br>".join(
[
a.lastplayed.strftime(Config.LAST_PLAYED_TOOLTIP_DATE_FORMAT)
for a in playdates
]
)
return "<br>".join(
[
a.lastplayed.strftime(Config.LAST_PLAYED_TOOLTIP_DATE_FORMAT)
for a in playdates
]
)
def update_or_insert(self, track_id: int, row_number: int) -> None: