send notification error handling

This commit is contained in:
Setyo Nugroho 2022-09-20 12:45:09 +07:00
parent f5be3a8c3c
commit 235d75fd30

View file

@ -1,3 +1,4 @@
import logging
import re import re
import math import math
@ -15,6 +16,8 @@ from core.component.labels import LABEL_INSTANCES, LABEL_IMAGES, LABEL_SNAPSHOTS
LABEL_VOLUMES LABEL_VOLUMES
from core.utils.model_utils import BaseModel, TimestampMixin, PriceMixin, InvoiceComponentMixin from core.utils.model_utils import BaseModel, TimestampMixin, PriceMixin, InvoiceComponentMixin
LOG = logging.getLogger("yuyu")
# region Dynamic Setting # region Dynamic Setting
class DynamicSetting(BaseModel): class DynamicSetting(BaseModel):
@ -271,24 +274,27 @@ class Notification(BaseModel, TimestampMixin):
def send(self): def send(self):
from core.utils.dynamic_setting import get_dynamic_setting, EMAIL_ADMIN from core.utils.dynamic_setting import get_dynamic_setting, EMAIL_ADMIN
try:
def textify(html):
# Remove html tags and continuous whitespaces
text_only = re.sub('[ \t]+', ' ', strip_tags(html))
# Strip single spaces in the beginning of each line
return text_only.replace('\n ', '\n').strip()
def textify(html): recipient = [get_dynamic_setting(EMAIL_ADMIN)]
# Remove html tags and continuous whitespaces if self.project is not None:
text_only = re.sub('[ \t]+', ' ', strip_tags(html)) recipient.append(self.project.email_notification)
# Strip single spaces in the beginning of each line
return text_only.replace('\n ', '\n').strip()
recipient = [get_dynamic_setting(EMAIL_ADMIN)] send_mail(
if self.project is not None: subject=self.title,
recipient.append(self.project.email_notification) message=textify(self.content),
from_email=settings.DEFAULT_FROM_EMAIL,
send_mail( recipient_list=recipient,
subject=self.title, html_message=self.content,
message=textify(self.content), )
from_email=settings.DEFAULT_FROM_EMAIL, self.sent_status = True
recipient_list=recipient, self.save()
html_message=self.content, except Exception as e:
) LOG.exception('Error sending notification')
self.sent_status = False
self.sent_status = True self.save()
self.save()