def cart_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) context = {'cart': cart} return render(request, 'cart.html', context)
def order_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) form = OrderForm(request.POST or None) context = {'cart': cart, 'form': form} return render(request, 'order.html', context)
def product_view(request, product_slug): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) product = Product.objects.get(slug=product_slug) categories = Category.objects.all() context = {'cart': cart, 'product': product, 'categories': categories} return render(request, 'product.html', context)
def change_item_qty(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) qty = request.GET.get('qty') item_id = request.GET.get('item_id') cart_item = CartItem.objects.get(id=int(item_id)) cart_item.qty = int(qty) cart_item.item_total = int(qty) * Decimal(cart_item.product.price) cart_item.save() new_cart_total = 0.00 for i in cart.items.all(): new_cart_total += float(i.item_total) cart.cart_total = new_cart_total cart.save() return JsonResponse({ "cart_total": cart.items.count(), "item_total": cart_item.item_total, 'cart_total_price': cart.cart_total })
def register(request): args = {} args.update(csrf(request)) args['form'] = UserCreationForm() if request.POST: newuser_form = UserCreationForm(request.POST) if newuser_form.is_valid(): newuser_form.save() newuser = auth.authenticate(username=newuser_form.cleaned_data['username'], password=newuser_form.cleaned_data['password2']) auth.login(request, newuser) request.session['visit'] = True cart = Cart() cart.owner = newuser cart.save() return redirect('/') else: args['form'] = newuser_form return render_to_response('register.html', args)
def make_order_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) comp_id = 0 for i in cart.items.all(): comp_id = i.product.owner.id break form = OrderForm(request.POST or None) if form.is_valid(): name = form.cleaned_data['name'] phone = form.cleaned_data['phone'] comments = form.cleaned_data['comments'] new_order = Order() new_order.user = request.user new_order.owner = Company.objects.get(id=comp_id) new_order.save() new_order.items.add(cart) new_order.name = name new_order.phone = phone new_order.comments = comments new_order.total = cart.cart_total new_order.save() slug = slugify( translit(unicode('заказ' + str(new_order.id)), reversed=True)) new_order.slug = slug new_order.save() path = 'media/files/' + new_order.slug + '.xlsx' new_order.path = path new_order.save() del request.session['cart_id'] del request.session['total'] return HttpResponseRedirect(reverse('thanks'))
def add_to_cart_view(request): try: cart_id = request.session['cart_id'] cart = Cart.objects.get(id=cart_id) request.session['total'] = cart.items.count() except: cart = Cart() cart.save() cart_id = cart.id request.session['cart_id'] = cart_id cart = Cart.objects.get(id=cart_id) product_slug = request.GET.get('product_slug') product = Product.objects.get(slug=product_slug) cart.add_to_cart(product.slug) new_cart_total = 0.00 for i in cart.items.all(): new_cart_total += float(i.item_total) cart.cart_total = new_cart_total cart.save() return JsonResponse({ 'cart_total': cart.items.count(), 'cart_total_price': cart.cart_total })