easycart.views

A set of views every cart needs.

On success, each view returns a JSON-response with the cart representation. For the details on the format of the return value, see the encode() method of the BaseCart class.

If a parameter required by a view is not present in the request’s POST data, then the JSON-response will have the format:

{'error': 'MissingRequestParam', 'param': parameter_name}

Almost the same thing happens, if a parameter is invalid and results in an exception, which is a subclass of CartException. In this case, the error value will be the name of the concrete exception class (e.g. 'ItemNotInCart' or 'NegativeItemQuantity'). And instead of param there may be one or more items providing additional info on the error, for example, the primary key of an item you was trying to change or an invalid quantity passed in the request.

Note

All of the views in this module accept only POST requests.

Warning

The views in this module do not protect you from race conditions, which may occur if, for example, server receives requests changing the cart state almost simultaneously. It seems there’s no good platform-independent way to do it (see this issue).

For now, I suggest to use JavaScript to ensure that you don’t make new requests to the cart until you have a response for the current one. Feel free to reopen the issue, if you have any suggestions on how to improve the situation.

class easycart.views.AddItem(**kwargs)[source]

Add an item to the cart.

This view expects request.POST to contain:

key value
pk the primary key of an item to add
quantity a quantity that should be associated with the item

The quantity parameter is optional (defaults to 1).

class easycart.views.RemoveItem(**kwargs)[source]

Remove an item from the cart.

Expects request.POST to contain key pk. The associated value should be the primary key of an item you wish to remove.

class easycart.views.ChangeItemQuantity(**kwargs)[source]

Change the quantity associated with an item.

This view expects request.POST to contain:

key value
pk the primary key of an item
quantity a new quantity to associate with the item
class easycart.views.EmptyCart(**kwargs)[source]

Remove all items from the cart.