def checkout(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # clear the cart cart.clear() # launch asynchronous task order_created.delay(order.id) # set the order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) else: form = OrderCreateForm() context = {'cart': cart, 'form': form} return render(request, 'orders/checkout.html', context)
def post(self, request, *args, **kwargs): if not request.user.is_authenticated: return JsonResponse({"authenticated": False}, status=403) cart = Cart(request) form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() data = {"order_id": order.id} return JsonResponse(data) else: return JsonResponse({}, status=401)
def order_create_view(request): cart = Cart(request) if request.method == 'POST': order_form = OrderCreateForm(request.POST) if order_form.is_valid(): order = order_form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # отправка письма на email, используя celery try: order_created_mail.delay(order.id) except OperationalError: order_created_mail(order.id) context = { 'order': order, } return render(request, 'orders/order_thanks.html', context) else: order_form = OrderCreateForm() context = { 'cart': cart, 'order_form': order_form, } return render(request, 'orders/order_create.html', context)
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # clear the cart cart.clear() order_created.delay(order.id) # set the order in the session request.session['order_id'] = order.id # redirect to the payment return redirect(reverse('payment:process')) # return render(request, # 'orders/order/created.html', # {'orders': order}) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', { 'cart': cart, 'form': form })
def order_create(request): cart = Cart(request) form = OrderCreateForm() if request.method == 'POST': print("I'm here") form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['fee'], quantity=item['quantity']) #send email body_email = "Cám ơn bạn! Đơn hàng của bạn đã được đặt thành công, mã đơn hàng là " + str( order.id) email = EmailMessage(subject='Thông tin đặt hàng', body=body_email, to=[order.email], from_email='*****@*****.**') email.send() #remove giỏ hàng cart.clear() return render(request, 'orders/created.html', context={'order': order}) else: form = OrderCreateForm() return render(request, 'orders/create.html', context={'form': form})
def order_create(request): cart = Cart(request) if cart.get_total_count() == 0: return redirect('cart:cart_detail') if not auth.get_user(request).is_authenticated(): return redirect('auth:register') if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() order.users = auth.get_user(request) order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['prices'], count=item['count_product']) cart.clear() args = {'order': order} thread = threading.Thread(target=send_mail, args=(order.id,)) thread.start() return render(request, 'orders/order_created.html', args) form = OrderCreateForm() args = { 'cart': cart, 'form_order': form, } return render(request, 'orders/order_create.html', args)
def order_create(request): cart = Cart(request) order = Order(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] email = form.cleaned_data['email'] address = form.cleaned_data['address'] phone_no = form.cleaned_data['phone_no'] city = form.cleaned_data['city'] order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() order: Order(request) #launch assy. tasks #order_created.delay(order.id) #set the order in the session request.session['order_id'] = order.id #redirect to payment return redirect(reverse('payment:process')) else: form = OrderCreateForm() return render(request, 'orders/create.html', {'cart': cart, 'form': form})
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create( order=order, product=item['product'], price=item['price'], quantity=item['quantity'] ) # clear the cart logger.info("Clearing cart after successful post") cart.clear() logger.info(f"Launching asynchronous email task for order {order.id}") order_created.delay(order.id) # set order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # launch asynchronous task order_created.delay(order.id) # set the order in the session request.session['order_id'] = order.id # redirect for payment return redirect(reverse('payment:process')) form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def test_invalid_address_field(self): form_data = { 'first_name': FIRST_NAME, 'last_name': LAST_NAME, 'email': EMAIL, 'address': '' } form = OrderCreateForm(data=form_data) self.assertFalse(form.is_valid())
def test_invalid_email_field(self): form_data = { 'first_name': FIRST_NAME, 'last_name': LAST_NAME, 'email': 'ivan', 'address': ADDRESS } form = OrderCreateForm(data=form_data) self.assertFalse(form.is_valid())
def test_form_is_valid(self): form_data = { 'first_name': FIRST_NAME, 'last_name': LAST_NAME, 'email': EMAIL, 'address': ADDRESS } form = OrderCreateForm(data=form_data) self.assertTrue(form.is_valid())
def order_create(request): cart = Cart(request) form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() return render(request.META.get('HTTP_REFERER'))
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): if len(cart) == 0: return redirect('/') order = form.save(commit=False) if order.paymentMethod == 'Card' or order.paymentMethod == 'PayPal': order.paid = True order_id = unique_order_id() if request.user.is_authenticated: order.user = request.user if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.order_id = order_id order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) orderMail(request, order) cart.clear() request.session['coupon_id'] = None if request.user.is_authenticated: update_cart_in_all_user_sessions(request) return render(request, 'orders/created.html', {'order': order}) form = OrderCreateForm(use_required_attribute=False, initial={'city': 'Киев'}) if len(cart) == 0: return redirect('cart:cart_detail') deliveries = Delivery.objects.order_by('price') return render(request, 'orders/checkout.html', { 'form': form, 'deliveries': deliveries })
def order(request): try: the_id = request.session['cart_id'] cart = Cart.objects.get(id=the_id) except: the_id = None return HttpResponseRedirect(reverse('cart_view')) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) order.order_id = the_id order.cart = cart order.save() try: new_order = Order.objects.get(cart=cart) messages.success( request, 'Спасибо за заказ! Ваш заказ №{}. Скоро с Вами свяжется наш менеджер.' .format(the_id)) del request.session['cart_id'] del request.session['items_total'] return HttpResponseRedirect('/') except Order.DoesNotExist: new_order = Order() new_order.cart = cart new_order.order_id = the_id new_order.save() messages.success( request, 'Спасибо за заказ! Ваш заказ №{}. Скоро с Вами свяжется наш менеджер.' .format(the_id)) del request.session['cart_id'] del request.session['items_total'] except: return HttpResponseRedirect(reverse('cart_view')) if new_order.status == 'Finished': cart.delete() return HttpResponseRedirect(reverse('cart_view')) context = {} template_name = 'base.html' return render(request, template_name, context)
def test(request): cart = Cart(request) print(cart) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() form = OrderCreateForm() return render(request, 'rompas/test.html', {'cart': cart, 'form': form, })
def order_create(request): cart = Cart(request) if request.method == "POST": form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() order_notify(order.id) return render(request, 'orders/created.html', {'order': order}) else: form = OrderCreateForm() return render(request, 'orders/create.html', {'cart': cart, 'form': form})
def order_create(request): cart = Cart(request) # fetch the existing cart if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() # create an instance of order for item in cart: # adding the items from cart into an order OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() # clearing the cart after posting an order request.session['order_id'] = order.id #adding id of an order instance to the session return redirect(reverse('payment:process')) else: # if request.method == 'GET': form = OrderCreateForm() return render(request, 'orders/create.html', {'cart': cart, 'form': form})
def order_create(request): cart = Cart(request) form = OrderCreateForm() if request.method == 'POST': print("I'm here") form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['fee'], quantity=item['quantity']) cart.clear() return render(request, 'orders/created.html', context={'order': order}) else: form = OrderCreateForm() return render(request, 'orders/create.html', context={'form': form})
def order_create(request): cart = Cart(request) form = OrderCreateForm() if request.method == 'POST': print("I'm here") form = OrderCreateForm(request.POST) email = request.POST.get('email', '') print(email) if form.is_valid(): order = form.save() data = 'Diamond Shop xin cảm ơn quý khách đã mua sản phẩm <br>'+\ 'Xác nhận thông tin đơn hàng của quý khách như bên dưới'+\ '<table><tbody><tr><td valign="top"> <h2>Chi tiết đơn hàng</h2>' for item in cart: data += '<ul> <li>'+ str(item['quantity']) + 'sản phẩm x '+\ item['product'].name + ' = <span>' + str(item['total_fee']) + '</span>'+\ '</li> </ul>' data += '<ul>Tổng: ' + str( cart.get_total_fee()) + ' vnđ</ul></td></tr></tbody></table>' data += '<br> chúng tôi sẽ liên hệ với quý khách trong thời gian gần nhất' for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['fee'], quantity=item['quantity']) #clear the cart cart.clear() msg = EmailMessage( 'Diamond Shop - xác nhận đơn hàng', data, "PLC", [email], ) msg.content_subtype = "html" msg.send() return render(request, 'orders/created.html', context={'order': order}) else: form = OrderCreateForm() return render(request, 'orders/create.html', context={'form': form})
def OrderCreate(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) order.warehouse = order.nova_warehouse order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) cart.clear() return form_send(request, order=order) form = OrderCreateForm() return render(request, 'orders/order/create.html', { 'cart': cart, 'form': form, })
def order_create(request): cart = Cart(request) if request.method == "POST": form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save() # Order for obj in cart: OrderItemModel.objects.create(order=order, item=obj.get('item'), price=obj.get('price'), quantity=obj.get('quantity')) # clear cart cart.clear() return render(request, "", {"order": order}) # order successful else: form = OrderCreateForm() return render(request, "orders/create_order.html", { "cart": cart, "form": form })
def order_create(request): cart = Cart(request) form = OrderCreateForm(request.POST) user = request.user email = user.email if form.is_valid(): print('valid', email) order = Order.objects.create(email=email) order_id = order.id for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # очистка корзины cart.clear() else: print(form.errors) return HttpResponseRedirect( reverse('orders:order_confirmation', kwargs={'order': order.id}))
def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) if cart.coupon: order.coupon = cart.coupon order.discount = cart.coupon.discount order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) # Clear the cart cart.clear() # launch asynchronous task order_created.delay(order.id) return render(request, 'orders/order/created.html', {'order': order}) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def form_valid(self, form): cart = Cart(self.request) if self.request.method == 'POST': form = OrderCreateForm(self.request.POST) order = form.save() if form.is_valid(): for item in cart: OrderItem.objects.create(order=order, book=item['book'], price=item['price'], quantity=item['quantity']) # очистка корзины cart.clear() return render(self.request, 'orders/created.html', {'order': order}) else: form = OrderCreateForm return render(self.request, 'orders/create.html', { 'cart': cart, 'form': form })
def order_create(request): cart = Cart(request) is_loading = False sent = False if request.user.is_authenticated: if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = form.save(commit=False) order.user = request.user order.pay = True order.save() for item in cart: OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity']) order_items = OrderItem.objects.filter(order=order) # clear the cart cart.clear() # data = { # "email": "*****@*****.**", # "password": "******" # } # response = requests.post('https://uptechpay.com/api/getaccess', data) # access_details = response.json() # access_token = access_details.get('access_token') # headers = { # 'Authorization': 'Bearer ' + access_token # } # payload = { # "user_phone": str(form.cleaned_data.get('phone')), # "transaction_summary": str(order.id), # "business_token": "bXVueXVhcGV0ZXIwN0BnbWFpbC5jb21LS1hwT3dpN1M0YlRlR2Q1Zkg5Nmk4OVhNdlFwUzFhVjAuMzQ3OTg4MDAgMTYwNTEwMDMzMw==", # "amount": 1, # "account_ref": "MCS" # } # print('payload', payload) # invoke_payment = requests.post('https://uptechpay.com/api/businesspay', headers=headers, data=payload) # # if invoke_payment: # is_loading = False # order = Order.objects.get(id=order.pk) # order.paid = True # order.save() # payment_response = invokeSTK.json() # "amount": int(order.get_total_cost()), # Send Email Upon Success order placement # subject = f"Regarding an order you placed from MunyuaClothStore (#{order.pk})" # message = f"Your order has been received and is being prepared. We will contact you when it's ready for delivery \n\n " \ # f"Name: {order.first_name} {order.last_name}\n" \ # f"Phone number: {order.phone}\n" \ # f"Address: {order.address}\n" \ # f"Postal Code: {order.postal_code}\n" \ # f"Total Cost: # {order.get_total_cost() + 200}\n" # # send_mail(subject, message, '*****@*****.**', # [order.email]) sent = True return render( request, 'customer-order.html', { 'order': order, 'order_items': order_items, 'email_sent': sent, }) else: form = OrderCreateForm() return render(request, 'checkout1.html', { 'cart': cart, 'is_loading': is_loading, 'form': form }) else: return redirect('login')