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()

from pyramid.view import view_config


def includeme(config):

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


@view_config(renderer='json', name='add_to_cart.json')
def add_to_cart(request):
    url = request.params.get('url')
    request.cart.add_item(url)
    return request.cart.to_json()


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

    def download_url(self):
        return self.url

    def to_json(self):
        return {'url': self.download_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):
        if url:
            item = CartItem(url)
            self.items[url] = item
            self.save()

    def remove_item(self, url):
        if url and 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 clear(self):
        self.items = {}
        self.session['cart'] = {}
        self.session.changed()

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

    def to_json(self):
        return [self.items[key].to_json() for key in self.items]

clear method added and add_to_cart action