Skip to content

Commit

Permalink
17 - Finalize Checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
codingforentrepreneurs committed Sep 29, 2017
1 parent 3cee508 commit f38442a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/carts/templates/carts/checkout.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ <h1>Finalize Checkout</h1>
<p>Cart Total: {{ object.cart.total }}</p>
<p>Shipping Total: {{ object.shipping_total }}</p>
<p>Order Total: {{ object.total }}</p>
<button>Checkout</button>
<form class='form' method='POST' action="">{% csrf_token %}
<button type='submit'>Checkout</button>
</form>
{% endif %}
{% endif %}

Expand Down
9 changes: 9 additions & 0 deletions src/carts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def checkout_home(request):
shipping_address_id = request.session.get("shipping_address_id", None)

billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request)

if billing_profile is not None:
order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj)
if shipping_address_id:
Expand All @@ -60,6 +61,14 @@ def checkout_home(request):
if billing_address_id or shipping_address_id:
order_obj.save()

if request.method == "POST":
"check that order is done"
is_done = order_obj.check_done()
if is_done:
order_obj.mark_paid()
request.session['cart_items'] = 0
del request.session['cart_id']
return redirect("/cart/success")
context = {
"object": order_obj,
"billing_profile": billing_profile,
Expand Down
Binary file modified src/db.sqlite3
Binary file not shown.
19 changes: 18 additions & 1 deletion src/orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def new_or_get(self, billing_profile, cart_obj):
qs = self.get_queryset().filter(
billing_profile=billing_profile,
cart=cart_obj,
active=True)
active=True,
status='created'
)
if qs.count() == 1:
obj = qs.first()
else:
Expand Down Expand Up @@ -58,6 +60,21 @@ def update_total(self):
self.save()
return new_total

def check_done(self):
billing_profile = self.billing_profile
shipping_address = self.shipping_address
billing_address = self.billing_address
total = self.total
if billing_profile and shipping_address and billing_address and total > 0:
return True
return False

def mark_paid(self):
if self.check_done():
self.status = "paid"
self.save()
return self.status


def pre_save_create_order_id(sender, instance, *args, **kwargs):
if not instance.order_id:
Expand Down

0 comments on commit f38442a

Please sign in to comment.