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 call_deserialize(self,pid,cnd,qty,lvl,avl,err): data = { keys.KEY_PID: pid, keys.KEY_CND: cnd, keys.KEY_QTY: qty, keys.KEY_LVL: lvl, keys.KEY_AVL: avl } inventory = Inventory() inventory.deserialize(data) if err==1: self.assertRaises(DataValidationError, inventory.validate_data) self.assertNotEqual(inventory, None) self.assertEqual(inventory.product_id, pid) self.assertEqual(inventory.condition, cnd) self.assertEqual(inventory.quantity, qty) self.assertEqual(inventory.restock_level, lvl) self.assertEqual(inventory.available, avl)
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})