Repository navigation
Expand file tree
/
Copy pathviews.py
More file actions
132 lines (111 loc) · 4.15 KB
/
Copy pathviews.py
File metadata and controls
132 lines (111 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# from django.shortcuts import render
# # Create your views here.
# def cart(request):
# return render(request,'store/cart.html')
from django.shortcuts import render, redirect, get_object_or_404
from store.models import Product, Variation
from .models import Cart, CartItems
from django.core.exceptions import ObjectDoesNotExist
# Create your views here.
from django.http import HttpResponse
def _cart_id(request):
cart = request.session.session_key
if not cart:
cart = request.session.create()
return cart
def add_cart(request, product_id):
product = Product.objects.get(id=product_id) #get the product
product_variation = []
if request.method == 'POST':
for item in request.POST:
key = item
value = request.POST[key]
try:
variation = Variation.objects.get(product=product, variation_category__iexact=key, variation_value__iexact=value)
product_variation.append(variation)
except:
pass
try:
cart = Cart.objects.get(cart_id=_cart_id(request)) # get the cart using the cart_id present in the session
except Cart.DoesNotExist:
cart = Cart.objects.create(
cart_id = _cart_id(request)
)
cart.save()
is_cart_item_exists = CartItems.objects.filter(product=product, cart=cart).exists()
if is_cart_item_exists:
cart_item = CartItems.objects.filter(product=product, cart=cart)
# existing_variations -> database
# current variation -> product_variation
# item_id -> database
ex_var_list = []
id = []
for item in cart_item:
existing_variation = item.variations.all()
ex_var_list.append(list(existing_variation))
id.append(item.id)
print(ex_var_list)
if product_variation in ex_var_list:
# increase the cart item quantity
index = ex_var_list.index(product_variation)
item_id = id[index]
item = CartItems.objects.get(product=product, id=item_id)
item.quantity += 1
item.save()
else:
item = CartItems.objects.create(product=product, quantity=1, cart=cart)
if len(product_variation) > 0:
item.variations.clear()
item.variations.add(*product_variation)
item.save()
else:
cart_item = CartItems.objects.create(
product = product,
quantity = 1,
cart = cart,
)
if len(product_variation) > 0:
cart_item.variations.clear()
cart_item.variations.add(*product_variation)
cart_item.save()
return redirect('cart')
def remove_cart(request, product_id, cart_item_id):
cart = Cart.objects.get(cart_id=_cart_id(request))
product = get_object_or_404(Product, id=product_id)
try:
cart_item = CartItems.objects.get(product=product, cart=cart, id=cart_item_id)
if cart_item.quantity > 1:
cart_item.quantity -= 1
cart_item.save()
else:
cart_item.delete()
except:
pass
return redirect('cart')
def remove_cart_item(request, product_id, cart_item_id):
cart = Cart.objects.get(cart_id=_cart_id(request))
product = get_object_or_404(Product, id=product_id)
cart_item = CartItems.objects.get(product=product, cart=cart, id=cart_item_id)
cart_item.delete()
return redirect('cart')
def cart(request, total=0, quantity=0, cart_items=None):
try:
tax = 0
grand_total = 0
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItems.objects.filter(cart=cart, is_active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
quantity += cart_item.quantity
tax = (2 * total)/100
grand_total = total + tax
except ObjectDoesNotExist:
pass #just ignore
context = {
'total': total,
'quantity': quantity,
'cart_items': cart_items,
'tax' : tax,
'grand_total': grand_total,
}
return render(request, 'store/cart.html', context)