Anonymous committed 8e264fa

Comments (0)

Files changed (2)


 '''Session based shopping cart'''
+from django.core.exceptions import ImproperlyConfigured
+from .utils import import_cart
 class CartItem(object):
     '''Lightweight container for cart items'''
     __slots__ = ('item', 'quantity',)
-    def __init__(self, item, quantity):
+    def __init__(self, item, quantity=None):
         self.item = item
-        self.quantity = quantity and quantity or 0
+        self.quantity = quantity if quantity or 0
     def __repr__(self):
         return 'CartItem(%r, %r)' % (self.item, self.quantity)
             from django.db import models
             from django.conf import settings
-                cart_app, cart_model = settings.CART_MODEL.split('.')
-                Cart.model = models.get_model(cart_app, cart_model)
+                Cart.model = import_cart(settiongs.CART_MODEL)
             except AttributeError:
-                # Should this raise ImproperlyConfigured ?
-                pass
+                raise ImproperlyConfigured("%s isn't a valid Cart model." % settings.CART_MODEL)
         # Cart is stored as a list of ( item_id, quantity )
         for item, quantity in request.session.get(, []):
         except ValueError:
             super(Cart, self).append(CartItem(item, quantity))
+    def index(self. value, **kwargs):
+        '''Prevent duplication of (item, quantity) pairs.'''
+        if isinstance(value, self.model):
+            value = CartItem(value)
+        return super(Cart, self).index(value)
     def remove(self, item):
         super(Cart, self).remove(self._get(item))


     carts = getattr(request, 'carts', None)
     if carts:
         if 'default' in get_carts():
-            context['cart'] = carts.default
+            context['cart'] = carts['default']
         context['carts'] = carts
     return context
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.