Commits

Anonymous committed 5747241

Reworked API to behave more like a normal list
Moved key to __init__ param 'name'

  • Participants
  • Parent commits 3ace508

Comments (0)

Files changed (1)

session_cart/cart.py

 
 class CartItem(object):
     '''Lightweight container for cart items'''
-    __slots__ = ('id', 'item', 'quantity',)
-    def __init__(self, index, item, quantity):
-        self.index = index
+    __slots__ = ('item', 'quantity',)
+    def __init__(self, item, quantity):
         self.item = item
         self.quantity = quantity and quantity or 0
 
 class Cart(list):
     '''Handles a list of items stored in the session'''
     model = None
-    key = 'cart'
-    def __init__(self, request, items =None):
+    def __init__(self, request, name='cart'):
         super(Cart, self).__init__()
         self.request = request
-        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(self.key, []):
-                try:
-                    self.add_item(prod, quantity)
-                except self.model.DoesNotExist:
-                    pass
+        self.name = name
+        if self.model is None:
+            from django.db import models
+            from django.conf import settings
+            try:
+                cart_app, cart_model = settings.CART_MODEL.split('.')
+                Cart.model = models.get_model(cart_app, cart_model)
+            except AttributeError:
+                # Should this raise ImproperlyConfigured ?
+                pass
+        # Cart is stored as a list of ( item_id, quantity )
+        for item, quantity in request.session.get(self.name, []):
+            try:
+                self.append(item, quantity)
+            except self.model.DoesNotExist:
+                pass
 
     def save(self):
         '''Save this cart to the session'''
-        self.request.session[self.key] = tuple(
+        self.request.session[self.name] = tuple(
             (i.item.pk, i.quantity,)
             for i in self
         )
 
-    def add_item(self, item, quantity =1):
-        '''Add a new item to this session'''
+    def _get(self, item):
+        '''Ensure item is an instance of self.model'''
         if not isinstance(item, self.model):
-            item = self.model._default_manager.get(pk=item)
-        # Dupe checking
+            return self.model._default_manager.get(pk=item)
+        return item
+
+    def append(self, item, quantity =1):
+        item = self._get(item)
         try:
             self[self.index(item)].quantity += quantity
         except ValueError:
-            self.append(CartItem(len(self), item, quantity))
+            super(Cart, self).append(CartItem(item, quantity))
 
-    def size(self):
-        '''Return the number of items in this cart'''
-        return len(self)
+    def remove(self, item):
+        super(Cart, self).remove(self._get(item))
 
     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])
+        return ','.join([repr(x) for x in self])