diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 1a9b6f37d4..288e3451ad 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1200,6 +1200,13 @@ class InvenTreeUserSetting(BaseInvenTreeSetting): 'validator': bool, }, + 'NOTIFICATION_SEND_EMAILS': { + 'name': _('Enable email notifications'), + 'description': _('Allow sending of emails for event notifications'), + 'default': True, + 'validator': bool, + }, + "LABEL_INLINE": { 'name': _('Inline label display'), 'description': _('Display PDF labels in the browser, instead of downloading as a file'), diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index 6ba7b6d659..9151f9b879 100644 --- a/InvenTree/common/notifications.py +++ b/InvenTree/common/notifications.py @@ -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().values_list('email', flat=True) 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') diff --git a/InvenTree/templates/InvenTree/notifications/inbox.html b/InvenTree/templates/InvenTree/notifications/inbox.html index 7ec2a27b6a..d8f5190860 100644 --- a/InvenTree/templates/InvenTree/notifications/inbox.html +++ b/InvenTree/templates/InvenTree/notifications/inbox.html @@ -10,7 +10,7 @@ {% endblock %} {% block actions %} -
{% trans "No unread notifications" %}
`; + html = `{% trans "No unread notifications" %}
`; } else { // build up items response.forEach(function(item, index) {