示例#1
0
def upload_track(artist_name):
    artist = Artist.objects(name=artist_name).first_or_404()
    if current_user not in artist.members:
        flash('You are not authorized to upload tracks for {}'.format(
            artist.name))
        return redirect(url_for('main.artist', artist_name=artist.name))
    form = UploadTrackForm()
    form.genre.choices = [(genre.id, genre.name)
                          for genre in Genre.objects.order_by('name')]
    if form.validate_on_submit():
        file = form.track.data
        if not allowed_file(file.filename):
            flash('Only music files are allowed')
            return render_template('upload_track.html',
                                   form=form,
                                   artist=artist,
                                   title='Upload Track')
        file.filename = secure_filename(file.filename)
        s3_filepath, cloudfront_filepath = upload_file_to_s3(
            file, current_app.config['S3_BUCKET'])
        track_exists = Track.objects(s3_filepath=s3_filepath,
                                     cloudfront_filepath=cloudfront_filepath
                                     ).first() is not None  # 1 if track exists
        if s3_filepath is not None and cloudfront_filepath is not None and not track_exists:
            new_track = Track(track_name=form.track_name.data,
                              artist=artist,
                              duration=0,
                              genre=[
                                  Genre.objects(id=genre).first()
                                  for genre in form.genre.data
                              ],
                              s3_filepath=s3_filepath,
                              cloudfront_filepath=cloudfront_filepath)
            new_track.save(cascade=True)  # Saves the track in the mongoDB

            # filepath = os.path.join(current_app.config['UPLOAD_FOLDER'], file.filename)
            # new_track = Track(track_name=form.track_name.data, artist=artist, duration=0,
            #                   genre=[Genre.objects(id=genre).first() for genre in form.genre.data], filepath=filepath)
            # new_track.save(cascade=True)  # Saves the track in the mongoDB
            # file.save(filepath)
            artist.tracks.append(new_track)
            artist.save(cascade=True)  # Save artist with new track
            flash('Track successfully uploaded')
            return redirect(url_for('main.artist', artist_name=artist_name))
        else:
            if track_exists:
                flash('Track already exists!')
                return render_template('upload_track.html',
                                       form=form,
                                       artist=artist,
                                       title='Upload Track')
            else:
                flash('Error uploading file')
                return render_template('upload_track.html',
                                       form=form,
                                       artist=artist,
                                       title='Upload Track')
    return render_template('upload_track.html',
                           form=form,
                           artist=artist,
                           title='Upload Track')
示例#2
0
def get_track(track_name):
    track = Track.objects(track_name=track_name).first_or_404()
    track_url = track.filepath
    return jsonify({'track_url': track_url})