Exemplo n.º 1
0
    def delete(cls):
        data = _datafile_parser.parse_args()

        current_user_id = get_jwt_identity()
        file = FileModel.find_by_name(name=data["file_name"],
                                      user_id=current_user_id)

        if not file:
            return {"msg": "The file was not found."}, 404

        file_path = file.path
        s3_storage.delete_file(data["file_name"], current_user_id, file_path)

        user = UserModel.find_by_id(current_user_id)
        user.decrement_files_size(file.size)

        file.delete_from_db()
        return {"msg": "The file was deleted successfully."}, 200
Exemplo n.º 2
0
    def post(cls):
        data = _databooks_parser.parse_args()

        current_user_id = get_jwt_identity()
        databook = DatabookModel.find_by_name(data["databook_name"], current_user_id)

        if not databook:
            return {"msg": "No databook with that name found."}, 404

        # below line gets the required data file from s3 if already not present locally
        file = FileModel.find_by_name(databook.file_name, current_user_id)

        if not file:
            return {
                       "msg": "The data-file with the name - {}, for this databook is not found.".format(
                           databook.file_name)
                   }, 404

        user = UserModel.find_by_id(current_user_id)

        allowed_size = (configs.allowed_size[user.account_type]) // 30
        s3_storage.get_file_to_local(databook.file_name, current_user_id, file.path, file.size, allowed_size)

        # think of unpickling logic
        if databook.json()["obj_created"]:
            ret_data = databook_action_handler.unpickle_databook(user_id=current_user_id,
                                                                 databook_name=data["databook_name"])
        else:
            ret_data = databook_action_handler.load_databook(user_id=current_user_id,
                                                             file_path="{}/data/{}/{}/{}".format(volume,
                                                                                                 current_user_id,
                                                                                                 file.path,
                                                                                                 databook.file_name),
                                                             databook_name=data["databook_name"],
                                                             datastore_name=data["store_name"])

        return ret_data, 200
Exemplo n.º 3
0
    def post(cls):
        data = _datafile_parser.parse_args()

        current_user_id = get_jwt_identity()
        user = UserModel.find_by_id(current_user_id)

        links = list()
        total_size = 0

        # "files in data[] contains a list/array of files object/dict which contains file name and size
        for file_obj in json.loads(data["files"]):
            file = FileModel.find_by_name(name=file_obj["name"],
                                          user_id=current_user_id)

            if file:
                return {"msg": "A file with that name already exists!"}, 403

            if user.files_size + file_obj["size"] >= configs.allowed_size[
                    user.account_type]:
                return {"msg": "Not enough space to upload!"}, 403

            # links is a list of upload link, so it is appended with link for each file
            links.append(
                s3_storage.get_upload_url(file_obj["name"], current_user_id,
                                          data["file_path"]))

            file = FileModel(file_obj["name"], current_user_id,
                             data["file_path"], file_obj["size"])
            file.save_to_db()

            total_size += file_obj["size"]

        # updating the total size of files the user currently has
        user.increment_files_size(total_size)

        return {"links": links}, 200