Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-cart forked from bmentges/django-cart #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions cart/cart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import models
from django.db.models import F

CART_ID = 'CART-ID'

Expand All @@ -21,6 +22,23 @@ def __init__(self, request):
cart = self.new(request)
self.cart = cart


@staticmethod
def get(request):
cart_id = request.session.get(CART_ID)
if cart_id:
try:
cart_exists = models.Cart.objects.filter(id=cart_id, checked_out=False).count() or False
except models.Cart.DoesNotExist:
cart_exists = False
else:
cart_exists = False
if cart_exists:
return Cart(request)
else:
return None


def __iter__(self):
for item in self.cart.item_set.all():
yield item
Expand All @@ -32,6 +50,7 @@ def new(self, request):
return cart

def add(self, product, unit_price, quantity=1):
assert self.cart is not None
try:
item = models.Item.objects.get(
cart=self.cart,
Expand All @@ -50,6 +69,7 @@ def add(self, product, unit_price, quantity=1):
item.save()

def remove(self, product):
assert self.cart is not None
try:
item = models.Item.objects.get(
cart=self.cart,
Expand All @@ -58,9 +78,14 @@ def remove(self, product):
except models.Item.DoesNotExist:
raise ItemDoesNotExist
else:
item.delete()
if item.quantity == 1:
item.delete()
else:
item.quantity = F('quantity')-1
item.save()

def update(self, product, quantity, unit_price=None):
assert self.cart is not None
try:
item = models.Item.objects.get(
cart=self.cart,
Expand All @@ -70,18 +95,20 @@ def update(self, product, quantity, unit_price=None):
raise ItemDoesNotExist

def count(self):
assert self.cart is not None
result = 0
for item in self.cart.item_set.all():
result += 1 * item.quantity
return result

def summary(self):
assert self.cart is not None
result = 0
for item in self.cart.item_set.all():
result += item.total_price
return result

def clear(self):
assert self.cart is not None
for item in self.cart.item_set.all():
item.delete()

item.delete()
4 changes: 4 additions & 0 deletions cart/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Meta:
verbose_name_plural = _('carts')
ordering = ('-creation_date',)

def checkout(self):
self.checked_out = True
self.save()

def __unicode__(self):
return unicode(self.creation_date)

Expand Down