def unavailable(offset: int=0, limit: int=0, sorted_by: str= "title", sort_order: str= "asc") -> List[Track]: """ Returns all non hidden tracks in the database :param sort_order: Whether to sort "asc"ending or "desc"ending :param sorted_by: By which attribute to sort (title, artist, album) :param offset: How many tracks to omit from the beginning of the result :param limit: The number of tracks to return :return: All non hidden tracks in the database """ q = db_session.query(Track)\ .join(Track.status)\ .filter(Status.imported == True)\ .filter(Status.available == False) if sorted_by == "title": order_by_column = Track.title elif sorted_by == "artist": order_by_column = Track.artist else: order_by_column = Track.album if sort_order == "desc": q = q.order_by(desc(order_by_column)) else: q = q.order_by(asc(order_by_column)) if offset > 0: q = q.offset(offset) if limit > 0: q = q.limit(limit) return q.all()
def all(offset: int = 0, limit: int = 0, sorted_by: str = "title", sort_order: str = "asc") -> List[Album]: """ Returns all albums with one or more non hidden tracks in the database :param sort_order: Whether to sort "asc"ending or "desc"ending :param sorted_by: By which attribute to sort (title, artist) :param offset: How many albums to omit from the beginning of the result :param limit: The number of albums to return :return: All albums with one or more non hidden tracks in the database """ q = db_session.query(Album)\ .join(Track)\ .join(Track.status)\ .filter(Status.imported == True) \ .filter(Status.available == True) \ .group_by(Album.id) order_by_column = Album.title if sorted_by == "title" else Album.artist if sort_order == "desc": q = q.order_by(desc(order_by_column)) else: q = q.order_by(asc(order_by_column)) if offset > 0: q = q.offset(offset) if limit > 0: q = q.limit(limit) return q.all()
def get_or_create(name: str) -> Artist: artist = db_session.query(Artist).filter(Artist.name == name).first() if artist is None: with persistance(): artist = Artist(name=name) db_session.add(artist) return artist
def on_playlist(playlist_id: int) -> List[Track]: """ gets List of Tracks to given playlist_id :param playlist_id: int to identify Playlist to get Tracks of :return: List of Tracks from Playlist """ tracks = [] q = db_session.query(PlaylistTrack).filter(PlaylistTrack.playlist_id == playlist_id).all() for p_track in q: tracks.append(p_track.track) return tracks
def exists(title: str, artist: str, album: str) -> bool: t = db_session.query(Track) \ .join(Artist) \ .join(Album) \ .filter(Track.title == title) \ .filter(Album.title == album) \ .filter(Artist.name == artist) \ .first() return t is not None
def get_or_create(title: str, artist: str) -> Album: a = artists.get_or_create(artist) album = db_session.query(Album)\ .filter(Album.title == title)\ .filter(Album.artist == a) \ .first() if album is None: with persistance(): album = Album(title=title) album.artist = a db_session.add(album) return album
def get_or_create(title: str, artist: str, album: str) -> Track: a = albums.get_or_create(album, artist) t = db_session.query(Track)\ .filter(Track.title == title)\ .filter(Track.album == a)\ .filter(Track.artist == a.artist)\ .first() if t is None: with persistance(): t = Track(title=title) t.album = a t.artist = t.album.artist db_session.add(t) if t.status is None: with persistance(): s = Status(t) db_session.add(s) return t
def get(album_id: int) -> Album: return db_session.query(Album).get(album_id)
def get(artist_id: int) -> Artist: return db_session.query(Artist).get(artist_id)
def get(track_id: int) -> Track: return db_session.query(Track).get(track_id)
def get(username: str) -> User: return db_session.query(User).get(username)