Ejemplo n.º 1
0
def addItem():
    allItemsAdded = []
    all_products = []
    index_error = None
    if request.method == 'GET':
        """clear session and display all items in product catalog"""
        session.clear()
        productcur = DBConnect.getAllProduct()
        data = list(productcur)
        all_products = list()
        for row in data:
            all_products.append(row)


        return render_template('index.html', all_products=all_products)
    elif request.method == 'POST':
        allItemsAdded = []
        """ obtain lst through stored session variable"""
        allItemsAdded = session.get('sub_list', None)
        button_value = str(request.form['submit'])
        itemName = str(request.form['item'])

        """ perform 2 important functionalities
            1. Add item
            2. Delte Item
            List data structure used which is (allItemsAdded)
        """
        if(button_value == 'add'):
            if allItemsAdded is None:
                allItemsAdded = []
            if(DBConnect.checkItemExists(itemName)):
                allItemsAdded.append(itemName)
            else:
                return "Sorry, Item cannot be added as it does not exist in our catalog. Please Enter the product code as mentioned in the catalof to swiftly add the item to your basket!"

        elif(button_value == 'delete'):
            ## check DB catalog and delete
            allItemsAdded.remove(itemName)

        productcur = DBConnect.getAllProduct()
        data = list(productcur)
        all_products = list()
        for row in data:
            all_products.append(row)


        session['sub_list'] = allItemsAdded
        return render_template('index.html', all_products=all_products, lstOfItems = allItemsAdded)
Ejemplo n.º 2
0
def addItem():
    allItemsAdded = []
    all_products = []
    index_error = None
    if request.method == 'GET':
        session.clear()
        productcur = DBConnect.getAllProduct()
        data = list(productcur)
        all_products = list()
        for row in data:
            all_products.append(row)

        return render_template('index.html', all_products=all_products)
    elif request.method == 'POST':
        allItemsAdded = []
        allItemsAdded = session.get('sub_list', None)
        button_value = str(request.form['submit'])
        itemName = str(request.form['item'])

        if (button_value == 'add'):
            if allItemsAdded is None:
                allItemsAdded = []
            if (DBConnect.checkItemExists(itemName)):
                allItemsAdded.append(itemName)
                print("Here what the f**k exists :'(")
            else:
                return "Sorry, Item cannot be added as it does not exist in our catalog. Please Enter the product code as mentioned in the catalof to swiftly add the item to your basket!"

        elif (button_value == 'delete'):
            ## check DB catalog and delete
            allItemsAdded.remove(itemName)

        productcur = DBConnect.getAllProduct()
        data = list(productcur)
        all_products = list()
        for row in data:
            all_products.append(row)

        session['sub_list'] = allItemsAdded
        return render_template('index.html',
                               all_products=all_products,
                               lstOfItems=allItemsAdded)