From 52d48406ea8d6fbd83a81391133d212587da867d Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 4 Jun 2021 15:43:35 +0100 Subject: [PATCH] Reordered functions in model.Playlists --- app/model.py | 136 ++++++++++++++++++++++----------------------------- 1 file changed, 58 insertions(+), 78 deletions(-) diff --git a/app/model.py b/app/model.py index ef9e5c6..36836ee 100644 --- a/app/model.py +++ b/app/model.py @@ -166,6 +166,64 @@ class Playlists(Base): def __repr__(self): return (f"") + def add_track(self, session, track, row=None): + """ + Add track to playlist at given row. + If row=None, add to end of playlist + """ + + if not row: + row = PlaylistTracks.new_row(session, self.id) + + glue = PlaylistTracks(row=row) + glue.track_id = track.id + self.tracks.append(glue) + session.commit() + + def close(self, session): + "Record playlist as no longer loaded" + + self.loaded = False + session.add(self) + session.commit() + + @staticmethod + def get_all_closed_playlists(session): + "Returns a list of all playlists not currently open" + + return ( + session.query(Playlists) + .filter( + (Playlists.loaded == False) | # noqa E712 + (Playlists.loaded == None) + ) + .order_by(Playlists.last_used.desc()) + ).all() + + @staticmethod + def get_all_playlists(session): + "Returns a list of all playlists" + + return session.query(Playlists).all() + + @staticmethod + def get_last_used(session): + """ + Return a list of playlists marked "loaded", ordered by loaded date. + """ + + return ( + session.query(Playlists) + .filter(Playlists.loaded == True) # noqa E712 + .order_by(Playlists.last_used.desc()) + ).all() + + def get_notes(self): + return [a.note for a in self.notes] + + def get_tracks(self): + return [a.tracks for a in self.tracks] + @staticmethod def new(session, name): DEBUG(f"Playlists.new(name={name})") @@ -186,77 +244,6 @@ class Playlists(Base): return p - def close(self, session): - "Record playlist as no longer loaded" - - self.loaded = False - session.add(self) - session.commit() - - @staticmethod - def get_last_used(session): - """ - Return a list of playlists marked "loaded", ordered by loaded date. - """ - - return ( - session.query(Playlists) - .filter(Playlists.loaded == True) # noqa E712 - .order_by(Playlists.last_used.desc()) - ).all() - - @staticmethod - def get_all_playlists(session): - "Returns a list of all playlists" - - return session.query(Playlists).all() - - @staticmethod - def get_all_closed_playlists(session): - "Returns a list of all playlists not currently open" - - return ( - session.query(Playlists) - .filter( - (Playlists.loaded == False) | # noqa E712 - (Playlists.loaded == None) - ) - .order_by(Playlists.last_used.desc()) - ).all() - - # Not currently used 1 June 2021 - # @staticmethod - # def get_name(session, plid): - # """ - # Return name of playlist with id 'plid' - # """ - - # return ( - # session.query(Playlists.name) - # .filter(Playlists.id == plid) - # ).one()[0] - - def add_track(self, session, track, row=None): - """ - Add track to playlist at given row. - If row=None, add to end of playlist - """ - - if not row: - row = PlaylistTracks.new_row(session, self.id) - - glue = PlaylistTracks(row=row) - glue.track_id = track.id - self.tracks.append(glue) - session.commit() - - def get_notes(self): - return [a.note for a in self.notes] - - def get_tracks(self): - return [a.tracks for a in self.tracks] - - class PlaylistTracks(Base): __tablename__ = 'playlisttracks' @@ -267,13 +254,6 @@ class PlaylistTracks(Base): tracks = relationship("Tracks", back_populates="playlists") playlists = relationship("Playlists", back_populates="tracks") - @staticmethod - def new_row(session, playlist_id): - "Return row number > largest existing row number" - - last_row = session.query(func.max(PlaylistTracks.row)).one()[0] - return last_row + 1 - @staticmethod def add_track(session, playlist_id, track_id, row): DEBUG(