Tidy up model.py
This commit is contained in:
parent
6336eb9215
commit
87fb74b14f
115
app/model.py
115
app/model.py
@ -53,6 +53,8 @@ class Notes(Base):
|
||||
|
||||
@staticmethod
|
||||
def add_note(session, playlist_id, row, text):
|
||||
"Add note"
|
||||
|
||||
DEBUG(f"add_note(playlist_id={playlist_id}, row={row}, text={text})")
|
||||
note = Notes()
|
||||
note.playlist_id = playlist_id
|
||||
@ -64,16 +66,13 @@ class Notes(Base):
|
||||
|
||||
@staticmethod
|
||||
def delete_note(session, id):
|
||||
"Delete note"
|
||||
|
||||
DEBUG(f"delete_note(id={id}")
|
||||
|
||||
session.query(Notes).filter(Notes.id == id).delete()
|
||||
session.commit()
|
||||
|
||||
# Not currently used 1 June 2021
|
||||
# @staticmethod
|
||||
# def get_note(session, id):
|
||||
# return session.query(Notes).filter(Notes.id == id).one()
|
||||
|
||||
@classmethod
|
||||
def update_note(cls, session, id, row, text=None):
|
||||
"""
|
||||
@ -99,6 +98,8 @@ class Playdates(Base):
|
||||
|
||||
@staticmethod
|
||||
def add_playdate(session, track):
|
||||
"Record that track was played"
|
||||
|
||||
DEBUG(f"add_playdate(track={track})")
|
||||
pd = Playdates()
|
||||
pd.lastplayed = datetime.now()
|
||||
@ -451,6 +452,17 @@ class Tracks(Base):
|
||||
f"<Track(id={self.id}, title={self.title}, "
|
||||
f"artist={self.artist}, path={self.path}>"
|
||||
)
|
||||
# Not currently used 1 June 2021
|
||||
# @staticmethod
|
||||
# def get_note(session, id):
|
||||
# return session.query(Notes).filter(Notes.id == id).one()
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_all_paths(session):
|
||||
"Return a list of paths of all tracks"
|
||||
|
||||
return [a[0] for a in session.query(Tracks.path).all()]
|
||||
|
||||
@classmethod
|
||||
def get_or_create(cls, session, path):
|
||||
@ -472,53 +484,6 @@ class Tracks(Base):
|
||||
ERROR(f"Can't find track id {id}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_all_paths(session):
|
||||
"Return a list of paths of all tracks"
|
||||
|
||||
return [a[0] for a in session.query(Tracks.path).all()]
|
||||
|
||||
@staticmethod
|
||||
def get_path(session, id):
|
||||
try:
|
||||
return session.query(Tracks.path).filter(Tracks.id == id).one()[0]
|
||||
except NoResultFound:
|
||||
ERROR(f"Can't find track id {id}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_track(session, id):
|
||||
try:
|
||||
DEBUG(f"Tracks.get_track(track_id={id})")
|
||||
track = session.query(Tracks).filter(Tracks.id == id).one()
|
||||
return track
|
||||
except NoResultFound:
|
||||
ERROR(f"get_track({id}): not found")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def search(session, title=None, artist=None, duration=None):
|
||||
"""
|
||||
Return any tracks matching passed criteria
|
||||
"""
|
||||
|
||||
DEBUG(
|
||||
f"Tracks.search({title=}, {artist=}), {duration=})"
|
||||
)
|
||||
|
||||
if not title and not artist and not duration:
|
||||
return None
|
||||
|
||||
q = session.query(Tracks).filter(False)
|
||||
if title:
|
||||
q = q.filter(Tracks.title == title)
|
||||
if artist:
|
||||
q = q.filter(Tracks.artist == artist)
|
||||
if duration:
|
||||
q = q.filter(Tracks.duration == duration)
|
||||
|
||||
return q.all()
|
||||
|
||||
@staticmethod
|
||||
def get_track_from_filename(session, filename):
|
||||
"""
|
||||
@ -546,6 +511,29 @@ class Tracks(Base):
|
||||
|
||||
return session.query(Tracks).filter(Tracks.path == path).first()
|
||||
|
||||
@staticmethod
|
||||
def get_path(session, track_id):
|
||||
"Return path of passed track_id, or None"
|
||||
|
||||
try:
|
||||
return session.query(Tracks.path).filter(
|
||||
Tracks.id == track_id).one()[0]
|
||||
except NoResultFound:
|
||||
ERROR(f"Can't find track id {track_id}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_track(session, track_id):
|
||||
"Return track or None"
|
||||
|
||||
try:
|
||||
DEBUG(f"Tracks.get_track(track_id={track_id})")
|
||||
track = session.query(Tracks).filter(Tracks.id == track_id).one()
|
||||
return track
|
||||
except NoResultFound:
|
||||
ERROR(f"get_track({track_id}): not found")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def remove_path(session, path):
|
||||
"Remove track with passed path from database"
|
||||
@ -558,6 +546,29 @@ class Tracks(Base):
|
||||
except IntegrityError as exception:
|
||||
ERROR(f"Can't remove track with {path=} ({exception=})")
|
||||
|
||||
@staticmethod
|
||||
def search(session, title=None, artist=None, duration=None):
|
||||
"""
|
||||
Return any tracks matching passed criteria
|
||||
"""
|
||||
|
||||
DEBUG(
|
||||
f"Tracks.search({title=}, {artist=}), {duration=})"
|
||||
)
|
||||
|
||||
if not title and not artist and not duration:
|
||||
return None
|
||||
|
||||
q = session.query(Tracks).filter(False)
|
||||
if title:
|
||||
q = q.filter(Tracks.title == title)
|
||||
if artist:
|
||||
q = q.filter(Tracks.artist == artist)
|
||||
if duration:
|
||||
q = q.filter(Tracks.duration == duration)
|
||||
|
||||
return q.all()
|
||||
|
||||
@staticmethod
|
||||
def search_titles(session, text):
|
||||
return (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user