Esempio n. 1
0
def addBasket(request, product_id, qty):
    # ToDo figure out this tax stuff
    tax = 1
    stockrecord = get_object_or_404(StockRecords, product_id=product_id)
    if (qty > stockrecord.num_in_stock):
        errorMsg = "Quantity is greater than {0}.".format(
            stockrecord.num_in_stock)
        return HttpResponseBadRequest(json.dumps({'errors': errorMsg}),
                                      content_type='application/json')
    if (qty < 1):
        return HttpResponseBadRequest(json.dumps(
            {'errors': "Quantity cannot be less than 1."}),
                                      content_type='application/json')

    currentproduct = get_object_or_404(Product, id=product_id)
    parentproduct = get_object_or_404(Product, id=currentproduct.parent_id)
    price_excl_tax = convert_to_currency(stockrecord.price_excl_tax * qty)
    price_incl_tax = convert_to_currency(
        (stockrecord.price_excl_tax * qty) * tax)
    image = get_list_or_empty(ProductImages, product_id=parentproduct.id)

    basket = request.basket
    line_quantity = basket.line_quantity(product=currentproduct,
                                         stockrecord=stockrecord)
    line_ref = basket._create_line_reference(product=currentproduct,
                                             stockrecord=stockrecord,
                                             options=None)
    if line_quantity == 0:
        # item not in the basket line
        basket.add_product(currentproduct, qty)
        # Send signal for basket addition
        add_signal.send(sender=None,
                        product=currentproduct,
                        user=request.user,
                        request=request)
        basketline = get_list_or_empty(Line,
                                       line_reference=line_ref,
                                       basket_id=basket.id)[0]

    else:
        # item already in the basket_line but add to the qty
        basketline = Line.objects.get(basket=basket,
                                      product=currentproduct,
                                      line_reference=line_ref)
        basketline.quantity = qty
        basketline.save(update_fields=["quantity"])

    cartInfo = cartInfoJson(basket, basketline, currentproduct, parentproduct,
                            stockrecord, qty, image)

    return HttpResponse(json.dumps(cartInfo, use_decimal=True),
                        content_type='application/json')
Esempio n. 2
0
def load_cart(request):
    if request.method == 'GET':
        cartItems = []
        basket = request.basket
        index = 0
        ret = request.POST
        basketlines = get_list_or_empty(Line, basket_id=basket)
        if len(basketlines) > 0:
            for basketline in basketlines:
                currentproduct = get_object_or_404(Product,
                                                   id=basketline.product_id)
                parentproduct = get_object_or_404(Product,
                                                  id=currentproduct.parent_id)
                image = get_list_or_empty(ProductImages,
                                          product_id=parentproduct.id)
                stockrecord = get_object_or_404(
                    StockRecords, product_id=basketline.product_id)
                price_excl_tax = convert_to_currency(basketline.price_excl_tax)
                cartInfo = cartInfoJson(basket, basketline, currentproduct,
                                        parentproduct, stockrecord,
                                        basketline.quantity, image)
                cartItems.append(cartInfo)

        return HttpResponse(json.dumps(
            sorted(cartItems, key=lambda k: k['shop'])),
                            content_type='application/json')
Esempio n. 3
0
def cartInfoJson(basket, basketline, currentproduct, parentproduct,
                 stockrecord, qty, image):
    color, size = get_single_variant(currentproduct)
    return {
        'Id':
        basketline.id,
        'product_id':
        currentproduct.id,
        'title':
        currentproduct.title,
        'description':
        strip_tags(parentproduct.description),
        'price':
        convert_to_currency(stockrecord.price_excl_tax),
        'subtotal':
        convert_to_currency(stockrecord.price_excl_tax * qty),
        'image':
        str(
            get_thumbnailer(image[0].original).get_thumbnail({
                'size': (400, 400),
                'box': image[0].cropping,
                'crop': True,
                'detail': True,
            })),
        'qty':
        qty,
        'color':
        color,
        'size':
        size,
        'currentStock':
        stockrecord.num_in_stock,
        'total':
        convert_to_currency(basket.total_excl_tax),
        'shop':
        currentproduct.shop.name,
        'shopSlug':
        currentproduct.shop.slug,
        'msg':
        ''
    }
Esempio n. 4
0
def get_min_price(item):
    return str(convert_to_currency(item.min_child_price_excl_tax))
Esempio n. 5
0
def cart_total(request):
    basket = request.basket
    return HttpResponse(json.dumps(
        {'total': convert_to_currency(basket.total_excl_tax)},
        use_decimal=True),
                        content_type='application/json')