stephenmcd/cartridge — pull request #217 by AlexHill (2014-11-27 05:22:26+00:00)
Title: Replace use of EmptyCart with an unsaved Cart

``EmptyCart`` exists to avoid DB queries for visitors who have nothing in their cart. An unsaved Cart can be used for the same purpose.

I'd like to make this change in the spirit of making Cartridge easier to use without its Product model. Currently, the only "official" way to add items to the cart (beyond just directly manipulating CartItems) is via the ``product`` view. It would be nice to be able to offer a variant of ``CartItemFormset`` for adding items as well as updating them, and have the cart view handle that. If the cart is always a ``Cart``, you can pass it to model forms and it will work as expected, whether or not it's in the database yet.

The tradeoff is in finding a place to call ``Cart.save()`` and to store the cart id in the session. I've chosen to save the cart in ``Cart.add_item()`` if it doesn't have an id, and to store the cart id in the session in ``recalculate_cart``. That means the existing views work unmodified.

Always feels good to get rid of some code, too.

Cheers
Alex