Skip to content
Snippets Groups Projects
Commit be7d2d31 authored by Michael Drury's avatar Michael Drury
Browse files

changed cart and wishlist remove form so that can remove each item with a...

changed cart and wishlist remove form so that can remove each item with a remove button. Also made wishlist match cart style
parent ec3e6a2f
No related branches found
No related tags found
No related merge requests found
...@@ -26,12 +26,10 @@ class addToCart(FlaskForm): ...@@ -26,12 +26,10 @@ class addToCart(FlaskForm):
submit = SubmitField('Confirm') submit = SubmitField('Confirm')
class removeFormCart(FlaskForm): class removeFormCart(FlaskForm):
item_id_remove = StringField('Enter item id of item that you wish to remove')
clear_all = BooleanField('Clear all?') clear_all = BooleanField('Clear all?')
submit = SubmitField('Confirm') submit = SubmitField('Confirm')
class removeFormWishlist(FlaskForm): class removeFormWishlist(FlaskForm):
item_id_remove = StringField('Enter item id of item that you wish to remove')
clear_all = BooleanField('Clear all?') clear_all = BooleanField('Clear all?')
submit = SubmitField('Confirm') submit = SubmitField('Confirm')
......
...@@ -131,30 +131,32 @@ def cart(): ...@@ -131,30 +131,32 @@ def cart():
cart_list = make_lists_out[0] cart_list = make_lists_out[0]
cart_id_list = make_lists_out[1] 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 # performs the functionality of the remove from cart form. If remove button pressed it removes one of those items
# item from the current users cart list. If the remove all button is ticked it makes the current cart empty by # from the cart item from the current users cart list. If the remove all button is ticked it makes the current cart
# making the users cart string ''. # empty by making the users cart string ''.
# At the end it refreshes the page so that the removed item is not on the page. # At the end it refreshes the page so that the removed item is not on the page.
remove_form = removeFormCart() remove_form = removeFormCart()
if remove_form.validate_on_submit() and remove_form.validate(): 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 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: if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()): for user in db.session.query(User).filter_by(id=current_user.get_id()):
c.cart = '' user.cart = ''
db.session.commit() db.session.commit()
return redirect(url_for('cart')) 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 # 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 # shipping cost to the final price
# repeated in checkout as need some of the variables created in the code to pass into template # repeated in checkout as need some of the variables created in the code to pass into template
...@@ -189,22 +191,24 @@ def wishlist(): ...@@ -189,22 +191,24 @@ def wishlist():
remove_form = removeFormWishlist() remove_form = removeFormWishlist()
if remove_form.validate_on_submit(): if remove_form.validate_on_submit():
remove_item_id = remove_form.item_id_remove.data
clear_all = remove_form.clear_all.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: if clear_all:
for c in db.session.query(User).filter_by(id=current_user.get_id()): for user in db.session.query(User).filter_by(id=current_user.get_id()):
c.wishlist = '' user.wishlist = ''
db.session.commit() db.session.commit()
return redirect(url_for('wishlist')) 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) return render_template('wishlist.html', wishlist_list = wishlist_list, wishlist_size = len(wishlist_list), remove_form = remove_form)
......
...@@ -12,9 +12,6 @@ ...@@ -12,9 +12,6 @@
<div class = "remove_form"> <div class = "remove_form">
<form method="POST"> <form method="POST">
{{ remove_form.csrf_token }} {{ 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"> <p class = "login_field">
clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }} clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }}
</p> </p>
...@@ -54,7 +51,10 @@ ...@@ -54,7 +51,10 @@
<img class = "item_cart_img" src = "{{ url_for('static', filename='images/' + item.product_image) }}" alt="background image"/></a> <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><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">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> </div>
{% endfor %} {% endfor %}
......
...@@ -14,10 +14,7 @@ ...@@ -14,10 +14,7 @@
<div class = "remove_form"> <div class = "remove_form">
<form method="POST"> <form method="POST">
{{ remove_form.csrf_token }} {{ remove_form.csrf_token }}
<p> <p class = "login_field">
{{ remove_form.item_id_remove.label }} {{ remove_form.item_id_remove(class = "field_order_by") }}
</p>
<p>
clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }} clear all? {{ remove_form.clear_all }} {{ remove_form.submit(class = "orange_button") }}
</p> </p>
</form> </form>
...@@ -37,9 +34,12 @@ ...@@ -37,9 +34,12 @@
<div class = "cart_box" > <div class = "cart_box" >
<a href="/item?id={{item.product_id}}"> <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> <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><a class = "item_shopping_name" href="/item?id={{item.product_id}}"><b>{{ item.product_name }}</b></a></p>
<p>price: £{{ item.product_price }}</p> <p class = "shopping_item_text">price: £{{ item.product_price }}</p>
<p>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> </div>
{% endfor %} {% endfor %}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment