diff --git a/shop/forms.py b/shop/forms.py index cf249a7f8c400dc89dd16f050f26fb761a2368ee..4f5bc5c25a9b08876411a7b2361742b09889b19c 100644 --- a/shop/forms.py +++ b/shop/forms.py @@ -26,12 +26,10 @@ class addToCart(FlaskForm): submit = SubmitField('Confirm') class removeFormCart(FlaskForm): - item_id_remove = StringField('Enter item id of item that you wish to remove') clear_all = BooleanField('Clear all?') submit = SubmitField('Confirm') class removeFormWishlist(FlaskForm): - item_id_remove = StringField('Enter item id of item that you wish to remove') clear_all = BooleanField('Clear all?') submit = SubmitField('Confirm') diff --git a/shop/routes.py b/shop/routes.py index d5488f5bb11f19330375ce1f1daba8b6255086a6..cabe86a132cdd567973885427f0b07902cec5576 100644 --- a/shop/routes.py +++ b/shop/routes.py @@ -131,30 +131,32 @@ def cart(): cart_list = make_lists_out[0] cart_id_list = make_lists_out[1] - # performs the functionality of the remove from cart form. If an item id is submitted it removes one case of that - # item from the current users cart list. If the remove all button is ticked it makes the current cart empty by - # making the users cart string ''. + # performs the functionality of the remove from cart form. If remove button pressed it removes one of those items + # from the cart item from the current users cart list. If the remove all button is ticked it makes the current cart + # empty by making the users cart string ''. # At the end it refreshes the page so that the removed item is not on the page. remove_form = removeFormCart() if remove_form.validate_on_submit() and remove_form.validate(): - remove_item_id = remove_form.item_id_remove.data clear_all = remove_form.clear_all.data - if remove_item_id in cart_id_list: - cart_id_list.remove(remove_item_id) - new_cart_string = '' - for item_id in cart_id_list: - new_cart_string = new_cart_string + item_id + ' ' - for c in db.session.query(User).filter_by(id=current_user.get_id()): - c.cart = new_cart_string - db.session.commit() if clear_all: - for c in db.session.query(User).filter_by(id=current_user.get_id()): - c.cart = '' + for user in db.session.query(User).filter_by(id=current_user.get_id()): + user.cart = '' db.session.commit() return redirect(url_for('cart')) + elif request.method == 'POST': + remove_id = request.form['remove_id'] + cart_id_list.remove(remove_id) + new_cart_string = '' + for item_id in cart_id_list: + new_cart_string = new_cart_string + item_id + ' ' + for user in db.session.query(User).filter_by(id=current_user.get_id()): + user.cart = new_cart_string + db.session.commit() + return redirect(url_for('cart')) + # gets the total price of the cart and then decides on if the user is allowed free shipping and if not adds # shipping cost to the final price # repeated in checkout as need some of the variables created in the code to pass into template @@ -189,22 +191,24 @@ def wishlist(): remove_form = removeFormWishlist() if remove_form.validate_on_submit(): - remove_item_id = remove_form.item_id_remove.data clear_all = remove_form.clear_all.data - if remove_item_id in wishlist_id_list: - wishlist_id_list.remove(remove_item_id) - new_wishlist_string = '' - for item_id in wishlist_id_list: - new_wishlist_string = new_wishlist_string + item_id + ' ' - for c in db.session.query(User).filter_by(id=current_user.get_id()): - c.wishlist = new_wishlist_string - db.session.commit() if clear_all: - for c in db.session.query(User).filter_by(id=current_user.get_id()): - c.wishlist = '' + for user in db.session.query(User).filter_by(id=current_user.get_id()): + user.wishlist = '' db.session.commit() return redirect(url_for('wishlist')) + elif request.method == 'POST': + remove_id = request.form['remove_id'] + wishlist_id_list.remove(remove_id) + new_wishlist_string = '' + for item_id in wishlist_id_list: + new_wishlist_string = new_wishlist_string + item_id + ' ' + for user in db.session.query(User).filter_by(id=current_user.get_id()): + user.wishlist = new_wishlist_string + db.session.commit() + return redirect(url_for('wishlist')) + return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), remove_form = remove_form) diff --git a/shop/templates/cart.html b/shop/templates/cart.html index b280c830ddbebf212e44e0470ebfe596c3a982bc..620760c7d5f0d918d3d1635a692adfeca32a6f6c 100644 --- a/shop/templates/cart.html +++ b/shop/templates/cart.html @@ -12,9 +12,6 @@ <div class = "remove_form"> <form method="POST"> {{ remove_form.csrf_token }} - <p class = "login_field"> - {{ remove_form.item_id_remove.label }} {{ remove_form.item_id_remove(class = "input_field") }} - </p> <p class = "login_field"> clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }} </p> @@ -54,7 +51,10 @@ <img class = "item_cart_img" src = "{{ url_for('static', filename='images/' + item.product_image) }}" alt="background image"/></a> <p><a class = "item_shopping_name" href="/item?id={{item.product_id}}"><b>{{ item.product_name }}</b></a></p> <p class = "shopping_item_text">price: £{{ '{:,.2f}'.format(item.product_price) }}</p> - <p class = "shopping_item_text">item id: {{ item.product_id }}</p> + <form method = "post" style = "padding-top: 10px"> + <input type = "hidden" name = "remove_id" value = {{ item.product_id }}> + <input class = "orange_button" type = "submit" value = "remove"> + </form> </div> {% endfor %} diff --git a/shop/templates/wishlist.html b/shop/templates/wishlist.html index 774c8708e69589878221453f8ddf23a07146b797..c5991216a7d9d9581ebee8ea55f3061f91cef064 100644 --- a/shop/templates/wishlist.html +++ b/shop/templates/wishlist.html @@ -14,10 +14,7 @@ <div class = "remove_form"> <form method="POST"> {{ remove_form.csrf_token }} - <p> - {{ remove_form.item_id_remove.label }} {{ remove_form.item_id_remove(class = "field_order_by") }} - </p> - <p> + <p class = "login_field"> clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }} </p> </form> @@ -37,9 +34,12 @@ <div class = "cart_box" > <a href="/item?id={{item.product_id}}"> <img class = "item_cart_img" src = "{{ url_for('static', filename='images/' + item.product_image) }}" alt="product image"/></a> - <p><a class = "item_cart_name" href="/item?id={{item.product_id}}"><b>{{ item.product_name }}</b></a></p> - <p>price: £{{ item.product_price }}</p> - <p>item id: {{ item.product_id }}</p> + <p><a class = "item_shopping_name" href="/item?id={{item.product_id}}"><b>{{ item.product_name }}</b></a></p> + <p class = "shopping_item_text">price: £{{ item.product_price }}</p> + <form method = "post" style = "padding-top: 10px"> + <input type = "hidden" name = "remove_id" value = {{ item.product_id }}> + <input class = "orange_button" type = "submit" value = "remove"> + </form> </div> {% endfor %}