예제 #1
0
def list_inventory():
    """ Returns all of the inventory """
    app.logger.info('Request for inventory list')
    inventories = []
    restock = request.args.get('restock')
    restock_level = request.args.get('restock-level')
    condition = request.args.get('condition')
    product_id = request.args.get('product-id')
    available = request.args.get('available')
    if restock:
        if restock == "true":
            inventories = Inventory.find_by_restock(True)
        elif restock == "false":
            inventories = Inventory.find_by_restock(False)
    elif restock_level:
        inventories = Inventory.find_by_restock_level(restock_level)
    elif condition:
        if product_id:
            inventories = Inventory.find_by_condition_with_pid(
                condition, product_id)
        elif not product_id:
            inventories = Inventory.find_by_condition(condition)
    elif product_id:
        inventories = Inventory.find_by_product_id(product_id)
    elif available:
        if available == 'true':
            inventories = Inventory.find_by_availability(True)
        elif available == 'false':
            inventories = Inventory.find_by_availability(False)
    else:
        inventories = Inventory.all()
    results = [e.serialize() for e in inventories]
    return make_response(jsonify(results), status.HTTP_200_OK)
예제 #2
0
 def test_find_an_inventory_by_availability(self):
     """ Find an inventory by availability """
     Inventory(product_id=1,
               quantity=100,
               restock_level=50,
               condition="new",
               available=False).save()
     Inventory(product_id=1,
               quantity=50,
               restock_level=50,
               condition="used",
               available=False).save()
     Inventory(product_id=2,
               quantity=10,
               restock_level=20,
               condition="used",
               available=False).save()
     Inventory(product_id=2,
               quantity=21,
               restock_level=20,
               condition="used",
               available=True).save()
     inventory = Inventory.find_by_availability(True)
     self.assertEqual(len(inventory), 1)
     self.assertEqual(inventory[0].product_id, 2)
     self.assertEqual(inventory[0].quantity, 21)
     self.assertEqual(inventory[0].restock_level, 20)
     self.assertEqual(inventory[0].condition, "used")
     self.assertEqual(inventory[0].available, True)
     inventory = Inventory.find_by_availability_with_pid(False, 1)
     self.assertEqual(len(inventory), 2)
예제 #3
0
    def get(self):
        """ Returns all of the inventory """
        app.logger.info('Request for inventory list')
        inventories = []
        args = inventory_args.parse_args()
        restock = args['restock']
        restock_level = args['restock-level']
        condition = args['condition']
        product_id = args['product-id']
        available = args['available']
        args_len = len(request.args)

        message_invalid_fields = \
        'Only accept query by product-id, available, ' \
        + 'product-id & availabe, condition, product-id & condition, ' \
        + 'restock-level, restock (list all the inventory that need ' \
        + 'to be restocked).'
        message_condition_empty = '{} can\'t be empty'.format('condition')
        message_condition_invalid = '{} must be new, open_box, used'\
        .format('condition')

        if args_len is 0:
            inventories = Inventory.all()
        elif args_len is 1:
            if product_id is not None:
                inventories = Inventory.find_by_product_id(int(product_id))
            elif restock is not None:
                inventories = Inventory.find_by_restock(restock)
            elif restock_level is not None:
                inventories = Inventory.find_by_restock_level\
                (int(restock_level))
            elif condition is not None:
                if condition is '':
                    api.abort(400, message_condition_empty)
                elif condition not in ('new', 'open_box', 'used'):
                    api.abort(400, message_condition_invalid)
                else:
                    inventories = Inventory.find_by_condition(condition)
            elif available is not None:
                inventories = Inventory.find_by_availability(available)
            else:
                api.abort(400, message_invalid_fields)
        elif args_len is 2:
            if condition is not None and product_id is not None:
                if condition is '':
                    api.abort(400, message_condition_empty)
                elif condition not in ('new', 'open_box', 'used'):
                    api.abort(400, message_condition_invalid)
                else:
                    inventories = Inventory.find_by_condition_with_pid(
                        condition, int(product_id))
            elif available is not None and product_id is not None:
                inventories = Inventory.\
                find_by_availability_with_pid(available, int(product_id))
            else:
                api.abort(400, message_invalid_fields)
        else:
            api.abort(400, message_invalid_fields)
        results = [e.serialize() for e in inventories]
        return results, status.HTTP_200_OK