def includeme(config):

    def cart(request):
        return Cart()
    config.add_request_method(cart, reify=True)


class CartItem(object):
    def __init__(self, url):
        self.url = url

    def download_url(self):
        return self.url


class Cart(object):

    def __init__(self, request):
        self.request = request
        self.session = request.session
        # load items
        self.items = self.session.get('cart', {})

    def __iter__(self):
        """
        Allow the cart to be iterated giving access to the cart's items.
        """
        for key in self.items:
            yield self.items[key]

    def add_item(self, url):
        item = CartItem(url)
        self.items[url] = item
        self.save()

    def remove_item(self, url):
        if url in self.items:
            del self.items[url]
        self.save()

    def num_items(self):
        return len(self.items)

    def has_items(self):
        return self.num_items() > 0

    def save(self):
        self.session['cart'] = self.items
        self.session.changed()

@@ -1,12 +1,51 @@
+from pyramid.view import view_config%0A%0A%0A
 def includem
@@ -99,16 +99,23 @@
 rn Cart(
+request
 )%0A    co
@@ -158,16 +158,200 @@
 True)%0A%0A%0A
+@view_config(renderer='json', name='add_to_cart.json')%0Adef add_to_cart(request):%0A    url = request.params.get('url')%0A    request.cart.add_item(url)%0A    return request.cart.to_json()%0A%0A%0A
 class Ca
@@ -472,16 +472,84 @@
 lf.url%0A%0A
+    def to_json(self):%0A        return %7B'url': self.download_url()%7D%0A%0A
 %0Aclass C
@@ -945,32 +945,52 @@
 tem(self, url):%0A
+        if url:%0A    
         item = C
@@ -998,24 +998,28 @@
 rtItem(url)%0A
+    
         self
@@ -1037,32 +1037,36 @@
  = item%0A        
+   
+ 
 self.save()%0A%0A   
@@ -1109,16 +1109,24 @@
  if url 
+and url 
 in self.
@@ -1164,32 +1164,36 @@
 ms%5Burl%5D%0A        
+    
 self.save()%0A%0A   
@@ -1320,89 +1320,289 @@
 def 
-save(self):%0A        self.session%5B'cart'%5D = self.items%0A        self.session.changed()
+clear(self):%0A        self.items = %7B%7D%0A        self.session%5B'cart'%5D = %7B%7D%0A        self.session.changed()%0A%0A    def save(self):%0A        self.session%5B'cart'%5D = self.items%0A        self.session.changed()%0A%0A    def to_json(self):%0A        return %5Bself.items%5Bkey%5D.to_json() for key in self.items%5D
 %0A

clear method added and add_to_cart action