def get_base_context(request): if "user" in request.session: c = RequestContext(request, {"user": request.session["user"]}) c.dicts.append({"user": request.session["user"]}) else: c = RequestContext(request, {}) if "basket" in request.session: basket = request.session["basket"] if not basket: basket = Basket() basket.orders = [] delete_zeros(basket) request.session["basket"] = basket c.dicts.append({"basket": basket}) grs = Product_group.objects.all() links = list(grs) for l in links: tps = Product_type.objects.all().filter(group=l) l.sublinks = list(tps) c.dicts.append({"links": links}) return c
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/')
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))