def addToCartView(request): form = CartForm() if request.method == 'POST': food_id = request.POST.get('foodIdfk') sessionemail = request.session['Email'] form = CartForm(request.POST) cart = CartModel.objects.filter(emailIdfk=sessionemail) food = FoodModel.objects.get(id=food_id) if (food.quantity > int(request.POST['Quantity'])): if form.is_valid(): if cart.count() == 0: form.save() else: for c in cart: if int(c.foodIdfk) == int(request.POST['foodIdfk']): cursor = connection.cursor() newQuantity = int(c.Quantity) + int( request.POST['Quantity']) if newQuantity <= food.quantity: sqlUpdate = 'UPDATE cart_tbl SET Quantity={0} WHERE id={1}'.format( newQuantity, c.id) cursor.execute(sqlUpdate) break else: form.save() else: messages.info( request, "Please Enter value less than food Quantity " + str(food.quantity)) return redirect('/Foodapp/foods') return redirect('/Foodapp/foods') return render(request, 'cartapp/addcart.html', { 'form': form, 'foodIdfk': id })
def addToCartView(request): form = CartForm() if request.method == 'POST': sessionemail = request.session['email'] form = CartForm(request.POST) cart = CartModel.objects.filter(emailIdfk=sessionemail) if form.is_valid(): if cart.count() == 0: form.save() else: for c in cart: if int(c.foodIdfk) == int(request.POST['foodIdfk']): cursor = connection.cursor() newQuantity = int(c.foodQuantity) + int( request.POST['foodQuantity']) sqlupdate = 'UPDATE cart_tbl SET foodQuantity ={0} WHERE id={1}'.format( newQuantity, c.id) cursor.execute(sqlupdate) break else: form.save() return redirect('/foodapp/foods') return render(request, 'cartapp/addcart.html', {'form': form})
def addToCartView(request): food_id = request.POST["foodIdfk"] form = CartForm() if request.method == 'POST': sessionemail = request.session['email'] form = CartForm(request.POST) cart = CartModel.objects.filter(emailIdfk=sessionemail) food = FoodModel.objects.get(id=food_id) print(food) if (food.quantity > int(request.POST['foodQuantity'])): if form.is_valid(): print("In is valid save") if cart.count() == 0: form.save() return redirect('/foodapp/foods') else: for c in cart: if int(c.foodIdfk) == int(request.POST['foodIdfk']): cursor = connection.cursor() newQuantity = int(c.foodQuantity) + int( request.POST['foodQuantity']) sqlupdate = 'UPDATE cart_tbl SET foodQuantity ={0} WHERE id={1}'.format( newQuantity, c.id) cursor.execute(sqlupdate) break else: print("in else save") form.save() return redirect('/foodapp/foods') else: print("Else") messages.info( request, "Please enter the value less than food Quantity" + str(food.quantity)) return redirect('/foodapp/foods') return render(request, 'cartapp/addcart.html', {'form': form})
def showAddToCartView(request, id): form = CartForm() return render(request, 'cartapp/addcart.html', { 'form': form, 'foodIdfk': id })