session-cart / session_cart /

from session_cart.utils import get_carts

class CartMiddleware(object):
    Middleware that handles adding session carts to the request object.


    def process_request(self, request):
        Add the carts to the request object. 
        request.carts = dict(
            (name, Cart(request, name))
            for name, Cart in get_carts().items()

        if 'default' in get_carts():
            request.cart = request.carts['default']

    def process_response(self, request, response):
        Update the session carts if they have changed.

        # Don't assume the request object has carts, another layer may have
        # returned request this middleware's process_request was called.
        if hasattr(request, 'carts'):
            for name in get_carts():
        return response