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/')