From d0777187f79bd0ec96b746e0fc41021548430cd5 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Sun, 19 Apr 2020 21:15:07 +0200 Subject: [PATCH] Move both send_mail utils to same file --- cotisations/utils.py | 22 +++------------------- re2o/mail_utils.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cotisations/utils.py b/cotisations/utils.py index 5da8eee0..c7ca6187 100644 --- a/cotisations/utils.py +++ b/cotisations/utils.py @@ -23,9 +23,7 @@ import os from django.template.loader import get_template from django.core.mail import EmailMessage -from django.utils.translation import ugettext_lazy as _ -from django.contrib import messages -from smtplib import SMTPException +from re2o.mail_utils import send_mail_object from .tex import create_pdf from preferences.models import AssoOption, GeneralOption, CotisationsOption, Mandate @@ -46,20 +44,6 @@ def find_payment_method(payment): return None -def send_mail(mail, request): - """Wrapper for Django's EmailMessage.send which handles errors""" - try: - mail.send() - except (SMTPException, ConnectionError) as e: - if request: - messages.error( - request, - _("Failed to send email: %(error)s.") % { - "error": e, - }, - ) - - def send_mail_invoice(invoice, request=None): """Creates the pdf of the invoice and sends it by email to the client""" purchases_info = [] @@ -108,7 +92,7 @@ def send_mail_invoice(invoice, request=None): attachments=[("invoice.pdf", pdf, "application/pdf")], ) - send_mail(mail, request) + send_mail_object(mail, request) def send_mail_voucher(invoice, request=None): @@ -145,4 +129,4 @@ def send_mail_voucher(invoice, request=None): attachments=[("voucher.pdf", pdf, "application/pdf")], ) - send_mail(mail, request) + send_mail_object(mail, request) diff --git a/re2o/mail_utils.py b/re2o/mail_utils.py index 86d446fb..72dbac81 100644 --- a/re2o/mail_utils.py +++ b/re2o/mail_utils.py @@ -43,3 +43,17 @@ def send_mail(request, *args, **kwargs): "error": e, }, ) + + +def send_mail_object(mail, request): + """Wrapper for Django's EmailMessage.send which handles errors""" + try: + mail.send() + except (SMTPException, ConnectionError) as e: + if request: + messages.error( + request, + _("Failed to send email: %(error)s.") % { + "error": e, + }, + )