def __save_order_to_db(cart, client, address): order = Order() order = add_order_products_to_order(order, cart) order.client = client order.address = address order.order_date = datetime.datetime.now() order.save()
def make_order_view(request): '''Page order''' 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) images = ThemeFront.objects.all() categories = Category.objects.filter(priority=True) context = { 'cart': cart, 'images': images, 'categories': categories, } context = {**context, **context_1} if form.is_valid(): name = form.cleaned_data['name'] last_name = form.cleaned_data['last_name'] phone = form.cleaned_data['phone'] buying_type = form.cleaned_data['buying_type'] sail = form.cleaned_data['sail'] date_delivery = form.cleaned_data['date_delivery'] address_true = form.cleaned_data['address_true'] comment = form.cleaned_data['comment'] new_order = Order() new_order.user = request.user new_order.save() new_order.items.add(cart) new_order.first_name = name new_order.last_name = last_name new_order.phone = phone new_order.buying_type = buying_type new_order.sail = sail new_order.date_delivery = date_delivery new_order.address = address_true new_order.comment = comment new_order.total = cart.cart_total new_order.save() del request.session['cart_id'] del request.session['total'] return HttpResponseRedirect(reverse('thank_you')) return render(request, 'thank_you.html', context=context)
def create_order(req): if req.method != 'POST': return nl_render(req, 'pages/access_denied.html') p = dict(req.POST) cart = Cart(req) order = Order() order.first_name = p['first_name'][0] order.phone = p['phone_number'][0] order.city = p['city'][0] order.street = p['street'][0] order.building = p['building'][0] order.comment = p['comment'][0] last_order = Order.objects.all().order_by('-id')[0] if last_order: order.number = last_order.number + 1 order.save() for item in cart: order.items.add(item.product) cart.check_out() subj = 'Получен заказ #%s на сумму %s' % ( order.number, order.total_price(), ) message = ''' Поступил заказ #{number} на сумму {price}. ФИО: {name} Телефон: {phone} Адрес: {address} Комментарий: {comment} '''.format(number=order.number, price=order.total_price(), name=order.first_name[0], phone=order.phone[0], comment=order.comment, address=order.address()) send_mail(subj, message, settings.DEFAULT_FROM_EMAIL, [settings.ADMIN_EMAIL], fail_silently=True) return nl_render(req, 'pages/order_success.html')