コード例 #1
0
ファイル: service.py プロジェクト: 602071349/inventories
def update_inventory_restock(product_id, condition):
    """- Given the product_id, condition and amount (body) this updates quantity += amount"""
    app.logger.info("Request to update inventory with key ({}, {})"\
                    .format(product_id, condition))

    # Checking for Content-Type
    check_content_type("application/json")

    # Checking for 'amount' keyword
    json = request.get_json()
    print(json.keys())
    if "amount" not in json.keys():
        return bad_request("Invalid data: Amount missing")

    # Checking for amount >= 0
    amount = json['amount']
    if amount < 0:
        return forbidden("Invalid data: Amount <= 0")

    # If there is no matching inventory
    inventory = Inventory.find(product_id, condition)
    if not inventory:
        return not_found("Inventory with ({}, {})".format(
            product_id, condition))

    inventory.quantity += amount

    inventory.validate_data()
    inventory.update()
    app.logger.info("Inventory ({}, {}) updated.".format(
        product_id, condition))
    return make_response(jsonify(inventory.serialize()), status.HTTP_200_OK)
コード例 #2
0
ファイル: service.py プロジェクト: 602071349/inventories
def update_inventory(product_id, condition):
    """
    Regular Update
    Updates the inventory with the given product_id and condition
    """
    app.logger.info("Request to update inventory with key ({}, {})"\
                    .format(product_id, condition))
    check_content_type("application/json")
    inventory = Inventory.find(product_id, condition)
    if not inventory:
        return not_found("Inventory with ({}, {})".format(
            product_id, condition))

    resp_old = inventory.serialize()
    resp_new = request.get_json()
    for key in resp_old.keys():
        if key in resp_new:
            resp_old[key] = resp_new[key]

    if inventory.quantity == 0:
        inventory.available = 0

    inventory.deserialize(resp_old)
    inventory.validate_data()
    inventory.update()
    app.logger.info("Inventory ({}, {}) updated.".format(
        product_id, condition))
    return make_response(jsonify(inventory.serialize()), status.HTTP_200_OK)
コード例 #3
0
ファイル: service.py プロジェクト: 602071349/inventories
def delete_inventory(product_id, condition):
    """Deletes an inventory with the given product_id and condition"""
    inventory = Inventory.find(product_id, condition)
    app.logger.info("Request to delete inventory with key ({}, {})"\
                    .format(product_id, condition))
    if inventory:
        inventory.delete()
    app.logger.info(
        "Inventory with product_id {} and condition {} deleted".format(
            product_id, condition))
    return make_response("", status.HTTP_204_NO_CONTENT)
コード例 #4
0
ファイル: service.py プロジェクト: 602071349/inventories
def get_inventory_by_pid_condition(product_id, condition):
    """
    Returns the inventory with the given product_id and condition
    GET /inventory/<int:product_id>/condition/<string:condition>
    """
    app.logger.info("A GET request for inventories with product_id {} and condition {}"\
                    .format(product_id, condition))
    inventory = Inventory.find(product_id, condition)
    # if (not inventory) or\
    # (inventory and inventory.serialize()['available'] == 0):
    if not inventory:
        return not_found("Inventory ({}, {})".format(product_id, condition))
    app.logger.info("Return inventory with product_id {} and condition {}"\
                    .format(product_id, condition))
    return make_response(jsonify(inventory.serialize()), status.HTTP_200_OK)
コード例 #5
0
ファイル: service.py プロジェクト: 602071349/inventories
def update_inventory_deactivate(product_id, condition):
    """Given the product_id and condition this updates available = 0"""
    app.logger.info("Request to update inventory with key ({}, {})"\
                    .format(product_id, condition))
    inventory = Inventory.find(product_id, condition)
    if not inventory:
        return not_found("Inventory with ({}, {})".format(
            product_id, condition))

    inventory.available = 0
    inventory.validate_data()
    inventory.update()
    app.logger.info("Inventory ({}, {}) updated.".format(
        product_id, condition))
    return make_response(jsonify(inventory.serialize()), status.HTTP_200_OK)
コード例 #6
0
ファイル: service.py プロジェクト: 602071349/inventories
def update_inventory_activate(product_id, condition):
    """Given the product_id and condition this updates available = 1"""
    app.logger.info("Request to update inventory with key ({}, {})"\
                    .format(product_id, condition))
    inventory = Inventory.find(product_id, condition)
    if not inventory:
        return not_found("Inventory with ({}, {})".format(
            product_id, condition))

    if inventory.quantity == 0:
        return forbidden(
            "This product is currently out of stock and cannot be made available"
        )
    inventory.available = 1

    inventory.validate_data()
    inventory.update()
    app.logger.info("Inventory ({}, {}) updated.".format(
        product_id, condition))
    return make_response(jsonify(inventory.serialize()), status.HTTP_200_OK)
コード例 #7
0
ファイル: service.py プロジェクト: 602071349/inventories
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})