Skip to content

Commit

Permalink
[chore] Global Notification Preference changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhanus3133 committed Jul 25, 2024
1 parent 74a48f5 commit 99bf8c7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 46 deletions.
5 changes: 5 additions & 0 deletions openwisp_notifications/api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ class NotificationSettingFilter(OrganizationMembershipFilter):
class Meta(OrganizationMembershipFilter.Meta):
model = NotificationSetting
fields = OrganizationMembershipFilter.Meta.fields + ['type']

@property
def qs(self):
parent_qs = super().qs
return parent_qs.exclude(organization__isnull=True, type__isnull=True)
3 changes: 3 additions & 0 deletions openwisp_notifications/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def post(self, request, user_id):
type=None,
defaults={'email': email, 'web': web},
)
NotificationSetting.objects.filter(user_id=user_id).update(
email=email, web=web
)
return Response(status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Expand Down
10 changes: 0 additions & 10 deletions openwisp_notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ def send_email_notification(sender, instance, created, **kwargs):
# Get email preference of user for this type of notification.
target_org = getattr(getattr(instance, 'target', None), 'organization_id', None)

# Check for global notification setting
try:
notification_setting = instance.recipient.notificationsetting_set.get(
organization=None, type=None
)
if not notification_setting.email_notification:
return
except NotificationSetting.DoesNotExist:
pass

if instance.type and target_org:
try:
notification_setting = instance.recipient.notificationsetting_set.get(
Expand Down
13 changes: 12 additions & 1 deletion openwisp_notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,21 @@ def delete_old_notifications(days):
# Following tasks updates notification settings in database.
# 'ns' is short for notification_setting
def create_notification_settings(user, organizations, notification_types):
global_setting, _ = NotificationSetting.objects.get_or_create(
user=user, organization=None, type=None, defaults={'email': True, 'web': True}
)

for type in notification_types:
for org in organizations:
NotificationSetting.objects.update_or_create(
defaults={'deleted': False}, user=user, type=type, organization=org
defaults={
'deleted': False,
'email': global_setting.email,
'web': global_setting.web,
},
user=user,
type=type,
organization=org,
)


Expand Down
24 changes: 17 additions & 7 deletions openwisp_notifications/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ def test_bearer_authentication(self, mocked_test):
notify.send(sender=self.admin, type='default', target=self._get_org_user())
n = Notification.objects.first()
notification_setting = NotificationSetting.objects.first()
notification_setting_count = NotificationSetting.objects.count()
notification_setting_count = NotificationSetting.objects.exclude(
organization__isnull=True
).count()
token = self._obtain_auth_token(username='admin', password='tester')

with self.subTest('Test listing all notifications'):
Expand Down Expand Up @@ -518,7 +520,9 @@ def test_obsolete_notifications_busy_worker(self, mocked_task):

def test_notification_setting_list_api(self):
self._create_org_user(is_admin=True)
number_of_settings = NotificationSetting.objects.filter(user=self.admin).count()
number_of_settings = NotificationSetting.objects.filter(
user=self.admin, organization__isnull=False
).count()
url = self._get_path('notification_setting_list')

with self.subTest('Test notification setting list view'):
Expand Down Expand Up @@ -570,15 +574,17 @@ def test_notification_setting_list_api(self):
self.assertEqual(response.status_code, 200)
notification_setting = response.data['results'][0]
self.assertIn('id', notification_setting)
self.assertIsNone(notification_setting['web'])
self.assertIsNone(notification_setting['email'])
self.assertTrue(notification_setting['web'])
self.assertTrue(notification_setting['email'])
self.assertIn('organization', notification_setting)

def test_list_notification_setting_filtering(self):
url = self._get_path('notification_setting_list')

with self.subTest('Test listing notification setting without filters'):
count = NotificationSetting.objects.count()
count = NotificationSetting.objects.exclude(
organization__isnull=True
).count()
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data['results']), count)
Expand Down Expand Up @@ -612,7 +618,9 @@ def test_list_notification_setting_filtering(self):
self.assertEqual(ns['type'], 'default')

def test_retreive_notification_setting_api(self):
notification_setting = NotificationSetting.objects.first()
notification_setting = NotificationSetting.objects.exclude(
organization__isnull=True
).first()

with self.subTest('Test for non-existing notification setting'):
url = self._get_path('notification_setting', uuid.uuid4())
Expand All @@ -637,7 +645,9 @@ def test_retreive_notification_setting_api(self):
self.assertEqual(data['email'], notification_setting.email)

def test_update_notification_setting_api(self):
notification_setting = NotificationSetting.objects.first()
notification_setting = NotificationSetting.objects.exclude(
organization__isnull=True
).first()
update_data = {'web': False}

with self.subTest('Test for non-existing notification setting'):
Expand Down
34 changes: 6 additions & 28 deletions openwisp_notifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_superuser_notifications_disabled(self):
organization_id=target_obj.organization.pk,
type='default',
)
self.assertEqual(notification_preference.email, None)
self.assertTrue(notification_preference.email)
notification_preference.web = False
notification_preference.save()
notification_preference.refresh_from_db()
Expand Down Expand Up @@ -743,15 +743,15 @@ def test_notification_type_email_notification_setting_false(self):

with self.subTest('Test user email preference not defined'):
self._create_notification()
self.assertEqual(len(mail.outbox), 0)
self.assertEqual(len(mail.outbox), 1)

with self.subTest('Test user email preference is "True"'):
NotificationSetting.objects.filter(
user=self.admin,
type='test_type',
).update(email=True)
self._create_notification()
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(len(mail.outbox), 2)

@mock_notification_types
def test_notification_type_web_notification_setting_true(self):
Expand Down Expand Up @@ -779,28 +779,6 @@ def test_notification_type_web_notification_setting_true(self):
self._create_notification()
self.assertEqual(notification_queryset.count(), 0)

@mock_notification_types
def test_global_email_notification_setting(self):
with self.subTest('Test email global preference is "False"'):
NotificationSetting.objects.update_or_create(
user=self.admin,
organization=None,
type=None,
defaults={'email': False, 'web': False},
)
self._create_notification()
self.assertEqual(len(mail.outbox), 0)

with self.subTest('Test email global preference is "True"'):
NotificationSetting.objects.update_or_create(
user=self.admin,
organization=None,
type=None,
defaults={'email': True, 'web': True},
)
self._create_notification()
self.assertEqual(len(mail.outbox), 1)

@mock_notification_types
def test_notification_type_web_notification_setting_false(self):
target_obj = self._get_org_user()
Expand All @@ -819,7 +797,7 @@ def test_notification_type_web_notification_setting_false(self):

with self.subTest('Test user web preference not defined'):
self._create_notification()
self.assertEqual(notification_queryset.count(), 0)
self.assertEqual(notification_queryset.count(), 1)

with self.subTest('Test user email preference is "True"'):
notification_setting = NotificationSetting.objects.get(
Expand All @@ -828,14 +806,14 @@ def test_notification_type_web_notification_setting_false(self):
notification_setting.email = True
notification_setting.save()
notification_setting.refresh_from_db()
self.assertFalse(notification_setting.email)
self.assertTrue(notification_setting.email)

with self.subTest('Test user web preference is "True"'):
NotificationSetting.objects.filter(
user=self.admin, type='test_type'
).update(web=True)
self._create_notification()
self.assertEqual(notification_queryset.count(), 1)
self.assertEqual(notification_queryset.count(), 2)

@mock_notification_types
def test_notification_type_email_web_notification_defaults(self):
Expand Down

0 comments on commit 99bf8c7

Please sign in to comment.