Commits

curtis  committed ceeea1f

Initial commitInitial commit

  • Participants

Comments (0)

Files changed (3)

File session_cart/__init__.py

Empty file added.

File session_cart/cart.py

+'''Session based shopping cart'''
+
+class CartItem(object):
+    '''Lightweight container for cart items'''
+    __slots__ = ( 'id', 'item', 'quantity' )
+    def __init__( self, index, item, quantity ):
+        self.index = index
+        self.item = item
+        self.quantity = quantity and quantity or 0
+
+    def __repr__(self):
+        return 'CartItem(%r, %r)' % ( self.item, self.quantity, )
+
+    def __cmp__( self, other ):
+        if isinstance( other, CartItem ):
+            return cmp(self.item, other.item)
+        return cmp(self.item, other)
+
+class Cart( list ):
+    '''Handles a list of items stored in the session'''
+    def __init__( self, request, items =None, model=None ):
+        super( Cart, self ).__init__()
+        self.request = request
+        self.model = model
+        if items is not None:
+            for item in items:
+                self.add_item( item.item, item.quantity )
+        else:
+            # Cart is stored as a list of ( item_id, quantity )
+            for prod, quantity in request.session.get('cart', [] ):
+                try:
+                    self.add_item( prod, quantity )
+                except self.model.DoesNotExist:
+                    pass
+
+    def save( self ):
+        '''Save this cart to the session'''
+        self.request.session['cart'] = tuple(
+            ( i.item.id, i.quantity, )
+            for i in self
+        )
+
+    def add_item( self, item, quantity =1 ):
+        '''Add a new item to this session'''
+        if not isinstance( item, self.model ):
+            item = self.model.objects.get(id=item)
+        # Dupe checking
+        try:
+            self[self.index(item)].quantity += quantity
+        except ValueError:
+            self.append(CartItem( len(self), item, quantity ))
+
+    def size(self):
+        '''Return the number of items in this cart'''
+        return len(self)
+
+    def empty(self):
+        '''Remove all items from cart'''
+        while len(self):
+            self.pop()
+        self.save()
+
+    def __repr__( self ):
+        return ','.join([ str(x) for x in self])
+
+from distutils.core import setup
+
+setup( name='django-session-cart',
+	version='1.0',
+	py_modules=['session_cart']
+)