def post(self): """ Creates a Inventory This endpoint will create a Inventory based the data in the body that is posted """ try: app.logger.info("Request to create an Inventory record") inventory = Inventory() inventory.deserialize(api.payload) inventory.validate_data() if Inventory.find_by_product_id_condition( api.payload[keys.KEY_PID], api.payload[keys.KEY_CND]): api.abort( status.HTTP_409_CONFLICT, "Inventory with ({}, {})".format(inventory.product_id, inventory.condition)) inventory.create() location_url = api.url_for(InventoryResource, product_id=inventory.product_id, condition=inventory.condition, _external=True) app.logger.info("Inventory ({}, {}) created."\ .format(inventory.product_id, inventory.condition)) return inventory.serialize(), status.HTTP_201_CREATED, { 'Location': location_url } except DataValidationError as err: api.abort(status.HTTP_400_BAD_REQUEST, err)
def create_inventory(): """ Creates a new inventory in the Inventory DB based the data in the body POST /inventory """ app.logger.info("Request to create an Inventory record") check_content_type("application/json") json = request.get_json() inventory = Inventory() inventory.deserialize(json) inventory.validate_data() if Inventory.find(json['product_id'], json['condition']): return create_conflict_error( "The Record you're trying to create already exists!") inventory.create() location_url = url_for("get_inventory_by_pid_condition",\ product_id=inventory.product_id, condition=inventory.condition, _external=True) app.logger.info("Inventory ({}, {}) created."\ .format(inventory.product_id, inventory.condition)) return make_response(jsonify(inventory.serialize()), status.HTTP_201_CREATED, {"Location": location_url})
def call_validate_data(self,pid,cnd,qty,lvl,avl,res): inventory = Inventory(product_id=pid, condition=cnd, quantity=qty, restock_level=lvl, available=avl) self.assertTrue(inventory != None) res_pid = inventory.validate_data_product_id() self.assertEqual(res_pid,res) res_cnd = inventory.validate_data_condition() self.assertEqual(res_cnd,res) res_qty = inventory.validate_data_quantity() self.assertEqual(res_qty,res) res_lvl = inventory.validate_data_restock_level() self.assertEqual(res_lvl,res) res_avl = inventory.validate_data_available() self.assertEqual(res_avl,res) try: err = inventory.validate_data() except DataValidationError as err: print(err)