Spaces:
Sleeping
Sleeping
Update cart/views.py
Browse files- cart/views.py +69 -69
cart/views.py
CHANGED
|
@@ -1,69 +1,69 @@
|
|
| 1 |
-
from django.shortcuts import render, redirect, get_object_or_404
|
| 2 |
-
from django.contrib.auth.decorators import login_required
|
| 3 |
-
from django.utils.timezone import now
|
| 4 |
-
from Boxes.models import MysteryBox
|
| 5 |
-
from Accounts.models import Profile
|
| 6 |
-
from .models import Cart, CartItem, Order, OrderItem, Payment
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
@login_required
|
| 11 |
-
def cart_detail(request):
|
| 12 |
-
"""Show all items in the cart"""
|
| 13 |
-
profile = request.user.profile
|
| 14 |
-
cart, created = Cart.objects.get_or_create(user=profile)
|
| 15 |
-
|
| 16 |
-
return render(request, "
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@login_required
|
| 20 |
-
def cart_add(request, pk):
|
| 21 |
-
"""Add an item (MysteryBox) to the cart"""
|
| 22 |
-
profile = request.user.profile
|
| 23 |
-
cart, created = Cart.objects.get_or_create(user=profile)
|
| 24 |
-
box = get_object_or_404(MysteryBox, pk=pk)
|
| 25 |
-
|
| 26 |
-
item, created = CartItem.objects.get_or_create(cart=cart, box=box)
|
| 27 |
-
item.quantity += 1 if not created else 1
|
| 28 |
-
item.save()
|
| 29 |
-
|
| 30 |
-
return redirect("cart:cart_detail")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@login_required
|
| 34 |
-
def cart_remove(request, pk):
|
| 35 |
-
"""Remove one item from the cart"""
|
| 36 |
-
profile = request.user.profile
|
| 37 |
-
cart = get_object_or_404(Cart, user=profile)
|
| 38 |
-
item = get_object_or_404(CartItem, cart=cart, pk=pk)
|
| 39 |
-
item.delete()
|
| 40 |
-
return redirect("cart:cart_detail")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
@login_required
|
| 45 |
-
def checkout(request):
|
| 46 |
-
"""Convert cart into an order"""
|
| 47 |
-
profile = request.user.profile
|
| 48 |
-
cart = get_object_or_404(Cart, user=profile)
|
| 49 |
-
|
| 50 |
-
if not cart.items.exists():
|
| 51 |
-
return redirect("cart:cart_detail")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
order = Order.objects.create(
|
| 55 |
-
user=profile,
|
| 56 |
-
total_amount=cart.total_amount
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
for item in cart.items.all():
|
| 60 |
-
OrderItem.objects.create(
|
| 61 |
-
order=order,
|
| 62 |
-
box=item.box,
|
| 63 |
-
quantity=item.quantity,
|
| 64 |
-
unit_price=item.box.price
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
cart.items.all().delete()
|
| 68 |
-
|
| 69 |
-
return render(request, "
|
|
|
|
| 1 |
+
from django.shortcuts import render, redirect, get_object_or_404
|
| 2 |
+
from django.contrib.auth.decorators import login_required
|
| 3 |
+
from django.utils.timezone import now
|
| 4 |
+
from Boxes.models import MysteryBox
|
| 5 |
+
from Accounts.models import Profile
|
| 6 |
+
from .models import Cart, CartItem, Order, OrderItem, Payment
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
@login_required
|
| 11 |
+
def cart_detail(request):
|
| 12 |
+
"""Show all items in the cart"""
|
| 13 |
+
profile = request.user.profile
|
| 14 |
+
cart, created = Cart.objects.get_or_create(user=profile)
|
| 15 |
+
|
| 16 |
+
return render(request, "templates/cart.html", {"cart": cart})
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@login_required
|
| 20 |
+
def cart_add(request, pk):
|
| 21 |
+
"""Add an item (MysteryBox) to the cart"""
|
| 22 |
+
profile = request.user.profile
|
| 23 |
+
cart, created = Cart.objects.get_or_create(user=profile)
|
| 24 |
+
box = get_object_or_404(MysteryBox, pk=pk)
|
| 25 |
+
|
| 26 |
+
item, created = CartItem.objects.get_or_create(cart=cart, box=box)
|
| 27 |
+
item.quantity += 1 if not created else 1
|
| 28 |
+
item.save()
|
| 29 |
+
|
| 30 |
+
return redirect("cart:cart_detail")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@login_required
|
| 34 |
+
def cart_remove(request, pk):
|
| 35 |
+
"""Remove one item from the cart"""
|
| 36 |
+
profile = request.user.profile
|
| 37 |
+
cart = get_object_or_404(Cart, user=profile)
|
| 38 |
+
item = get_object_or_404(CartItem, cart=cart, pk=pk)
|
| 39 |
+
item.delete()
|
| 40 |
+
return redirect("cart:cart_detail")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@login_required
|
| 45 |
+
def checkout(request):
|
| 46 |
+
"""Convert cart into an order"""
|
| 47 |
+
profile = request.user.profile
|
| 48 |
+
cart = get_object_or_404(Cart, user=profile)
|
| 49 |
+
|
| 50 |
+
if not cart.items.exists():
|
| 51 |
+
return redirect("cart:cart_detail")
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
order = Order.objects.create(
|
| 55 |
+
user=profile,
|
| 56 |
+
total_amount=cart.total_amount
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
for item in cart.items.all():
|
| 60 |
+
OrderItem.objects.create(
|
| 61 |
+
order=order,
|
| 62 |
+
box=item.box,
|
| 63 |
+
quantity=item.quantity,
|
| 64 |
+
unit_price=item.box.price
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
cart.items.all().delete()
|
| 68 |
+
|
| 69 |
+
return render(request, "templates/checkout_success.html", {"order": order})
|