Repository: comic/grand-challenge.org

Issue:
Notification and newsletter emails should only go to active users
At the moment these emails are sent to all users, deactivated or not.


Discussion:

Resolving patch:
diff --git a/app/grandchallenge/emails/tasks.py b/app/grandchallenge/emails/tasks.py
--- a/app/grandchallenge/emails/tasks.py
+++ b/app/grandchallenge/emails/tasks.py
@@ -19,12 +19,16 @@ def get_receivers(action):
     if action == SendActionChoices.MAILING_LIST:
         receivers = (
             get_user_model()
-            .objects.filter(user_profile__receive_newsletter=True)
+            .objects.filter(
+                user_profile__receive_newsletter=True, is_active=True
+            )
             .order_by("pk")
         )
     elif action == SendActionChoices.STAFF:
         receivers = (
-            get_user_model().objects.filter(is_staff=True).order_by("pk")
+            get_user_model()
+            .objects.filter(is_staff=True, is_active=True)
+            .order_by("pk")
         )
     elif action == SendActionChoices.CHALLENGE_ADMINS:
         receivers = (
@@ -32,6 +36,7 @@ def get_receivers(action):
             .objects.filter(
                 groups__admins_of_challenge__isnull=False,
                 user_profile__receive_newsletter=True,
+                is_active=True,
             )
             .distinct()
             .order_by("pk")
@@ -42,6 +47,7 @@ def get_receivers(action):
             .objects.filter(
                 groups__editors_of_readerstudy__isnull=False,
                 user_profile__receive_newsletter=True,
+                is_active=True,
             )
             .distinct()
             .order_by("pk")
@@ -52,6 +58,7 @@ def get_receivers(action):
             .objects.filter(
                 groups__editors_of_algorithm__isnull=False,
                 user_profile__receive_newsletter=True,
+                is_active=True,
             )
             .distinct()
             .order_by("pk")
diff --git a/app/grandchallenge/notifications/tasks.py b/app/grandchallenge/notifications/tasks.py
--- a/app/grandchallenge/notifications/tasks.py
+++ b/app/grandchallenge/notifications/tasks.py
@@ -10,7 +10,9 @@
 def send_unread_notification_emails():
     profiles = (
         UserProfile.objects.filter(
-            receive_notification_emails=True, user__notification__read=False
+            receive_notification_emails=True,
+            user__notification__read=False,
+            user__is_active=True,
         )
         .distinct()
         .prefetch_related("user__notification_set")


Test patch:
diff --git a/app/tests/emails_tests/test_tasks.py b/app/tests/emails_tests/test_tasks.py
--- a/app/tests/emails_tests/test_tasks.py
+++ b/app/tests/emails_tests/test_tasks.py
@@ -24,6 +24,8 @@
 @pytest.mark.django_db
 def test_get_receivers(factory, action):
     u1, u2, u3, u4 = UserFactory.create_batch(4)
+    u2.is_active = False
+    u2.save()
     for user in [u1, u2]:
         user.user_profile.receive_newsletter = True
         user.user_profile.save()
@@ -42,10 +44,10 @@ def test_get_receivers(factory, action):
 
     receivers = get_receivers(action)
 
-    assert len(receivers) == 2
-    for user in [u1, u2]:
+    assert len(receivers) == 1
+    for user in [u1]:
         assert user in receivers
-    for user in [u3, u4]:
+    for user in [u2, u3, u4]:
         assert user not in receivers
 
 
diff --git a/app/tests/notifications_tests/test_tasks.py b/app/tests/notifications_tests/test_tasks.py
--- a/app/tests/notifications_tests/test_tasks.py
+++ b/app/tests/notifications_tests/test_tasks.py
@@ -10,16 +10,22 @@
 
 @pytest.mark.django_db
 def test_notification_email():
-    user1 = UserFactory()
+    user1, user2 = UserFactory.create_batch(2)
+    user2.is_active = False
+    user2.save()
     _ = NotificationFactory(user=user1, type=Notification.Type.GENERIC)
+    _ = NotificationFactory(user=user2, type=Notification.Type.GENERIC)
 
     assert user1.user_profile.has_unread_notifications
     assert user1.user_profile.receive_notification_emails
+    assert user2.user_profile.has_unread_notifications
+    assert user2.user_profile.receive_notification_emails
     assert len(mail.outbox) == 0
     send_unread_notification_emails()
     assert len(mail.outbox) == 1
     email = mail.outbox[-1]
-    assert email.to[0] == user1.email
+    assert user1.email in email.to
+    assert user2.email not in email.to
     assert "You have 1 new notification" in email.body