Skip to content

Commit

Permalink
[chore] Update global notification setting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 committed Sep 20, 2024
1 parent ad8ff56 commit 6c53bf6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
14 changes: 11 additions & 3 deletions openwisp_notifications/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,19 @@ def save(self, *args, **kwargs):
with transaction.atomic():
if not self.organization and not self.type:
try:
original = self.__class__.objects.get(pk=self.pk)
previous_state = self.__class__.objects.only('email').get(
pk=self.pk
)
updates = {'web': self.web}

# Update 'email' only if it's different from the previous state
if not self.web or self.email != original.email:
# If global web notifiations are disabled, then disable email notifications as well
if not self.web:
updates['email'] = False

# Update email notifiations only if it's different from the previous state
# Otherwise, it would overwrite the email notification settings for specific
# setting that were enabled by the user after disabling global email notifications
if self.email != previous_state.email:
updates['email'] = self.email

self.user.notificationsetting_set.exclude(pk=self.pk).update(
Expand Down
18 changes: 17 additions & 1 deletion openwisp_notifications/tests/test_notification_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_global_notification_setting_update(self):
global_setting.save()

with self.subTest(
'Update global web to False while ensuring at least one email setting is True'
'Test global web to False while ensuring at least one email setting is True'
):
# Set the default type notification setting's email to True
NotificationSetting.objects.filter(
Expand All @@ -308,6 +308,22 @@ def test_global_notification_setting_update(self):
).exists()
)

with self.subTest('Test global web to False'):
global_setting.web = False
global_setting.full_clean()
global_setting.save()

self.assertFalse(
NotificationSetting.objects.filter(
user=admin, organization=org, web=True
).exists()
)
self.assertFalse(
NotificationSetting.objects.filter(
user=admin, organization=org, email=True
).exists()
)

def test_global_notification_setting_delete(self):
admin = self._get_admin()
global_setting = NotificationSetting.objects.get(
Expand Down

0 comments on commit 6c53bf6

Please sign in to comment.