def process_request(request, product:cmod.Product=None):
    prodict = {}
    prodict['pid'] = product.id
    prodict['max'] = product.get_quantity()
    if product.__class__.__name__ == 'BulkProduct':
        prodict['bulk'] = True
    else:
        prodict['bulk'] = False
    products = cmod.Product.objects.all()
    if product in request.last_five:
        request.last_five.remove(product)
    request.last_five.insert(0, product)
    if len(request.last_five) > 6:
        del request.last_five[-1]
    form = AddToCart(request, initial=prodict)
    form.submit_text = form.buy_now_text
    if form.is_valid():
        if request.user.is_authenticated:
            form.commit()
            cart = request.user.get_shopping_cart()
            cart.recalculate()
            return HttpResponseRedirect('/catalog/cart/')
        else:
            return HttpResponseRedirect('/account/signup/')

    context = {
        'product': product,
        'form': form,
    }
    return request.dmp.render('detail.html', context)
예제 #2
0
def process_request(request, product: cmod.Product = None):
    print(product, ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    if product is None:
        return HttpResponseRedirect('/catalog/index')

    picList = product.image_urls().order_by('id')
    for p in picList:
        print(p.filename)
    type = product.TITLE
    quant = product.get_quantity()
    name = cmod.Category.objects.get(id=product.category_id).name
    user = request.user

    if product in request.last_five:
        request.last_five.remove(product)

    request.last_five.insert(0, product)

    if len(request.last_five) > 6:
        request.last_five.pop()

    form = BuyForm(request, prod=product)
    if form.is_valid():
        if request.user.is_authenticated != True:
            return HttpResponseRedirect('/account/login')

        form.commit(product)
        return HttpResponseRedirect('/catalog/cart')
    form.submit_text = 'Add to Cart'

    context = {
        # sent to index.html:
        'form': form,
        'quant': quant,
        'type': type,
        'product': product,
        'name': name if name is not None else "All Products",
        'catID': product.category,
        'pictures': picList,
        # sent to index.html and index.js:
    }
    return request.dmp.render('details.html', context)