def __init__(self, request): self.sort = request.args.get('sort', 'asc') self.limit = request.args.get('limit', 3) self.search_date = request.args.get('date', '2019-03-01') self.orders_controller = OrdersController() self.products_controller = ProductsController() self.providers_controller = ProvidersController()
def new_inventory(self): sold_products = self.products_controller.get_sold_products() inventory = self.products_controller.get_inventory() if not sold_products or not inventory: return inventory product_list = ProductsController.get_products_list(sold_products) return ProductsController.calculate_inventory(inventory, product_list)
class ProductsApi(object): def __init__(self, request): self.sort = request.args.get('sort', 'asc') self.limit = request.args.get('limit', 3) self.search_date = request.args.get('date', '2019-03-01') self.orders_controller = OrdersController() self.products_controller = ProductsController() self.providers_controller = ProvidersController() @flask_request def sold_products(self): return self.products_controller.get_sold_products( self.search_date, self.sort, self.limit) @flask_request def new_inventory(self): sold_products = self.products_controller.get_sold_products() inventory = self.products_controller.get_inventory() if not sold_products or not inventory: return inventory product_list = ProductsController.get_products_list(sold_products) return ProductsController.calculate_inventory(inventory, product_list) @flask_request def organize_from_inventory(self): orders, inventory_list = self.calc_products_to_organize() return ProductsController.list_products(orders, inventory_list, type="inventory") @flask_request def organize_from_providers(self): orders, inventory_list = self.calc_products_to_organize() orders_complete = ProductsController.list_products(orders, inventory_list, type="providers") if orders_complete: orders_complete = self.providers_controller.set_product_providers( orders_complete) return orders_complete def calc_products_to_organize(self): orders = self.orders_controller.get_complete_grouped_orders() inventory = self.products_controller.get_inventory() if not orders or not inventory: return [] inventory_list = ProductsController.get_inventory_list(inventory) return orders, inventory_list
def test_get_products_list(): response = ProductsController.get_products_list(ORDER_PRODUCTS) assert response == { ORDER_PRODUCTS[0]['product_id']: ORDER_PRODUCTS[0]["quantity"], ORDER_PRODUCTS[1]['product_id']: ORDER_PRODUCTS[1]["quantity"], ORDER_PRODUCTS[2]['product_id']: ORDER_PRODUCTS[2]["quantity"] }
def test_clean_product_info(): response = ProductsController.clean_product_info(ORDER_PRODUCTS[2]) assert response == { "id": ORDER_PRODUCTS[2]['product_id'], "name": ORDER_PRODUCTS[2]['name'], "provider_id": None, "quantity": ORDER_PRODUCTS[2]['quantity'] }
def calc_products_to_organize(self): orders = self.orders_controller.get_complete_grouped_orders() inventory = self.products_controller.get_inventory() if not orders or not inventory: return [] inventory_list = ProductsController.get_inventory_list(inventory) return orders, inventory_list
def test_get_stock_information(): quantity = 5 product_info = ORDER_PRODUCTS[1] response = ProductsController.get_stock_info(product_info, quantity) assert response == { "product_id": product_info['product_id'], "name": product_info['name'], "quantity": quantity }
def test_calculate_inventory(): products_list = {10: 8, 9: 3, 8: 1} response = ProductsController.calculate_inventory(ORDER_PRODUCTS, products_list) assert response == [{ **ORDER_PRODUCTS[0], "quantity": 22 }, { **ORDER_PRODUCTS[1], "quantity": 0 }, { **ORDER_PRODUCTS[2], "quantity": 2 }]
def organize_from_providers(self): orders, inventory_list = self.calc_products_to_organize() orders_complete = ProductsController.list_products(orders, inventory_list, type="providers") if orders_complete: orders_complete = self.providers_controller.set_product_providers( orders_complete) return orders_complete
def test_get_inventory_list(): inventory = [{ "id": 1, "quantity": 4 }, { "id": 10, "quantity": 34 }, { "id": 8, "quantity": 89 }] response = ProductsController.get_inventory_list(inventory) assert response == {1: 4, 10: 34, 8: 89}
class OrdersApi(object): def __init__(self, request): self.orders_controller = OrdersController() self.products_controller = ProductsController() @flask_request def get_order(self, order_id): return self.orders_controller.get_order_by_id(order_id) @flask_request def order_products_stock(self, order_id): order_products = self.products_controller.get_products(order_id) in_stock, out_of_stock = ProductsController.get_stock_products( order_products) return {"in_stock": in_stock, "out_of_stock": out_of_stock}
def __init__(self, request): self.orders_controller = OrdersController() self.products_controller = ProductsController()
def order_products_stock(self, order_id): order_products = self.products_controller.get_products(order_id) in_stock, out_of_stock = ProductsController.get_stock_products( order_products) return {"in_stock": in_stock, "out_of_stock": out_of_stock}
def test_get_stock_information_missing_data(): response = ProductsController.get_stock_info() assert response == {"product_id": 0, "name": "", "quantity": 0}
def organize_from_inventory(self): orders, inventory_list = self.calc_products_to_organize() return ProductsController.list_products(orders, inventory_list, type="inventory")