Example #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
Example #2
0
 def delete(self):
     file_name = request.get_json()['file_name']
     if file_name:
         if file_name == "all":
             contents = s3.list_objects(Bucket=config.S3_BUCKET)['Contents']
             if contents is None:
                 return "There are no files."
             for key in contents:
                 print(key['Key'])
                 s3.delete_object(Bucket=config.S3_BUCKET, Key=key['Key'])
         else:
             s3.delete_object(Bucket=config.S3_BUCKET, Key=file_name)
             return file_name + " deleted.", 200
     else:
         return "dont receive file_name", 200
Example #3
0
 def delete(self, room_id):
     current_user = User.find_by_username(get_jwt_identity())
     room = Room.query.filter_by(id=room_id).first()
     if room is None:
         return {"status": "error", 'message': 'Room does not exist.'}, 400
     elif current_user not in room.members:
         return {
             "status": "error",
             'message': 'You are not in this room.'
         }, 400
     elif room.members.count() == 1:
         # delete room + delete song + delete joins
         db.session.execute(
             delete(joins).where(joins.c.user_id == current_user.id).where(
                 joins.c.room_id == room_id))
         room.current_song_id = None
         for song in room.playlist:
             s3.delete_object(Bucket=config.S3_BUCKET,
                              Key=song.link[len(config.S3_LOCATION):])
         db.session.delete(room)
     else:
         if current_user is room.owner:
             # make some one owner , then delete join
             new_owner = room.members[1]
             room.owner_id = new_owner.id
             db.session.execute(
                 delete(joins).where(
                     joins.c.user_id == current_user.id).where(
                         joins.c.room_id == room_id))
             message = "User " + new_owner.username + " becomes room owner." \
                                                      " User " + current_user.username + " left the room."
         else:
             db.session.execute(
                 delete(joins).where(
                     joins.c.user_id == current_user.id).where(
                         joins.c.room_id == room_id))
             message = "User " + current_user.username + " left the room."
         socketio.emit('member', {
             'data': {
                 'room_id': room_id,
                 'action': 'member_leave',
                 'data': user_schema.dump(current_user),
                 'message': message
             }
         },
                       room=room_id)
     db.session.commit()
     return {"status": "success", "message": "You have left the room."}, 200
Example #4
0
def delete_profile_image(image_path, image_filename, s3_bucket_name=None):
    """
    Deletes the image stored on the app server or on Amazon S3 depending on the configuration.
    """
    if image_server_config == "amazon_s3":
        s3.delete_object(Bucket=s3_bucket_name,
                         Key=f"{image_path}{image_filename}")
    elif image_server_config == "app_server_storage":
        try:
            path_to_image = Path(
                f"{current_app.root_path}/base/static/{image_path}/{image_filename}"
            )
            os.remove(path_to_image)
        except FileNotFoundError:
            pass
    return "deletion task completed"
Example #5
0
 def post(self, room_id):
     song_file = request.files["song"]
     if song_file.filename == "":
         return {"status": "error", 'message': "Please select a file"}, 400
     elif not (song_file and allowed_file(song_file.filename)):
         return {
             "status": "error",
             'message': "Files must be in mp3 format."
         }, 400
     elif Room.query.filter_by(id=room_id).first() is None:
         return {"status": "error", "message": "Room does not exist."}, 400
     else:
         link = None
         try:
             song_name = secure_filename(song_file.filename)
             link = upload_file_to_s3(song_file, config.S3_BUCKET)
             new_song = Song(name=song_name, link=link, room_id=room_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 uploading new song. Please try again."
             }, 400
         db.session.commit()
         result = song_schema.dump(new_song)
         socketio.emit('playlist', {
             'data': {
                 'room_id': room_id,
                 'action': 'song_added',
                 'data': result,
                 'message': 'Song ' + song_name + ' has been added.'
             }
         },
                       room=room_id)
         return {"status": "success", "data": result}, 200
Example #6
0
def delete():

    if request.method == "POST":
        uuid = request.form.get("sheet_uuid")
        sheet = Sheet.query.filter_by(uuid=uuid).first()
        if not sheet:
            flash("Sheet not found. " + uuid, "error")
            return redirect(url_for("index"))

        images = Image.query.filter_by(sheet_id=sheet.id).all()

        if int(sheet.user_id) != int(current_user.get_id()):
            flash("You do not have permission to delete this sheet.", "error")
            return redirect(url_for("index"))

        for image in images:
            try:
                s3.delete_object(Bucket=Config.S3_BUCKET, Key=image.path)
            except ClientError as e:
                flash("Error while deleting files: " + e, "error")
                return redirect(url_for("index"))

        if sheet.pdf:
            pdf_path = sheet.uuid + "/" + secure_filename(sheet.name) + ".pdf"
            try:
                s3.delete_object(Bucket=Config.S3_BUCKET, Key=pdf_path)
            except ClientError as e:
                flash("Error while deleting pdf: " + e, "error")
                return redirect(url_for("index"))

        try:
            Image.query.filter_by(sheet_id=sheet.id).delete()
            Sheet.query.filter_by(uuid=uuid).delete()
            db.session.commit()
        except:
            flash("Database error while deleting sheet.", "error")
            return redirect(url_for("index"))

        flash("Successfully deleted " + sheet.name, "message")

    return redirect(url_for("index"))
Example #7
0
def delete_property_listing_images(images_location,
                                   image_path,
                                   images_folder,
                                   images_list,
                                   s3_bucket_name=None):
    """
    Deletes the image stored on the app server or on Amazon S3 depending on the configuration.
    """

    if images_location == "amazon_s3":
        for image_name in images_list[1:]:
            s3.delete_object(Bucket=s3_bucket_name,
                             Key=f"{image_path}{images_folder}{image_name}")
    elif images_location == "app_server_storage":
        try:
            path_to_image = Path(
                f"{current_app.root_path}/base/static/{image_path}/{images_folder}"
            )
            shutil.rmtree(path_to_image)
        except FileNotFoundError as error:
            print(error)
    return "deletion task completed"
Example #8
0
 def delete(self, room_id, song_id):
     if song_id is None:
         return {
             "status": "error",
             'message': "song_id can not be null"
         }, 400
     else:
         song = Song.query.filter_by(id=song_id).first()
         if song is None or song.room_id != room_id:
             return {
                 "status": "error",
                 'message': "The song is not exist."
             }, 400
         if Song.query.filter_by(room_id=room_id).count() == 1:
             return {
                 "status":
                 "error",
                 'message':
                 "The song is the last song in the playlist. You must have at least one song."
             }, 400
         s3.delete_object(Bucket=config.S3_BUCKET,
                          Key=song.link[len(config.S3_LOCATION):])
         db.session.delete(song)
         db.session.commit()
         socketio.emit('playlist', {
             'data': {
                 'room_id': room_id,
                 'action': 'song_deleted',
                 'song_id': song_id,
                 'message': 'Song ' + song.name + ' has been deleted.'
             }
         },
                       room=room_id)
         return {
             "status": "success",
             "message": song.name + " was deleted."
         }, 200