Email notifications are only sent for users who have the setting enabled

This commit is contained in:
Oliver Walters 2022-03-20 19:31:59 +11:00
parent d2969d0235
commit 5f98cdf3c7

View File

@ -8,15 +8,19 @@ from allauth.account.models import EmailAddress
from InvenTree.helpers import inheritors
from InvenTree.ready import isImportingData
from common.models import NotificationEntry, NotificationMessage
from common.models import InvenTreeUserSetting
import InvenTree.tasks
logger = logging.getLogger('inventree')
# region notification classes
# region base classes
class NotificationMethod:
"""
Base class for notification methods
"""
METHOD_NAME = ''
CONTEXT_BUILTIN = ['name', 'message', ]
CONTEXT_EXTRA = []
@ -95,10 +99,8 @@ class SingleNotificationMethod(NotificationMethod):
class BulkNotificationMethod(NotificationMethod):
def send_bulk(self):
raise NotImplementedError('The `send` method must be overriden!')
# endregion
# region implementations
class EmailNotification(BulkNotificationMethod):
METHOD_NAME = 'mail'
CONTEXT_EXTRA = [
@ -108,13 +110,26 @@ class EmailNotification(BulkNotificationMethod):
]
def get_targets(self):
"""
Return a list of target email addresses,
only for users which allow email notifications
"""
allowed_users = []
for user in self.targets:
allows_emails = InvenTreeUserSetting.get_setting('NOTIFICATION_SEND_EMAILS', user=user)
if allows_emails:
allowed_users.append(user)
return EmailAddress.objects.filter(
user__in=self.targets,
user__in=allowed_users,
)
def send_bulk(self):
html_message = render_to_string(self.context['template']['html'], self.context)
targets = self.targets.values_list('email', flat=True)
targets = self.get_targets()
InvenTree.tasks.send_email(self.context['template']['subject'], '', targets, html_message=html_message)
@ -137,20 +152,20 @@ class UIMessageNotification(SingleNotificationMethod):
message=self.context['message'],
)
return True
# endregion
# endregion
def trigger_notifaction(obj, category=None, obj_ref='pk', targets=None, target_fnc=None, target_args=[], target_kwargs={}, context={}):
"""
Send out an notification
Send out a notification
"""
# check if data is importet currently
# Check if data is importing currently
if isImportingData():
return
# Resolve objekt reference
obj_ref_value = getattr(obj, obj_ref)
# Try with some defaults
if not obj_ref_value:
obj_ref_value = getattr(obj, 'pk')