mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +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:
		| @@ -557,11 +557,13 @@ class BuildTest(BuildTestBase): | ||||
|             category='build.new_build', | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual(messages.count(), 2) | ||||
|         self.assertEqual(messages.count(), 1) | ||||
|  | ||||
|         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()) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -243,8 +243,9 @@ class UIMessageNotification(SingleNotificationMethod): | ||||
|     METHOD_NAME = 'ui_message' | ||||
|  | ||||
|     def get_targets(self): | ||||
|         """Just return the targets - no tricks here.""" | ||||
|         return self.targets | ||||
|         """Only send notifications for active users""" | ||||
|  | ||||
|         return [target for target in self.targets if target.is_active] | ||||
|  | ||||
|     def send(self, target): | ||||
|         """Send a UI notification to a user.""" | ||||
|   | ||||
| @@ -778,7 +778,8 @@ class NotificationTest(InvenTreeAPITestCase): | ||||
|         messages = NotificationMessage.objects.all() | ||||
|  | ||||
|         # 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 | ||||
|         my_notifications = messages.filter(user=self.user) | ||||
| @@ -822,7 +823,7 @@ class NotificationTest(InvenTreeAPITestCase): | ||||
|  | ||||
|         # Only 7 notifications should have been deleted, | ||||
|         # 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) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -267,7 +267,7 @@ class SalesOrderTest(TestCase): | ||||
|             category='order.overdue_sales_order', | ||||
|         ) | ||||
|  | ||||
|         self.assertEqual(len(messages), 2) | ||||
|         self.assertEqual(len(messages), 1) | ||||
|  | ||||
|     def test_new_so_notification(self): | ||||
|         """Test that a notification is sent when a new SalesOrder is created. | ||||
|   | ||||
| @@ -326,6 +326,11 @@ class OrderTest(TestCase): | ||||
|                 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()) | ||||
|  | ||||
|             msg = messages.first() | ||||
|   | ||||
| @@ -6,6 +6,7 @@ from django.utils.translation import gettext_lazy as _ | ||||
| from allauth.account.models import EmailAddress | ||||
|  | ||||
| import common.models | ||||
| import InvenTree.helpers | ||||
| import InvenTree.tasks | ||||
| from plugin import InvenTreePlugin | ||||
| from plugin.mixins import BulkNotificationMethod, SettingsMixin | ||||
| @@ -61,7 +62,12 @@ class CoreNotificationsPlugin(SettingsMixin, InvenTreePlugin): | ||||
|             allowed_users = [] | ||||
|  | ||||
|             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: | ||||
|                     allowed_users.append(user) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user