-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.py
57 lines (47 loc) · 1.61 KB
/
cart.py
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
from dashboard.models import Document
class Cart():
def __init__(self, request):
self.session = request.session
cart = self.session.get('session_key')
if 'session_key' not in request.session:
cart = self.session['session_key'] = {}
self.cart = cart
def add(self, document):
document_id = str(document.id)
if document_id in self.cart:
pass
else:
self.cart[document_id] = {'cost': str(document.cost)}
self.session.modified = True
def __len__(self):
return len(self.cart)
def get_docs(self):
# Get ids from cart
doc_ids = self.cart.keys()
documents = Document.objects.filter(id__in=doc_ids)
return documents
def delete(self, document):
document_id = str(document.id)
# Delete from dictionary/cart
if document_id in self.cart:
del self.cart[document_id]
self.session.modified = True
def cart_total(self):
# Get document IDS
document_ids = self.cart.keys()
# lookup those keys in our documents database model
documents = Document.objects.filter(id__in=document_ids)
# Get quantities
quantities = self.cart
# Start counting at 0
total = 0
for key, value in quantities.items():
key = int(key)
for doc in documents:
if doc.id == key:
total = total + (doc.cost)
return total
def clear(self):
self.cart = {}
self.session['session_key'] = {}
self.session.modified = True