Пример #1
0
def remove(playlist_id: int) -> bool:
    """
    removes Playlist by given playlist_id
    :param playlist_id: unique int of Playlist to remove
    :return: succeed?
    """
    playlist = db_session.query(Playlist).filter(Playlist.id == playlist_id)
    if playlist is None:
        return False
    else:
        with persistance():
            db_session.query(Playlist).filter(
                Playlist.id == playlist_id).delete()
        return True
Пример #2
0
def get(p_id: int):
    """

    :param p_id: Unique Id of Playlist to get.
    :return: Playlist
    """
    return db_session.query(Playlist).get(p_id)
Пример #3
0
def all(offset: int = 0,
        limit: int = 0,
        sorted_by: str = "id",
        sort_order: str = "asc") -> List[Playlist]:
    """
    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 (id, playlist_name, user_name, username)
    :param offset: The e.g. id of the track to start from
    :param limit: The number of tracks to return
    :return: All non hidden tracks in the database
    """
    q = db_session.query(Playlist)

    if sorted_by == "id":
        col_order = Playlist.id
    elif sorted_by == "playlist_name":
        col_order = Playlist.name
    elif sorted_by == "username":
        col_order = Playlist.user
    else:
        col_order = Playlist.username

    if sort_order == "desc":
        q = q.order_by(desc(col_order))
    else:
        q = q.order_by(asc(col_order))

    if offset > 0:
        q = q.offset(offset)

    if limit > 0:
        q = q.limit(limit)

    return q.all()
Пример #4
0
def remove_track(playlist_id: int, track_id: int) -> bool:
    """
    removes Track by id in Playlist by id
    :param playlist_id: unique int of Playlist to remove
    :param track_id: unique int of track to remove
    :return: succeed?
    """
    playlist_track = db_session.query(PlaylistTrack). \
        filter(PlaylistTrack.playlist_id == playlist_id). \
        filter(PlaylistTrack.track_id == track_id).first()
    if playlist_track is None:
        return False

    with persistance():
        db_session.query(PlaylistTrack).filter(PlaylistTrack.id == playlist_track.id).\
                filter(PlaylistTrack.track_id == track_id).delete()
    return True
Пример #5
0
def from_user(username: str) -> List[Playlist]:
    """

    :param username: name of User to get List of Playlist from.
    :return: List of Playlist
    """
    playlist = db_session.query(Playlist).filter(
        Playlist.username == username).all()

    if playlist is None:
        return []

    return [p for p in playlist]