Esempio n. 1
0
def create_product(product_name, product_attribute_fields, category):
    product_attributes = []
    loop = True
    while loop:
        attributes = input(
            f"Introduce the {product_attribute_fields} for the {product_name}, each attribute separated by a comma\n"
        )
        product_attributes = attributes.split(',')
        if product_attributes.__len__() != 6:
            product_attributes.clear()
            option_to_go = int(
                input(
                    "Please make sure to include all the attributes. Input 1 to try again or any other number to return to the store menu:\n"
                ))
            if option_to_go != 1:
                return
        else:
            loop = False
    result = getattr(product, product_name)
    new_product = result(category, product_attributes[0],
                         product_attributes[1], product_attributes[2],
                         product_attributes[3], product_attributes[4],
                         product_attributes[5])
    Products.add_product(new_product)
    input(
        f"{product_name} product added successfully. Press enter key in order to continue\n"
    )
def add_new_product():
    if request.method == 'POST':
        name = request.form['product-name']
        cost = request.form['product-cost']
        ingredients = request.form['product-ingredients']

        product_instance = Products()
        product_instance.add_product(name, cost, ingredients)
        return redirect('/products')
    else:
        return render_template('add_new_product.html')
    def test_add_product(self):
        """test add product method for products"""
        product_to_add = {'name': 'cucumber', 'cost': 5.55, 'ingredients': None}

        product_instance = Products()

        # method should return object added if successful
        test = product_instance.add_product(product_to_add["name"], product_to_add["cost"], product_to_add["ingredients"])
        self.assertEqual(test, product_to_add)

        # method should return 0 if object already exists
        product_to_add_key_already_exists = {'name': 'chips', 'cost': 2.22, 'ingredients': None}
        test2 = product_instance.add_product(product_to_add_key_already_exists["name"], product_to_add_key_already_exists["cost"], product_to_add_key_already_exists["ingredients"])
        self.assertEqual(test2, 0)