From 7f3f2fef2bcf7d7ca8f22dd5429967ea4f4718d7 Mon Sep 17 00:00:00 2001 From: Marc Date: Sat, 2 Nov 2024 11:58:07 +0100 Subject: [PATCH] Refactor send method by extracting notification logic into a separate function - Created `send_notification_by_channel` method to handle notification sending by channel and make it easier to extend the channels with a new channel - Simplified `send` method by moving channel-based notification logic to the new function --- .../doctype/notification/notification.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/frappe/email/doctype/notification/notification.py b/frappe/email/doctype/notification/notification.py index 4111cce390..eeeb2550f0 100644 --- a/frappe/email/doctype/notification/notification.py +++ b/frappe/email/doctype/notification/notification.py @@ -333,21 +333,8 @@ def get_context(context): if self.is_standard: self.load_standard_properties(context) - try: - if self.channel == "Email": - self.send_an_email(doc, context) - if self.channel == "Slack": - self.send_a_slack_msg(doc, context) - - if self.channel == "SMS": - self.send_sms(doc, context) - - if self.channel == "System Notification" or self.send_system_notification: - self.create_system_notification(doc, context) - - except Exception: - self.log_error("Failed to send Notification") + self.send_notification_by_channel(doc, context) if self.set_property_after_alert: allow_update = True @@ -376,6 +363,20 @@ def get_context(context): except Exception: self.log_error("Document update failed") + def send_notification_by_channel(self, doc, context): + """Send notification based on the specified channel.""" + try: + if self.channel == "Email": + self.send_an_email(doc, context) + elif self.channel == "Slack": + self.send_a_slack_msg(doc, context) + elif self.channel == "SMS": + self.send_sms(doc, context) + elif self.channel == "System Notification" or self.send_system_notification: + self.create_system_notification(doc, context) + except Exception: + self.log_error("Failed to send Notification") + def create_system_notification(self, doc, context): subject = self.subject if "{" in subject: