From d2969d0235ee3a593725cb24e42f1871bdfcdf58 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 20 Mar 2022 19:19:42 +1100 Subject: [PATCH 1/5] Adds a per-user setting to allow sending of emails for notification events --- InvenTree/common/models.py | 7 +++++++ .../InvenTree/settings/settings.html | 1 + .../templates/InvenTree/settings/sidebar.html | 2 ++ .../settings/user_notifications.html | 20 +++++++++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 InvenTree/templates/InvenTree/settings/user_notifications.html diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index 56e41dd24b..54da688992 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -1193,6 +1193,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/templates/InvenTree/settings/settings.html b/InvenTree/templates/InvenTree/settings/settings.html index ba27187747..dbb526cfd3 100644 --- a/InvenTree/templates/InvenTree/settings/settings.html +++ b/InvenTree/templates/InvenTree/settings/settings.html @@ -22,6 +22,7 @@ {% include "InvenTree/settings/user_settings.html" %} {% include "InvenTree/settings/user_homepage.html" %} {% include "InvenTree/settings/user_search.html" %} +{% include "InvenTree/settings/user_notifications.html" %} {% include "InvenTree/settings/user_labels.html" %} {% include "InvenTree/settings/user_reports.html" %} {% include "InvenTree/settings/user_display.html" %} diff --git a/InvenTree/templates/InvenTree/settings/sidebar.html b/InvenTree/templates/InvenTree/settings/sidebar.html index 85e0b4ce94..9df6b0736f 100644 --- a/InvenTree/templates/InvenTree/settings/sidebar.html +++ b/InvenTree/templates/InvenTree/settings/sidebar.html @@ -14,6 +14,8 @@ {% include "sidebar_item.html" with label='user-home' text=text icon="fa-home" %} {% trans "Search Settings" as text %} {% include "sidebar_item.html" with label='user-search' text=text icon="fa-search" %} +{% trans "Notifications" as text %} +{% include "sidebar_item.html" with label='user-notifications' text=text icon="fa-bell" %} {% trans "Label Printing" as text %} {% include "sidebar_item.html" with label='user-labels' text=text icon="fa-tag" %} {% trans "Reporting" as text %} diff --git a/InvenTree/templates/InvenTree/settings/user_notifications.html b/InvenTree/templates/InvenTree/settings/user_notifications.html new file mode 100644 index 0000000000..4e9889ca69 --- /dev/null +++ b/InvenTree/templates/InvenTree/settings/user_notifications.html @@ -0,0 +1,20 @@ +{% extends "panel.html" %} + +{% load i18n %} +{% load inventree_extras %} + +{% block label %}user-notifications{% endblock label %} + +{% block heading %}{% trans "Notification Settings" %}{% endblock heading %} + +{% block content %} + +
+ + + {% include "InvenTree/settings/setting.html" with key="NOTIFICATION_SEND_EMAILS" icon='fa-envelope' user_setting=True %} + +
+
+ +{% endblock content %} \ No newline at end of file From 5f98cdf3c751568ecad16614918e136b2b891954 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 20 Mar 2022 19:31:59 +1100 Subject: [PATCH 2/5] Email notifications are only sent for users who have the setting enabled --- InvenTree/common/notifications.py | 35 ++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index 6ba7b6d659..a2c0622f91 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() 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') From 9cc2cc568301a379355d297218a3feb2adae430f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 20 Mar 2022 19:36:52 +1100 Subject: [PATCH 3/5] Fix asynchronous order of events for "mark all as read" button --- InvenTree/templates/InvenTree/notifications/inbox.html | 2 +- .../templates/InvenTree/notifications/notifications.html | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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 "Mark all as read" %}
diff --git a/InvenTree/templates/InvenTree/notifications/notifications.html b/InvenTree/templates/InvenTree/notifications/notifications.html index 89f10c3e3d..fedf8a1448 100644 --- a/InvenTree/templates/InvenTree/notifications/notifications.html +++ b/InvenTree/templates/InvenTree/notifications/notifications.html @@ -126,8 +126,12 @@ $("#mark-all").on('click', function() { { read: false, }, + { + success: function(response) { + updateNotificationTables(); + } + } ); - updateNotificationTables(); }); loadNotificationTable("#history-table", { From ace61c6aab8114b85ae7d0541afc9e79d7442d24 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 20 Mar 2022 19:38:40 +1100 Subject: [PATCH 4/5] Little bit of eye candy --- InvenTree/templates/js/translated/notification.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/templates/js/translated/notification.js b/InvenTree/templates/js/translated/notification.js index 4da4c2e875..a289dcb8f7 100644 --- a/InvenTree/templates/js/translated/notification.js +++ b/InvenTree/templates/js/translated/notification.js @@ -253,7 +253,7 @@ function openNotificationPanel() { { success: function(response) { if (response.length == 0) { - html = `

{% trans "No unread notifications" %}

`; + html = `

{% trans "No unread notifications" %}

`; } else { // build up items response.forEach(function(item, index) { From c8bbd25001d67149824dea1fa91ed3b2e78648bf Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 20 Mar 2022 19:41:42 +1100 Subject: [PATCH 5/5] Bug fix --- InvenTree/common/notifications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/InvenTree/common/notifications.py b/InvenTree/common/notifications.py index a2c0622f91..9151f9b879 100644 --- a/InvenTree/common/notifications.py +++ b/InvenTree/common/notifications.py @@ -129,7 +129,7 @@ class EmailNotification(BulkNotificationMethod): def send_bulk(self): html_message = render_to_string(self.context['template']['html'], self.context) - targets = self.get_targets() + targets = self.get_targets().values_list('email', flat=True) InvenTree.tasks.send_email(self.context['template']['subject'], '', targets, html_message=html_message)