def create(businessUnitDetails): """ Creates a new businessUnit in the businessUnit list based on the passed in businessUnit data :param businessUnit: businessUnit to create in businessUnit structure :return: 201 on success, 406 on businessUnit exists """ # Remove id as it's created automatically if "id" in businessUnitDetails: del businessUnitDetails["id"] # Does the cd exist already? existing_businessUnit = (db.session.query(BusinessUnit).filter( BusinessUnit.name == businessUnitDetails["name"]).one_or_none()) if existing_businessUnit is None: schema = BusinessUnitSchema() new_businessUnit = schema.load(businessUnitDetails, session=db.session) db.session.add(new_businessUnit) db.session.commit() # Serialize and return the newly created deployment # in the response data = schema.dump(new_businessUnit) app.logger.debug(data) return data, 201 # Otherwise, it already exists, that's an error else: abort( 406, f"BusinessUnit with name {businessUnitDetails['name']} already exists" )
def create(businessUnitDetails): """ This function creates a new businessUnit in the businessUnit list based on the passed in businessUnit data :param businessUnit: businessUnit to create in businessUnit structure :return: 201 on success, 406 on businessUnit exists """ key = businessUnitDetails.get("key", None) # Does the cd exist already? existing_businessUnit = (BusinessUnit.query.filter( BusinessUnit.key == key).one_or_none()) if existing_businessUnit is None: schema = BusinessUnitSchema() new_businessUnit = schema.load(businessUnitDetails, session=db.session) db.session.add(new_businessUnit) db.session.commit() # Serialize and return the newly created deployment # in the response data = schema.dump(new_businessUnit) app.logger.debug(data) return data, 201 # Otherwise, it already exists, that's an error else: abort(406, f"BusinessUnit already exists")
def update(oid, businessUnitDetails): """ Updates an existing businessUnit in the businessUnit list :param key: id of the businessUnit to update in the businessUnit list :param businessUnit: businessUnit to update :return: updated businessUnit. """ if businessUnitDetails["id"] != oid: abort(400, "id mismatch in path and body") # Does the businessUnit exist in businessUnit list? existing_businessUnit = (db.session.query(BusinessUnit).filter( BusinessUnit.id == oid).one_or_none()) # Does businessUnit exist? if existing_businessUnit is not None: schema = BusinessUnitSchema() update_businessUnit = schema.load(businessUnitDetails, session=db.session) update_businessUnit.id = existing_businessUnit.id db.session.merge(update_businessUnit) db.session.commit() # return the updted deployment in the response data = schema.dump(update_businessUnit) app.logger.debug(data) return data, 200 # otherwise, nope, businessUnit doesn't exist, so that's an error else: abort(404, f"BusinessUnit with id {oid} not found")
def update(key, businessUnit): """ This function updates an existing businessUnit in the businessUnit list :param key: key of the businessUnit to update in the businessUnit list :param businessUnit: businessUnit to update :return: updated businessUnit """ if businessUnit["key"] != key: abort(400, f"Key mismatch in path and body") # Does the businessUnit exist in businessUnit list? existing_businessUnit = BusinessUnit.query.filter( BusinessUnit.key == key).one_or_none() # Does businessUnit exist? if existing_businessUnit is not None: schema = BusinessUnitSchema() update_businessUnit = schema.load(businessUnit, session=db.session) update_businessUnit.key = existing_businessUnit.key db.session.merge(update_businessUnit) db.session.commit() # return the updted deployment in the response data = schema.dump(update_businessUnit) app.logger.debug(data) return data, 200 # otherwise, nope, businessUnit doesn't exist, so that's an error else: abort(404, f"BusinessUnit not found")