Commits

Anonymous committed 67c534b

Added simple middleware, for single cart only use

  • Participants
  • Parent commits 10aa9c7

Comments (0)

Files changed (1)

session_cart/middleware.py

+
+from session_cart.cart import Cart
 from session_cart.utils import get_carts
 
 
-class CartMiddleware(object):
+class MultiCartMiddleware(object):
     """
     Middleware that handles adding session carts to the request object.
 
 
     def process_request(self, request):
         """
-        Add the carts to the request object. 
+        Add the carts to the request object.
         """
         request.carts = dict(
             (name, Cart(request, name))
             for name in get_carts():
                 request.carts[name].save()
         return response
+
+class SimpleCartMiddleware(object):
+    '''
+    Middleware to support only a single cart, for the simple case
+    '''
+
+    def process_request(self, request):
+        request.cart = Cart(request)
+        return request
+
+    def process_response(self, request, response):
+        if hasattr(request, 'cart'):
+            request.cart.save()
+        return response