Пример #1
0
 def post(self):
     """
     Create the room
     """
     data = room_parser.parse_args()
     if 'song' not in request.files:
         return {
             "status": "error",
             'message': 'You have to upload at least one song'
         }, 400
     if Room.find_by_name(data['name']):
         return {
             "status": "error",
             'message': 'Room {} already exists'.format(data['name'])
         }, 400
     current_user = User.find_by_username(get_jwt_identity())
     song = request.files["song"]
     if song.filename == "":
         return {"status": "error", 'message': "Please select a file"}, 400
     elif not (song and allowed_file(song.filename)):
         return {
             "status": "error",
             'message': "Files must be in mp3 format."
         }, 400
     else:
         link = None
         try:
             room = Room(name=data['name'])
             db.session.add(room)
             song_name = secure_filename(song.filename)
             link = upload_file_to_s3(song, config.S3_BUCKET)
             db.session.flush()
             db.session.execute(joins.insert().values(
                 user_id=current_user.id, room_id=room.id))
             new_song = Song(name=song_name, link=link, room_id=room.id)
             room.current_song_id = new_song.id
             room.current_song = new_song
             room.owner_id = current_user.id
             db.session.add(new_song)
         except Exception as e:
             if link is not None:
                 s3.delete_object(Bucket=config.S3_BUCKET,
                                  Key=link[len(config.S3_LOCATION):])
             db.session.rollback()
             print(repr(e))
             return {
                 "status":
                 "error",
                 'message':
                 "Something wrong happen while creating room. Please try again."
             }, 400
         db.session.commit()
         result = room_schema.dump(room)
         return {"status": "success", "data": result}, 201