Exemple #1
0
def upload_api_handler():
    usr = auth()

    data = parse_req_body(['name', 'author', 'mtype', 'introduction', 'genre'])
    try:
        f = request.files['music_file']
    except:
        return fail(u'发布失败,请选择要上传的文件')

    music = Music()
    music.name = data['name']
    if not music.name: return fail(u'请填写音乐名称')
    music.author = data['author']
    music.type = data['mtype']
    music.username = usr.username
    music.introduction = data['introduction']

    music.file_path = './attachments/' + secure_filename(gen_random_string() +
                                                         f.filename[-70:])
    try:
        f.save(music.file_path)
    except:
        return fail(u'保存音乐文件失败,请重试')

    if data['author']:
        artist = Artist(data['author'])
        artist.load()
        artist.genre = data['genre']
    else:
        artist = Artist('')
        artist.load()
        artist.genre = ''

    artist.save()
    music.save()
    return succAndRedirect(u'成功发布', '/view/' + str(music.id))