def list_inventories(): """ Returns a list of all inventories in the inventory GET /inventory """ app.logger.info("A GET request for ALL inventories") inventories = [] params = request.args if len(params) > 0: pid = params.get("product_id") if pid: inventories = Inventory.find_by_product_id(pid) else: return bad_request( "Invalid request parameters: missing (product_id)") else: inventories = Inventory.all() results = [] for inv in inventories: json = inv.serialize() # if json['available'] == 1: results.append(json) if len(results) == 0: return not_found("Inventories were not found") app.logger.info("Returning {} inventories".format(len(results))) return make_response(jsonify(results), status.HTTP_200_OK)
def test_find_by_product_id(self): inventory = Inventory(product_id=444, condition="used", quantity=1, restock_level=10, available=1) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventory = Inventory(product_id=444, condition="new", quantity=0, restock_level=10, available=0) if not Inventory.find_by_product_id_condition(inventory.product_id, inventory.condition): inventory.create() inventories = Inventory.find_by_product_id(444) self.assertEqual(len(list(inventories)), 2)
def get(self): """ Returns a collection of the inventory records """ msg = "A GET request for ALL inventories." inventories = [] params = inventory_args.parse_args() if params[keys.KEY_PID]: pid = params[keys.KEY_PID] query = 'Filtering by category: {}'.format(params[keys.KEY_PID]) app.logger.info(query) msg = "{} {}".format(msg, query) inventories = Inventory.find_by_product_id(pid) elif params[keys.KEY_CND]: cnd = params[keys.KEY_CND] query = 'Filtering by category: {}'.format(params[keys.KEY_CND]) app.logger.info(query) msg = "{} {}".format(msg, query) inventories = Inventory.find_by_condition(cnd) elif params[keys.KEY_QTY] and params[keys.KEY_QTY] >= 0: qty = params[keys.KEY_QTY] query = 'Filtering by category: {}'.format(params[keys.KEY_QTY]) app.logger.info(query) msg = "{} {}".format(msg, query) inventories = Inventory.find_by_quantity(qty) elif params[keys.KEY_AVL] in [ keys.AVAILABLE_TRUE, keys.AVAILABLE_FALSE ]: avl = params[keys.KEY_AVL] query = 'Filtering by category: {}'.format(params[keys.KEY_AVL]) app.logger.info(query) msg = "{} {}".format(msg, query) inventories = Inventory.find_by_available(avl) else: inventories = Inventory.find_all() app.logger.info(msg) results = [inv.serialize() for inv in inventories] app.logger.info("Returning {} inventories".format(len(results))) return results, status.HTTP_200_OK