def add_type(): from bson import json_util type_manager = TypeManager(database_manager=current_app.database_manager) error_collection = {} upload = request.form.get('uploadFile') new_type_list = json.loads(upload, object_hook=json_util.object_hook) for new_type_data in new_type_list: try: new_type_data['public_id'] = object_manager.get_new_id(TypeModel.COLLECTION) new_type_data['creation_time'] = datetime.now(timezone.utc) except TypeError as e: LOGGER.warning(e) return abort(400) try: type_instance = TypeModel.from_data(new_type_data) except CMDBError: return abort(400) try: type_manager.insert(type_instance) except Exception as ex: error_collection.update({"public_id": type_instance.public_id, "message": ex.message}) resp = make_response(error_collection) return resp
def insert_type(data: dict): """ HTTP `POST` route for insert a single type resource. Args: data (TypeModel.SCHEMA): Insert data of a new type. Raises: ManagerGetError: If the inserted resource could not be found after inserting. ManagerInsertError: If something went wrong during insertion. Returns: InsertSingleResponse: Insert response with the new type and its public_id. """ type_manager = TypeManager(database_manager=current_app.database_manager) data.setdefault('creation_time', datetime.utcnow()) try: result_id: PublicID = type_manager.insert(data) raw_doc = type_manager.get(public_id=result_id) except ManagerGetError as err: return abort(404, err.message) except ManagerInsertError as err: return abort(400, err.message) api_response = InsertSingleResponse(result_id, raw=TypeModel.to_json(raw_doc), url=request.url, model=TypeModel.MODEL) return api_response.make_response(prefix='types')