Esempio n. 1
0
def add_product (request, pr_id):
    
    pr = Product.objects.all().get(id=pr_id)
    
    if "basket" in request.session:
        basket = request.session['basket']
    else:
        basket = Basket()
        basket.orders = []
    
    
    if is_product_in_basket(basket, pr):
        i = 1
        for o in basket.orders:
            if o.product == pr:
                num = request.POST[str(i)]
                i = i + 1
                o.quantity = int(o.quantity) + int(num)
                o.price = get_last_price(pr_id).value
    else:
        order = Order()
        order.basket = basket
        order.product = pr
        num = request.POST['1']
        order.quantity = num
        order.price = get_last_price(pr_id).value
        if num != 0:
            basket.orders.append(order)
            
    basket.total = get_basket_summ(basket)
    print basket.total
    basket.size = len(basket.orders)
    request.session['basket'] = basket
    
    return HttpResponseRedirect('/basket/')
Esempio n. 2
0
def buy_product (request, pr_id):

    t = get_template('xml/answer.xml')

    login = request.GET['login']
    pas = request.GET['pass']
    total = request.GET['total']
    count = request.GET['count']

    print login
    print pas
    print total
    print count
    
    if is_user_exists(login, pas):
        u = get_user(login, pas)
    else:
        c = RequestContext(request, {'answer':'error'})
        return HttpResponse(t.render(c))

    basket = Basket()
    basket.user_id = u.id
    basket.adding_time = datetime.now()
    basket.address = u.city + ' ' + u.street
    basket.btype = u'R'
    basket.total = total
    basket.save()
    
    pr = get_product_by_id(pr_id)
    pr.quantity = pr.quantity - int(count)
    
    order = Order()
    order.basket = basket
    order.product = pr
    order.quantity = count
    order.price = float(total) / int(count)
    order.save()
     
    print pr.name
    
    print 'NICE'
    
    c = RequestContext(request, {'answer':'ok'})
    return HttpResponse(t.render(c))