From 5d9f671b323ba1836ce5d4997423ff9853af3a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Br=C3=BCck?= Date: Mon, 18 Nov 2024 09:39:27 +0100 Subject: [PATCH] Fix sending out cancellation emails --- bakeup/shop/models.py | 6 +++--- bakeup/shop/views.py | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bakeup/shop/models.py b/bakeup/shop/models.py index fed13bb..10747e9 100644 --- a/bakeup/shop/models.py +++ b/bakeup/shop/models.py @@ -1038,7 +1038,7 @@ def send_order_confirm_email(self, request): except Exception: logger.exception("Sending order confirm email failed.", stack_info=True) - def send_order_cancellation_email(self, request): + def generate_order_cancellation_email(self, request): from bakeup.pages.models import BrandSettings, EmailSettings try: @@ -1074,10 +1074,10 @@ def send_order_cancellation_email(self, request): [user_email], ) message.content_subtype = "html" - message.send(fail_silently=False) + return message except Exception: logger.exception( - "Sending order cancellation email failed.", stack_info=True + "Generating order cancellation email failed.", stack_info=True ) diff --git a/bakeup/shop/views.py b/bakeup/shop/views.py index f1ad6d6..72058d8 100644 --- a/bakeup/shop/views.py +++ b/bakeup/shop/views.py @@ -176,11 +176,21 @@ def customer_order_add_or_update(request, production_day): customer=request.user.customer, ) logger.error("Order #%s: order will be completely deleted!", customer_order) - customer_order.delete() + email = None if EmailSettings.load( request_or_site=request ).send_email_order_cancellation: - customer_order.send_order_cancellation_email(request) + email = customer_order.generate_order_cancellation_email(request) + customer_order.delete() + if email: + try: + email.send(fail_silently=False) + except Exception as e: + logger.error( + "Order #%s: Error while sending cancellation email: %s", + customer_order, + e, + ) messages.add_message( request, messages.INFO,