1

SO I am trying to build a simple shopping cart feature for my app, that simply add, remove a piece of equipment, and display a list of current cart. Here are my codes for this cart: (codes largely adopted from https://github.com/bmentges/django-cart)

cart.py:

import datetime
from .models import Cart, Item, ItemManager

CART_ID = 'CART-ID'

class ItemAlreadyExists(Exception):
    pass

class ItemDoesNotExist(Exception):
    pass

class Cart:
    def __init__(self, request, *args, **kwargs):
        super(Cart, self).__init__()
        cart_id = request.session.get(CART_ID)
        if cart_id:
            try:
                cart = models.Cart.objects.get(id=cart_id, checked_out=False)
            except models.Cart.DoesNotExist:
                cart = self.new(request)
        else:
            cart = self.new(request)
        self.cart = cart

    def __iter__(self):
        for item in self.cart.item_set.all():
            yield item

    def new(self, request):
        cart = Cart(request, creation_date=datetime.datetime.now())
        cart.save()
        request.session[CART_ID] = cart.id
        return cart

    def add(self, equipment):
        try:
            item = models.Item.objects.get(
                cart=self.cart,
                equipment=equipment,
            )
        except models.Item.DoesNotExist:
            item = models.Item()
            item.cart = self.cart
            item.equipment = equipment
            item.save()
        else: #ItemAlreadyExists

            item.save()

    def remove(self, equipment):
        try:
            item = models.Item.objects.get(
                cart=self.cart,
                equipment=equipment,
            )
        except models.Item.DoesNotExist:
            raise ItemDoesNotExist
        else:
            item.delete()

    def count(self):
        result = 0
        for item in self.cart.item_set.all():
            result += 1 * item.quantity
        return result


    def clear(self):
        for item in self.cart.item_set.all():
            item.delete()

and models.py:

class Cart(models.Model):
    creation_date = models.DateTimeField(verbose_name=_('creation date'))
    checked_out = models.BooleanField(default=False, verbose_name=_('checked out'))

    class Meta:
        verbose_name = _('cart')
        verbose_name_plural = _('carts')
        ordering = ('-creation_date',)

    def __unicode__(self):
        return unicode(self.creation_date)

class ItemManager(models.Manager):
    def get(self, *args, **kwargs):
        if 'equipment' in kwargs:
            kwargs['content_type'] = ContentType.objects.get_for_model(type(kwargs['equipment']))
            kwargs['object_id'] = kwargs['equipment'].pk
            del(kwargs['equipment'])
        return super(ItemManager, self).get(*args, **kwargs)

class Item(models.Model):
    cart = models.ForeignKey(Cart, verbose_name=_('cart'))
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()

    objects = ItemManager()

    class Meta:
        verbose_name = _('item')
        verbose_name_plural = _('items')
        ordering = ('cart',)

    def __unicode__(self):
        return u'%d units of %s' % (self.quantity, self.equipment.__class__.__name__)

    # product
    def get_product(self):
        return self.content_type.get_object_for_this_type(pk=self.object_id)

    def set_product(self, equipment):
        self.content_type = ContentType.objects.get_for_model(type(equipment))
        self.object_id = equipment.pk

When trying to go the view displaying current cart, I got this problem:

RecursionError at /calbase/cart/ maximum recursion depth exceeded in comparison

and basically it repeats calling the following :

cart = Cart(request, creation_date=datetime.datetime.now()) 
cart = self.new(request) 
cart = Cart(request, creation_date=datetime.datetime.now()) 
cart = self.new(request) 

.....

I am aware that this is because I am calling init when doing cart = Cart(...) and this again go back to cart = self.new(request) and tried several ways to fix this, in vain. Could somebody help me?

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/calbase/cart/

Django Version: 1.10
Python Version: 3.5.2
Installed Applications:
['calbase.apps.CalbaseConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'haystack',
 'whoosh']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py" in inner
  39.             response = get_response(request)

File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\views.py" in get_cart
  72.     return render_to_response('cart.html', dict(cart=Cart(request)))

File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\cart.py" in __init__
  22.             cart = self.new(request)

File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\cart.py" in __init__
  22.             cart = self.new(request)

File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\cart.py" in new
  30.         cart = Cart(request, creation_date=datetime.datetime.now())

Exception Type: RecursionError at /calbase/cart/
Exception Value: maximum recursion depth exceeded in comparison
2
  • Post the full traceback. One single line doesn't tell anything. Also, there seems to be a typo in remove method. It should be raise Item.DoesNotExist - you missed the dot. Commented Aug 23, 2016 at 19:17
  • @xyres sure. Posted. And I made ItemDoesNotExist a exception Commented Aug 24, 2016 at 13:43

1 Answer 1

2

You have two separate classes called Cart. Rename one of ,them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.