Skip to content

Commit 9cded7e

Browse files
author
Systemaker
committed
update add to cart
1 parent 3b34ca3 commit 9cded7e

File tree

5 files changed

+11
-5
lines changed

5 files changed

+11
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Python Flask WEB API DEMO
1717
- Module Sample database RESOURCES :
1818
- ITEMS with images gallery,]
1919
- ASSETS files and image processing with Pillow
20-
- EVENTS (start/end datetime) [coming-soon]
20+
- EVENTS (start/end datetime)
2121
- PLACES (geolocalization with latitude and longitude) [coming-soon]
22-
- Module Sample database ORDERS and orderitem many-to-many relationship
22+
- Module Sample database ORDERS and orderitem many-to-many relationship and add-to cart
2323
- Module sample ADMIN, backoffice member with authentication and authorization powered by Flask-login plugin :
2424
- User Registry, Login & Logout
2525
- Session based authentication or Basic HTTP authentication or Token based authentication (with active SSL recommended in production environement)

app/modules/orders/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from sqlalchemy import desc
88
from sqlalchemy import or_
99
from flask import session
10+
from flask_login import current_user
1011

1112
# ------- IMPORT LOCAL DEPENDENCIES -------
1213
from ... import db
@@ -103,7 +104,7 @@ def add_cart(self, item_id):
103104

104105
amount = decimal.Decimal(order.amount)
105106
item = Item.query.filter(Item.id == item_id).first_or_404()
106-
107+
107108
# if item already exist
108109
if order.orderitems:
109110
for orderitem in order.orderitems :
@@ -120,13 +121,16 @@ def add_cart(self, item_id):
120121

121122

122123
orderitem = OrderItem(order = order, item = item)
124+
123125
# Caculate amount
124126
orderitem.unit_amount = item.amount
125127
orderitem.quantity = 1
126128
orderitem.total_amount = orderitem.unit_amount * orderitem.quantity
127129
amount = amount + orderitem.total_amount
128130
order.orderitems.append(orderitem)
129131
order.amount = amount
132+
if current_user and not order.user :
133+
order.user = current_user
130134
db.session.add(order)
131135
db.session.commit()
132136

app/modules/orders/templates/orders/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h3>Order</h3>
4444
unit amount : {{ orderitem.unit_amount }} |
4545
quantity : {{ orderitem.quantity }} |
4646
Amount :{{ orderitem.total_amount }} |
47-
<a href="{{ url_for('orders_page.remove_cart', item_id = orderitem.item.id) }}" onclick="return confirm('Are you sure you want to remove this item from cart ?');"><i class="glyphicon glyphicon-trash"></i> </a>
47+
<a href="{{ url_for('orders_page.remove_cart', item_id = orderitem.item.id) }}" onclick="return confirm('Are you sure you want to remove this item from cart ?');"><i class="glyphicon glyphicon-remove"></i> </a>
4848
<br/>
4949
{% endfor %}
5050
{% else %}

app/modules/orders/templates/orders/show.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ <h4 class="text-right"><a href="/orders"><span class="glyphicon glyphicon-home">
2424
{{ orderitem.item.title_en_US }} |
2525
unit amount : {{ orderitem.unit_amount }} |
2626
quantity : {{ orderitem.quantity }} |
27-
Amount :{{ orderitem.total_amount }} <br/>
27+
Amount :{{ orderitem.total_amount }} |
28+
<a href="{{ url_for('orders_page.remove_cart', item_id = orderitem.item.id) }}" onclick="return confirm('Are you sure you want to remove this item from cart ?');"><i class="glyphicon glyphicon-remove"></i> </a>
29+
<br/>
2830
{% endfor %}
2931
{% else %}
3032
No items

data/db.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)