def change_sale_price_item():
    print("Changing Sale Price of Item")
    data = json.loads(request.data)

    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False}

    item_id = data["item_id"]
    sale_price = data["new_price"]
    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.ChangePrice(
                product_pb2.ChangePriceRequest(username=username,
                                               item_id=item_id,
                                               sale_price=sale_price))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
def remove_item_from_sale():
    print("Removing item")
    data = json.loads(request.data)

    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False}

    item_id = data["item_id"]
    quantity = data["quantity"]
    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.DeleteItem(
                product_pb2.DeleteItemRequest(username=username,
                                              item_id=item_id,
                                              quantity=quantity))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
def get_seller_rating():
    print("Getting Seller Rating")
    data = json.loads(request.data)
    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False, "thumbsup": -1, "thumbsdown": -1}
    seller_id = data["seller_id"]

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.GetRating(
                product_pb2.GetRatingRequest(seller_id=seller_id))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print(response.error)
    return {
        "status": response.status,
        "thumbsup": response.thumbsup,
        "thumbsdown": response.thumbsdown
    }
def put_item_for_sale():
    print("Putting item for sale")
    data = json.loads(request.data)
    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False, "item_id": -1}

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.CreateItem(
                product_pb2.CreateItemRequest(
                    username=username,
                    item_name=data["item"]["name"],
                    category=data["item"]["category"],
                    keywords=data["item"]["keywords"],
                    condition_new=data["item"]["condition_new"],
                    sale_price=data["item"]["sale_price"],
                    quantity=data["quantity"]))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status, "item_id": response.item_id}
def display_shopping_cart():
    print("Displaying shopping cart")
    data = json.loads(request.data)
    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False, "items": []}

    # Get item_ids and quantities
    while True:
        try:
            channel = grpc.insecure_channel(get_customer_db_ip())
            stub = customer_pb2_grpc.CustomerStub(channel)
            response = stub.GetShoppingCart(
                customer_pb2.CheckLoginRequest(username=username))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
        return {"status": False, "items": []}
    print(response.item_ids)
    print(response.quantities)
    # return {"status" : response.status}

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)

            items = []

            for i in range(len(response.item_ids)):
                item_id = response.item_ids[i]
                quantity = response.quantities[i]

                response2 = stub.GetItemByID(
                    product_pb2.GetItemByIDRequest(item_id=item_id))
                if response2.status:
                    items.append({
                        "name": response2.items[0].name,
                        "quantity": quantity
                    })
                else:
                    print("Unable to locate item")
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    return {"status": True, "items": items}
def add_item_shopping_cart():
    print("Adding items to shopping cart")
    data = json.loads(request.data)
    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False}

    item_id = data["item_id"]
    quantity = data["quantity"]

    # Check sufficient quantity
    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.GetItemByID(
                product_pb2.GetItemByIDRequest(item_id=item_id))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
        return {"status": response.status}
    if quantity > response.items[0].quantity:
        print("## ERROR ##")
        print(
            f"Insufficient quantity available: {response.items[0].quantity} requested: {quantity}"
        )
        return {"status": False}

    # Add item to shopping cart
    while True:
        try:
            channel = grpc.insecure_channel(get_customer_db_ip())
            stub = customer_pb2_grpc.CustomerStub(channel)
            response = stub.UpdateCart(
                customer_pb2.UpdateCartRequest(username=username,
                                               add=True,
                                               key="shopping_cart",
                                               item_id=item_id,
                                               quantity=quantity))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
def check_logged_in(username):
    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.CheckLogin(
                product_pb2.CheckLoginRequest(username=username, ))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print(response.error)
    return {"status": response.status, "logged_in": response.logged_in}
def display_active_seller_items():
    print("Displaying Active Items")
    data = json.loads(request.data)

    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False, "items": []}

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.GetAcct(
                product_pb2.GetAcctRequest(username=username))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            response = stub.GetItem(
                product_pb2.GetItemRequest(seller_id=response.seller_id))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    items = []
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    else:
        for i in response.items:
            item_dict = {
                "name": i.name,
                "category": i.category,
                "item_id": i.item_id,
                "condition_new": i.condition_new,
                "sale_price": i.sale_price,
                "quantity": i.quantity
            }
            items.append(item_dict)
    return {"status": response.status, "items": items}
def leave_feedback():
    print("Leaving Feedback")
    data = json.loads(request.data)
    username = data["username"]
    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False}

    item_id = data["item_id"]
    feedback_type = data["feedback"]

    # Check we've purchased this item before
    while True:
        try:
            channel = grpc.insecure_channel(get_customer_db_ip())
            stub = customer_pb2_grpc.CustomerStub(channel)
            response = stub.UpdateCart(
                customer_pb2.UpdateCartRequest(username=username,
                                               add=True,
                                               key="feedback",
                                               item_id=item_id,
                                               quantity=0))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
        return {"status": response.status}

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.LeaveFeedback(
                product_pb2.LeaveFeedbackRequest(feedback_type=feedback_type,
                                                 item_id=item_id))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
def search_items_for_sale():
    print("Searching items for sale")
    data = json.loads(request.data)
    username = data["username"]

    resp = check_logged_in(username)
    if not resp["status"] or not resp["logged_in"]:
        print("## ERROR ##")
        print("Not logged in")
        return {"status": False, "items": []}

    keywords = []
    category = -1  # Won't match any items
    if "keywords" in list(data.keys()):
        keywords = data["keywords"]
    if "category" in list(data.keys()):
        category = data["category"]

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.SearchItem(
                product_pb2.SearchItemRequest(keywords=keywords,
                                              category=category))
            items = []
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    else:
        for i in response.items:
            item_dict = {
                "name": i.name,
                "category": i.category,
                "item_id": i.item_id,
                "condition_new": i.condition_new,
                "sale_price": i.sale_price,
                "quantity": i.quantity,
                "seller_id": i.seller_id
            }
            items.append(item_dict)
    return {"status": response.status, "items": items}
def logout():
    print("logging out")
    data = json.loads(request.data)
    username = data["username"]

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.ChangeLogin(
                product_pb2.ChangeLoginRequest(
                    username=username,
                    password="",  # Not needed
                    logging_in=False))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
def create_user():
    data = json.loads(request.data)
    name = data["name"]
    username = data["username"]
    password = data["password"]

    while True:
        try:
            channel = grpc.insecure_channel(get_product_db_ip())
            stub = product_pb2_grpc.ProductStub(channel)
            response = stub.CreateUser(
                product_pb2.CreateUserRequest(name=name,
                                              username=username,
                                              password=password))
            break
        except grpc._channel._InactiveRpcError as grpc_exc:
            print("Server not available, trying a different one")
    if not response.status:
        print("## ERROR ##")
        print(response.error)
    return {"status": response.status}
Exemple #13
0
    def make_purchase(self, username, name, cc_number, cc_expiration):
        """Docstrings for service methods appear as documentation in the wsdl.
        #     <b>What fun!</b>
        #     @param username
        #     @param name the purchaser
        #     @param cc_number credit card number
        #     @param cc_expiration expiration date of the card
        #     @return confirmed purchase
        #     """
        print("Making Purchase")
        # print(name)
        # print(cc_number)
        # print(cc_expiration)

        # Look up the shopping cart
        # Get item_ids and quantities
        while True:
            try:
                channel = grpc.insecure_channel(get_customer_db_ip())
                stub = customer_pb2_grpc.CustomerStub(channel)
                response = stub.GetShoppingCart(
                    customer_pb2.CheckLoginRequest(username=username))
                break
            except grpc._channel._InactiveRpcError as grpc_exc:
                print("Server not available, trying a different one")
        if not response.status:
            print("## ERROR ##")
            print(response.error)
            return u"failure"
        """
        bool status = 1;
        repeated int32 item_ids = 2;
        repeated int32 quantities = 3;
        string error = 4;
        """

        # Make the purchase
        while True:
            try:
                channel = grpc.insecure_channel(get_product_db_ip())
                stub = product_pb2_grpc.ProductStub(channel)
                response = stub.MakePurchase(
                    product_pb2.MakePurchaseRequest(
                        item_ids=response.item_ids,
                        quantities=response.quantities))
                break
            except grpc._channel._InactiveRpcError as grpc_exc:
                print("Server not available, trying a different one")
        if not response.status:
            print("## ERROR ##")
            print(response.error)
            return u"failure"

        # Indicate to customer DB that we've made the purchase
        while True:
            try:
                channel = grpc.insecure_channel(get_customer_db_ip())
                stub = customer_pb2_grpc.CustomerStub(channel)
                response = stub.MakePurchase(
                    customer_pb2.CheckLoginRequest(username=username))
                break
            except grpc._channel._InactiveRpcError as grpc_exc:
                print("Server not available, trying a different one")
        if not response.status:
            print("## ERROR ##")
            print(response.error)

        return u"success"