Add ability to send email, when it was not sent before
This commit is contained in:
parent
ae9d7cf471
commit
88e7e0edcc
|
|
@ -105,18 +105,19 @@ class NotificationService:
|
||||||
)
|
)
|
||||||
emailed = False
|
emailed = False
|
||||||
try:
|
try:
|
||||||
if NotificationService._is_duplicate_notification(
|
notification = NotificationService._find_duplicate_notification(
|
||||||
recipient=recipient,
|
recipient=recipient,
|
||||||
verb=verb,
|
verb=verb,
|
||||||
notification_type=notification_type,
|
notification_type=notification_type,
|
||||||
target_url=target_url,
|
target_url=target_url,
|
||||||
template_name=email_template.name,
|
template_name=email_template.name,
|
||||||
template_data=template_data,
|
template_data=template_data,
|
||||||
):
|
)
|
||||||
log.info("A duplicate notification was omitted from being sent")
|
emailed = False
|
||||||
return f"{notification_identifier}_duplicate"
|
if notification and notification.emailed:
|
||||||
|
emailed = True
|
||||||
|
|
||||||
if cls._should_send_email(notification_type, recipient):
|
if cls._should_send_email(notification_type, recipient) and not emailed:
|
||||||
log.debug("Try to send email")
|
log.debug("Try to send email")
|
||||||
try:
|
try:
|
||||||
emailed = cls._send_email(
|
emailed = cls._send_email(
|
||||||
|
|
@ -136,25 +137,35 @@ class NotificationService:
|
||||||
|
|
||||||
if emailed:
|
if emailed:
|
||||||
notification_identifier += "_emailed"
|
notification_identifier += "_emailed"
|
||||||
|
if notification:
|
||||||
|
notification.emailed = True
|
||||||
|
notification.save()
|
||||||
else:
|
else:
|
||||||
log.debug("Should not send email")
|
log.debug("Should not send email")
|
||||||
|
|
||||||
response = notify.send(
|
if notification:
|
||||||
sender=sender,
|
log.info("Duplicate notification was omitted from being sent")
|
||||||
recipient=recipient,
|
notification_identifier += "_duplicate"
|
||||||
verb=verb,
|
return notification_identifier
|
||||||
emailed=emailed,
|
|
||||||
# The metadata is saved in the 'data' member of the AbstractNotification model
|
else:
|
||||||
email_template=email_template.name,
|
response = notify.send(
|
||||||
template_data=template_data,
|
sender=sender,
|
||||||
)
|
recipient=recipient,
|
||||||
sent_notification: Notification = response[0][1][0] # 🫨
|
verb=verb,
|
||||||
sent_notification.target_url = target_url
|
emailed=emailed,
|
||||||
sent_notification.notification_type = notification_type
|
# The metadata is saved in the 'data' member of the AbstractNotification model
|
||||||
sent_notification.course = course
|
email_template=email_template.name,
|
||||||
sent_notification.actor_avatar_url = actor_avatar_url
|
template_data=template_data,
|
||||||
sent_notification.save()
|
)
|
||||||
log.info("Notification sent successfully", emailed=emailed)
|
|
||||||
|
sent_notification: Notification = response[0][1][0] # 🫨
|
||||||
|
sent_notification.target_url = target_url
|
||||||
|
sent_notification.notification_type = notification_type
|
||||||
|
sent_notification.course = course
|
||||||
|
sent_notification.actor_avatar_url = actor_avatar_url
|
||||||
|
sent_notification.save()
|
||||||
|
log.info("Notification sent successfully", emailed=emailed)
|
||||||
return f"{notification_identifier}_success"
|
return f"{notification_identifier}_success"
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(
|
log.error(
|
||||||
|
|
@ -192,14 +203,14 @@ class NotificationService:
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _is_duplicate_notification(
|
def _find_duplicate_notification(
|
||||||
recipient: User,
|
recipient: User,
|
||||||
verb: str,
|
verb: str,
|
||||||
notification_type: NotificationType,
|
notification_type: NotificationType,
|
||||||
template_name: str,
|
template_name: str,
|
||||||
template_data: dict,
|
template_data: dict,
|
||||||
target_url: str | None,
|
target_url: str | None,
|
||||||
) -> bool:
|
) -> Notification | None:
|
||||||
"""Check if a notification with the same parameters has already been sent to the recipient.
|
"""Check if a notification with the same parameters has already been sent to the recipient.
|
||||||
This is to prevent duplicate notifications from being sent and to protect against potential programming errors.
|
This is to prevent duplicate notifications from being sent and to protect against potential programming errors.
|
||||||
"""
|
"""
|
||||||
|
|
@ -212,4 +223,4 @@ class NotificationService:
|
||||||
"email_template": template_name,
|
"email_template": template_name,
|
||||||
"template_data": template_data,
|
"template_data": template_data,
|
||||||
},
|
},
|
||||||
).exists()
|
).first()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue