From f3bc397b99d2cd4dcb78d3a1c1416c8a1457cf8f Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 28 Mar 2018 08:12:31 +0300 Subject: [PATCH] finance email --- finance/views.py | 33 ++++++++++++++++--------- templates/mail/sales/back_set_bill.html | 6 +++++ templates/mail/sales/back_set_bill.txt | 6 +++++ 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 templates/mail/sales/back_set_bill.html create mode 100644 templates/mail/sales/back_set_bill.txt diff --git a/finance/views.py b/finance/views.py index 10c1077..677af39 100644 --- a/finance/views.py +++ b/finance/views.py @@ -13,6 +13,9 @@ from rest_framework.response import Response from rest_framework.views import APIView from yandex_money.models import Payment from django.conf import settings +from django.core.mail import EmailMultiAlternatives +from django.template.loader import get_template +from django.template import Context from courses.models import Course from courses.api import CourseParamsApi @@ -154,20 +157,26 @@ class InvoiceDetailView(APIView): ) invoice.yandex_pay = yandex_pay - msg = EmailMessage( - 'Выставлен новый счёт.', - '''Менеджер %s выставил счёт пользователю %s на курс "%s".''' - % ( - invoice.bill.opener.get_full_name(), - invoice.bill.user.email, - Course.objects.get(token=invoice.bill.course_token).title, - ), - 'robo@skillbox.ru', - [invoice.bill.opener.email], - bcc=['dmitry.dolya@skillbox.ru'], - ) + plaintext = get_template('mail/sales/back_set_bill.txt') + html = get_template('mail/sales/back_set_bill.txt') + + context = Context({ + 'user_email': invoice.bill.user.email, + 'opener_full_name': invoice.bill.opener.get_full_name(), + 'course_title': Course.objects.get(token=invoice.bill.course_token).title, + 'date': str(invoice.date), + 'price': invoice.price, + }) + + subject, from_email, to = 'Выставлен новый счёт', 'robo@skillbox.ru', 'invoice.bill.opener.email' + text_content = plaintext.render(context) + html_content = html.render(context) + + msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) + msg.attach_alternative(html_content, "text/html") msg.send() + invoice.save() return Response(InvoiceSerializer(invoice).data, status=200) diff --git a/templates/mail/sales/back_set_bill.html b/templates/mail/sales/back_set_bill.html new file mode 100644 index 0000000..639ed4e --- /dev/null +++ b/templates/mail/sales/back_set_bill.html @@ -0,0 +1,6 @@ +Выставлен новый счет
+Пользователь: {{ user_email }}
+Продавец: {{ opener_full_name }}
+Курс: {{ course_title }}
+Дата продажи: {{ date }}
+Сумма: {{ price }} руб.
\ No newline at end of file diff --git a/templates/mail/sales/back_set_bill.txt b/templates/mail/sales/back_set_bill.txt new file mode 100644 index 0000000..982497f --- /dev/null +++ b/templates/mail/sales/back_set_bill.txt @@ -0,0 +1,6 @@ +Выставлен новый счет +Пользователь: {{ user_email }} +Продавец: {{ opener_full_name }} +Курс: {{ course_title }} +Дата продажи: {{ date }} +Сумма: {{ price }} руб. \ No newline at end of file