예제 #1
0
def add(request, id=0):
    """Add an item to the cart."""
    #TODO: Error checking for invalid combos

    try:
        product = Product.objects.get(slug=request.POST['productname'])
        p_types = product.get_subtypes()
        details = []

        if 'ConfigurableProduct' in p_types:
            # This happens when productname cannot be updated by javascript.
            cp = product.configurableproduct
            chosenOptions = optionset_from_post(cp, request.POST)
            product = cp.get_product_from_options(chosenOptions)

        if 'CustomProduct' in p_types:
            for customfield in product.customproduct.custom_text_fields.all():
                details.append((customfield,
                                request.POST["custom_%s" % customfield.slug]))

        template = find_product_template(product)
    except (Product.DoesNotExist, MultiValueDictKeyError):
        return bad_or_missing(
            request, _('The product you have requested does not exist.'))

    try:
        quantity = int(request.POST['quantity'])
    except ValueError:
        context = RequestContext(
            request, {
                'product': product,
                'error_message': _("Please enter a whole number.")
            })

        return HttpResponse(template.render(context))

    if quantity < 1:
        context = RequestContext(
            request, {
                'product': product,
                'error_message': _("Please enter a positive number.")
            })
        return HttpResponse(template.render(context))

    if request.session.get('cart'):
        cart = Cart.objects.get(id=request.session['cart'])
    else:
        cart = Cart()
        cart.save()  # Give the cart an id

    cart.add_item(product, number_added=quantity, details=details)
    request.session['cart'] = cart.id

    url = urlresolvers.reverse('satchmo_cart')
    return HttpResponseRedirect(url)
예제 #2
0
    def testCartAddVerifyVeto(self):
        """Test that vetoes from `signals.satchmo_cart_add_verify` are caught and cause an error."""
        try:
            cart = Cart()
            cart.save()
            p = ProductFactory()
            cart.add_item(p, 1)
            TestOrderFactory()
            self.fail("Should have thrown a CartAddProhibited error")
        except CartAddProhibited:
            pass

        self.assertEqual(len(cart), 0)
예제 #3
0
파일: cart.py 프로젝트: davemerwin/satchmo
def add(request, id=0):
    """Add an item to the cart."""
    #TODO: Error checking for invalid combos

    try:
        product = Product.objects.get(slug=request.POST['productname'])
        p_types = product.get_subtypes()
        details = []
        
        if 'ConfigurableProduct' in p_types:
            # This happens when productname cannot be updated by javascript.
            cp = product.configurableproduct
            chosenOptions = optionset_from_post(cp, request.POST)
            product = cp.get_product_from_options(chosenOptions)
                
        if 'CustomProduct' in p_types:
            for customfield in product.customproduct.custom_text_fields.all():
                details.append((customfield, request.POST["custom_%s" % customfield.slug]))
            
        template = find_product_template(product)
    except (Product.DoesNotExist, MultiValueDictKeyError):
        return bad_or_missing(request, _('The product you have requested does not exist.'))
        
    try:
        quantity = int(request.POST['quantity'])
    except ValueError:
        context = RequestContext(request, {
            'product': product,
            'error_message': _("Please enter a whole number.")})
        
        return HttpResponse(template.render(context))
        
    if quantity < 1:
        context = RequestContext(request, {
            'product': product,
            'error_message': _("Please enter a positive number.")})
        return HttpResponse(template.render(context))

    if request.session.get('cart'):
        cart = Cart.objects.get(id=request.session['cart'])
    else:
        cart = Cart()
        cart.save() # Give the cart an id
    
    cart.add_item(product, number_added=quantity, details=details)
    request.session['cart'] = cart.id

    url = urlresolvers.reverse('satchmo_cart')
    return HttpResponseRedirect(url)
예제 #4
0
    def test_line_cost(self):
        lb = Product.objects.get(slug__iexact="dj-rocks-l-bl")
        sb = Product.objects.get(slug__iexact="dj-rocks-s-b")

        cart = Cart(site=Site.objects.get_current())
        cart.save()
        cart.add_item(sb, 1)
        self.assertEqual(cart.numItems, 1)
        self.assertEqual(cart.total, Decimal("20.00"))

        cart.add_item(lb, 1)
        self.assertEqual(cart.numItems, 2)
        items = list(cart.cartitem_set.all())
        item1 = items[0]
        item2 = items[1]
        self.assertEqual(item1.unit_price, Decimal("20.00"))
        self.assertEqual(item2.unit_price, Decimal("23.00"))
        self.assertEqual(cart.total, Decimal("43.00"))
예제 #5
0
    def test_line_cost(self):
        lb = Product.objects.get(slug__iexact="dj-rocks-l-bl")
        sb = Product.objects.get(slug__iexact="dj-rocks-s-b")

        cart = Cart()
        cart.save()
        cart.add_item(sb, 1)
        self.assertEqual(cart.numItems, 1)
        self.assertEqual(cart.total, Decimal("20.00"))

        cart.add_item(lb, 1)
        self.assertEqual(cart.numItems, 2)
        items = list(cart.cartitem_set.all())
        item1 = items[0]
        item2 = items[1]
        self.assertEqual(item1.unit_price, Decimal("20.00"))
        self.assertEqual(item2.unit_price, Decimal("23.00"))
        self.assertEqual(cart.total, Decimal("43.00"))