示例#1
0
def add_single(artist_id):

    try:
        file_data = get_posted_audio(request)
    except admin.exceptions.InvalidData as err:
        return J(to_error_object(err.message)), 400

    # get artist
    artist = admin.artists.by_id_for_owner(artist_id, current_user_email())
    if not artist:
        raise Forbidden

    # persist file
    single_title, extension = os.path.splitext(file_data.filename)
    safe_title = admin.names.safe_obj_name(single_title)
    object_name = 'art/{0}-{1}/{2}-{3}{4}'.format(artist.normalized_name,
                                                  artist.id, safe_title,
                                                  ids.new_id()[:8], extension)
    url = admin.files.save(file_data,
                           object_name,
                           content_type=admin.files.content_types.get(
                               extension.lower()))

    try:
        single = admin.singles.create(artist,
                                      single_title=single_title,
                                      audio_url=url)
    except admin.exceptions.ModelException as err:
        return J(to_error_object(err.message)), 422

    data = SingleSchema().dump(single)
    return J(data)
示例#2
0
def add_artist_image(artist_id):

    try:
        file_data = get_posted_image(request)
    except admin.exceptions.InvalidData as err:
        return J(to_error_object(err.message)), 400

    # get artist
    artist = admin.artists.by_id_for_owner(artist_id, current_user_email())
    if not artist:
        raise Forbidden

    # persist image
    object_name = 'art/{0}-{1}/{0}-{2}.jpg'.format(artist.normalized_name,
                                                   artist.id,
                                                   ids.new_id()[:8])
    admin.files.save(file_data,
                     object_name,
                     content_type=admin.files.content_types['.jpg'])
    if artist.image_url:
        admin.files.delete(artist.image_url)

    # update artist with new image_url
    admin.artists.update_image_url(artist_id, object_name)

    # return entity with new profile_url
    artist.profile_image_url = get_resized_image_url(object_name, '300')
    data = ArtistSchema().dump(artist)
    return J(data)
示例#3
0
def add(artist, data):
    """
    Adds an album to the artist.
    """

    album = Album(id=ids.new_id(), artist=artist, image_url='', **data)
    if not hasattr(album, 'license'):
        setattr(album, 'license', '')
    if not hasattr(album, 'description'):
        setattr(album, 'description', '')

    table = db.get_table()
    # get existing albums to check name and get next sort
    res = table.query(IndexName='IX_ARTIST_CONTENT',
                      ScanIndexForward=True,
                      KeyConditionExpression=Key('AC_PK').eq(artist.id)
                      & Key('AC_SK').begins_with('2'),
                      ProjectionExpression='PK, AA_PK, AC_SK, AlbumTitle')
    existing_albums = list(map(item_to_album, res['Items']))
    if any(album.title == existing.title for existing in existing_albums):
        raise exceptions.AlbumTitleExists

    if len(existing_albums) > 0:
        last_album = existing_albums[-1]
        last_sort = int(last_album.sort)
        album.sort = str(last_sort + 1)
    else:
        album.sort = '200'

    print("creating '{0}', id '{1}', sort {2}".format(album.title, album.id,
                                                      album.sort))

    table.put_item(Item=album_to_item(album))
    return album
示例#4
0
def create_track(album, track_title, audio_url):

    if len(album.tracks) > 0:

        if any(existing_track.title == track_title
               for existing_track in album.tracks):
            dedupe_num = 0
            test_title = track_title
            while any(existing_track.title == test_title
                      for existing_track in album.tracks):
                dedupe_num += 1
                test_title = track_title + ' ' + str(dedupe_num)
            track_title = test_title

        last_track = album.tracks[-1]
        last_sort = int(last_track.sort)
        sort = str(last_sort + 1)
    else:
        sort = '100'

    track = Track(id=ids.new_id(),
                  title=track_title,
                  audio_url=audio_url,
                  sort=sort,
                  album=album)

    item = track_to_item(track)
    table = db.get_table()
    table.put_item(Item=item)

    return track
