Ejemplo n.º 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)
Ejemplo n.º 2
0
def _product_error(request, product, msg):
    brand = product.brands.all()[0]
    category = product.category.all()[0]
    template = find_product_template(product, names_only=True)
    context = {
        "product": product,
        "brand": brand,
        "category": category,
        "error_message": msg,
    }
    return render(request, template, context)
Ejemplo n.º 3
0
def _product_error(request, product, msg):
    brand = product.brands.all()[0]
    category = product.category.all()[0]
    template = find_product_template(product)
    context = RequestContext(request, {
        'product': product,
        'brand': brand,
        'category': category,
        'error_message': msg
    })
    return HttpResponse(template.render(context))
Ejemplo n.º 4
0
def _product_error(request, product, msg):
    brand = product.brands.all()[0]
    category = product.category.all()[0]
    template = find_product_template(product, names_only=True)
    context = {
        "product": product,
        "brand": brand,
        "category": category,
        "error_message": msg,
    }
    return render(request, template, context)
Ejemplo n.º 5
0
def _product_error(request, product, msg):
    brand = product.brands.all()[0]
    category = product.category.all()[0]
    template = find_product_template(product)
    context = RequestContext(
        request, {
            'product': product,
            'brand': brand,
            'category': category,
            'error_message': msg
        })
    return HttpResponse(template.render(context))
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
def wishlist_add(request):
    """Add an item to the wishlist."""
    try:
        contact = Contact.objects.from_request(request)
    except Contact.DoesNotExist:
        return _wishlist_requires_login(request)

    log.debug("FORM: %s", request.POST)
    formdata = request.POST.copy()
    productslug = None
    if formdata.has_key("productname"):
        productslug = formdata["productname"]
    try:
        product, details = product_from_post(productslug, formdata)
        template = find_product_template(product)

    except (Product.DoesNotExist, MultiValueDictKeyError):
        log.debug("Could not find product: %s", productslug)
        return bad_or_missing(request, _("The product you have requested does not exist."))

    wish = ProductWish.objects.create_if_new(product, contact, details)
    url = urlresolvers.reverse("satchmo_wishlist_view")
    return HttpResponseRedirect(url)
Ejemplo n.º 8
0
def _product_error(request, product, msg):
    template = find_product_template(product)
    context = RequestContext(request, {
        'product': product,
        'error_message': msg})
    return HttpResponse(template.render(context))