Esempio n. 1
0
    def get(self, request, order_id):
        order = Order.objects.get(pk=order_id)
        if order.pay_type == 'online':
            order.status = 'paid'
            order.save()

        cart = Cart(request)
        cart.clear()

        send_order_mail(request, order)

        context = {'order': order}
        return render(request, 'orders/order-done.html', context)
Esempio n. 2
0
    def post(request):
        order_form = OrderForm(request.POST)
        cart = Cart(request)

        if order_form.is_valid():
            try:
                new_order = order_form.save(commit=False)
                new_order.total_price = cart.get_total_price()
                new_order.save()

                for item in cart:
                    product = Product.objects.get(id=int(item['product_id']))
                    OrderItem.objects.create(order=new_order,
                                             product=product,
                                             price=item['price'],
                                             quantity=item['quantity'],
                                             total_price=item['cost'],
                                             bar_type=item['bar_type'])

                current_site = get_current_site(request)
                mail_subject = 'Новый заказ на сайте: ' + current_site.domain
                message = render_to_string('email_messages/order_message.html',
                                           {
                                               'domain': current_site.domain,
                                               'order': new_order,
                                           })

                to_email = MailToString.objects.first().email
                email = EmailMessage(mail_subject, message, to=[to_email])
                email.send()
                cart.clear()
                status = 1
            except Exception as e:
                print(e)
                status = 0
        else:
            status = 0

        context = {
            'status': status,
        }
        return JsonResponse(context)