def post(self, request, *args, **kwargs): def save_order(form_, cart_): """Save order to DB and to session.""" order_ = form_.save() order_.set_positions(cart_) cart_.clear() save_order_to_session(request.session, order_) return order_ self.object = self.get_object() # Define this attr, for get_context_data method context = self.get_context_data(request=request) cart, form = context['cart'], context['form'] # sometimes we have two posts in a row if cart.is_empty(): return redirect('/', permanent=True) if not form.is_valid(): return render(request, self.template_name, context) order = save_order(form, cart) mailer.send_order( subject=settings.EMAIL_SUBJECTS['order'], order=order, **self.email_extra_context ) return redirect(self.success_url)
def send_mail_to_customer(order): mailer.send_order( subject=settings.EMAIL_SUBJECTS['yandex_order'], order=order, to_shop=False, # see se.OrderPage class for a detail about `shop` context. extra_context={'shop': settings.SHOP}, )
def test_mail_send_works(self): """If we send email, Django will put it in mail.outbox collection.""" for i in range(10): mailer.send_order( subject='Testing email', order=self.order, ) self.assertEqual(len(mail.outbox), i + 1)
def test_mailer_construct_valid_email(self): """Saved email contains valid subject and valid body.""" mailer.send_order( subject='Testing email 1', order=self.order, ) sent_mail = mail.outbox[0] self.assertEqual(sent_mail.subject, 'Testing email 1') self.assertIn('Thank you, {}'.format(self.order.name), sent_mail.body)
def test_mailer_will_send_order_to_specified_recipients(self): """Saved email contains valid set of recipients.""" mailer.send_order( subject='Testing email 1', order=self.order, ) sent_mail = mail.outbox[0] recipients = [self.order.email, *settings.EMAIL_RECIPIENTS] self.assertListEqual(recipients, sent_mail.recipients()) mailer.send_order(subject='Testing email 1', order=self.order, to_customer=False) sent_mail = mail.outbox[1] recipients = settings.EMAIL_RECIPIENTS self.assertListEqual(recipients, sent_mail.recipients())
def send_order_emails(admin_model, request, order_qs): for order in order_qs: context = {'shop': settings.SHOP} if order.email: mailer.send_order( subject=settings.EMAIL_SUBJECTS['order'], order=order, extra_context=context, ) else: mailer.send_order( subject=settings.EMAIL_SUBJECTS['one_click'], order=order, to_customer=False, extra_context=context, )
def send_mail_to_shop(order): paid, profit = get_keys_from_post(request, 'orderSumAmount', 'shopSumAmount') proportion = 100 * float(profit) / float(paid) commission = round(100 - proportion, 2) # in percents mailer.send_order( template='ecommerce/yandex_order_email.html', subject=settings.EMAIL_SUBJECTS['yandex_order'], order=order, to_customer=False, extra_context={ 'paid': paid, 'profit': profit, 'commission': commission, # see se.OrderPage class for a detail about `shop` context. 'shop': settings.SHOP, })
def test_mailer_will_send_order_to_specified_recipients(self): """Saved email contains valid set of recipients.""" mailer.send_order( subject='Testing email 1', order=self.order, ) sent_mail = mail.outbox[0] recipients = [self.order.email, *settings.EMAIL_RECIPIENTS] self.assertListEqual(recipients, sent_mail.recipients()) mailer.send_order( subject='Testing email 1', order=self.order, to_customer=False ) sent_mail = mail.outbox[1] recipients = settings.EMAIL_RECIPIENTS self.assertListEqual(recipients, sent_mail.recipients())
def one_click_buy(request): """ Handle one-click-buy. Accept XHR, save Order to DB, send mail about it and return 200 OK. """ SECart(request.session).clear() cart = SECart(request.session) product = get_object_or_404(Product, id=request.POST['product']) cart.add(product, int(request.POST['quantity'])) order = Order(phone=request.POST['phone']) order.set_positions(cart) ec_views.save_order_to_session(request.session, order) mailer.send_order( subject=settings.EMAIL_SUBJECTS['one_click'], order=order, to_customer=False, ) return HttpResponse('ok')
def one_click_buy(request): """ Handle one-click-buy. Accept XHR, save Order to DB, send mail about it and return 200 OK. """ cart = SECart(request.session) cart.clear() product = get_object_or_404(Product, id=request.POST['product']) cart.add(product, int(request.POST['quantity'])) order = Order(phone=request.POST['phone']) order.set_positions(cart) ec_views.save_order_to_session(request.session, order) mailer.send_order( subject=settings.EMAIL_SUBJECTS['one_click'], order=order, to_customer=False, # see se.OrderPage class for a detail about `shop` context. extra_context={'shop': settings.SHOP}, ) return HttpResponse('ok')