示例#5
0
def create(artist, single_title, audio_url):
    existing_singles = list_for_artist(artist.id)
    if len(existing_singles) > 0:
        if any(ex.title == single_title for ex in existing_singles):
            dedupe_num = 0
            test_title = single_title
            while any(ex.title == test_title for ex in existing_singles):
                dedupe_num += 1
                test_title = single_title + ' ' + str(dedupe_num)
            single_title = test_title

        last = existing_singles[-1]
        last_sort = int(last.sort)
        sort = str(last_sort + 1)
    else:
        sort = DEFAULT_SORT

    single = Single(
        id=ids.new_id(),
        title=single_title,
        audio_url=audio_url,
        sort=sort,
        artist=artist,
        license=DEFAULT_LICENSE,
        release_date=datetime.datetime.utcnow().strftime('%Y-%m-%d'))

    item = single_to_item(single)
    table = db.get_table()
    table.put_item(Item=item)

    return single
示例#6
0
def create(data):
    """
    Creates a new artist from the supplied data, and saves to database.

    Parameters
    ----------
    data: dict
        Dictionary of attribute values.

    Returns
    -------
        The new Artist instance.
    """

    artist = Artist(id=ids.new_id(), **data)
    artist.normalized_name = names.safe_obj_name(artist.name)
    artist.image_url = ''

    if name_is_taken(artist.normalized_name):
        raise exceptions.NameIsTaken

    item = artist_to_item(artist)
    table = db.get_table()
    table.put_item(Item=item)
    return artist
示例#7
0
def add_album_track(artist_id, album_id):

    try:
        file_data = get_posted_audio(request)
    except admin.exceptions.InvalidData as err:
        return J(to_error_object(err.message)), 400

    # get artist
    artist = admin.artists.by_id_for_owner(artist_id, current_user_email())
    if not artist:
        raise Forbidden

    # get album
    album = admin.albums.get_by_id(album_id)
    if album.artist.id != artist_id:
        raise Forbidden

    # persist file
    track_title, extension = os.path.splitext(file_data.filename)
    safe_title = admin.names.safe_obj_name(track_title)
    object_name = 'art/{0}-{1}/{2}-{3}/{4}-{5}{6}'.format(
        artist.normalized_name, artist.id,
        admin.names.safe_obj_name(album.title), album.id, safe_title,
        ids.new_id()[:8], extension)
    url = admin.files.save(file_data,
                           object_name,
                           content_type=admin.files.content_types.get(
                               extension.lower()))

    try:
        track = admin.albums.create_track(album,
                                          track_title=track_title,
                                          audio_url=url)
    except admin.exceptions.ModelException as err:
        return J(to_error_object(err.message)), 422

    data = TrackSchema().dump(track)
    return J(data)
示例#8
0
def add_album_image(artist_id, album_id):

    try:
        file_data = get_posted_image(request)
    except InvalidData as err:
        return J(to_error_object(err.message)), 400

    # get artist
    artist = admin.artists.by_id_for_owner(artist_id, current_user_email())
    if not artist:
        raise Forbidden

    # get album
    album = admin.albums.get_by_id(album_id)
    if album.artist.id != artist_id:
        raise Forbidden

    # persist image
    object_name = 'art/{0}-{1}/{2}-{3}/{2}-{4}.jpg'.format(
        artist.normalized_name, artist.id,
        admin.names.safe_obj_name(album.title), album.id,
        ids.new_id()[:8])
    admin.files.save(file_data,
                     object_name,
                     content_type=admin.files.content_types['.jpg'])
    # delete old file, if exists
    if album.image_url:
        admin.files.delete(album.image_url)

    # update album with new image_url
    admin.albums.update_image_url(artist_id=artist.id,
                                  album_id=album.id,
                                  image_url=object_name)

    # return entity with new profile_url
    album.profile_image_url = get_resized_image_url(object_name, '300x300')
    data = AlbumSchema().dump(album)
    return J(data)