2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 19:46:46 +00:00

Fix email notification setting (#3832)

* Coerce setting value to a boolean

* Ignore inactive users when sending notification emails

* Only send UI notifications to active users

* Fixes for unit tests
This commit is contained in:
Oliver 2022-10-22 18:56:38 +11:00 committed by GitHub
parent c120de90ae
commit a898ebce40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 9 deletions

View File

@ -557,11 +557,13 @@ class BuildTest(BuildTestBase):
category='build.new_build', category='build.new_build',
) )
self.assertEqual(messages.count(), 2) self.assertEqual(messages.count(), 1)
self.assertFalse(messages.filter(user__pk=2).exists()) self.assertFalse(messages.filter(user__pk=2).exists())
self.assertTrue(messages.filter(user__pk=3).exists()) # Inactive users do not receive notifications
self.assertFalse(messages.filter(user__pk=3).exists())
self.assertTrue(messages.filter(user__pk=4).exists()) self.assertTrue(messages.filter(user__pk=4).exists())

View File

@ -243,8 +243,9 @@ class UIMessageNotification(SingleNotificationMethod):
METHOD_NAME = 'ui_message' METHOD_NAME = 'ui_message'
def get_targets(self): def get_targets(self):
"""Just return the targets - no tricks here.""" """Only send notifications for active users"""
return self.targets
return [target for target in self.targets if target.is_active]
def send(self, target): def send(self, target):
"""Send a UI notification to a user.""" """Send a UI notification to a user."""

View File

@ -778,7 +778,8 @@ class NotificationTest(InvenTreeAPITestCase):
messages = NotificationMessage.objects.all() messages = NotificationMessage.objects.all()
# As there are three staff users (including the 'test' user) we expect 30 notifications # As there are three staff users (including the 'test' user) we expect 30 notifications
self.assertEqual(messages.count(), 30) # However, one user is marked as i nactive
self.assertEqual(messages.count(), 20)
# Only 10 messages related to *this* user # Only 10 messages related to *this* user
my_notifications = messages.filter(user=self.user) my_notifications = messages.filter(user=self.user)
@ -822,7 +823,7 @@ class NotificationTest(InvenTreeAPITestCase):
# Only 7 notifications should have been deleted, # Only 7 notifications should have been deleted,
# as the notifications associated with other users must remain untouched # as the notifications associated with other users must remain untouched
self.assertEqual(NotificationMessage.objects.count(), 23) self.assertEqual(NotificationMessage.objects.count(), 13)
self.assertEqual(NotificationMessage.objects.filter(user=self.user).count(), 3) self.assertEqual(NotificationMessage.objects.filter(user=self.user).count(), 3)

View File

@ -267,7 +267,7 @@ class SalesOrderTest(TestCase):
category='order.overdue_sales_order', category='order.overdue_sales_order',
) )
self.assertEqual(len(messages), 2) self.assertEqual(len(messages), 1)
def test_new_so_notification(self): def test_new_so_notification(self):
"""Test that a notification is sent when a new SalesOrder is created. """Test that a notification is sent when a new SalesOrder is created.

View File

@ -326,6 +326,11 @@ class OrderTest(TestCase):
user__id=user_id, user__id=user_id,
) )
# User ID 3 is inactive, and thus should not receive notifications
if user_id == 3:
self.assertFalse(messages.exists())
continue
else:
self.assertTrue(messages.exists()) self.assertTrue(messages.exists())
msg = messages.first() msg = messages.first()

View File

@ -6,6 +6,7 @@ from django.utils.translation import gettext_lazy as _
from allauth.account.models import EmailAddress from allauth.account.models import EmailAddress
import common.models import common.models
import InvenTree.helpers
import InvenTree.tasks import InvenTree.tasks
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
from plugin.mixins import BulkNotificationMethod, SettingsMixin from plugin.mixins import BulkNotificationMethod, SettingsMixin
@ -61,7 +62,12 @@ class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin):
allowed_users = [] allowed_users = []
for user in self.targets: for user in self.targets:
allows_emails = self.usersetting(user)
if not user.is_active:
# Ignore any users who have been deactivated
continue
allows_emails = InvenTree.helpers.str2bool(self.usersetting(user))
if allows_emails: if allows_emails:
allowed_users.append(user) allowed_users.append(user)