From 29fe7adbcf66f95e4d8ab873105470dc1b637455 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Wed, 3 Jul 2024 17:47:02 +0200 Subject: [PATCH 1/2] =?UTF-8?q?fix(notification):=20mis=20=C3=A0=20jour=20?= =?UTF-8?q?le=20champ=20sent=5Fat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lacommunaute/notification/tasks.py | 13 ++++++++----- lacommunaute/notification/tests/tests_tasks.py | 6 ++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lacommunaute/notification/tasks.py b/lacommunaute/notification/tasks.py index 09e90c710..11b848e00 100644 --- a/lacommunaute/notification/tasks.py +++ b/lacommunaute/notification/tasks.py @@ -1,6 +1,7 @@ from django.conf import settings from django.template.defaultfilters import pluralize from django.urls import reverse +from django.utils import timezone from config.settings.base import DEFAULT_FROM_EMAIL, NEW_MESSAGES_EMAIL_MAX_PREVIEW, SIB_NEW_MESSAGES_TEMPLATE from lacommunaute.forum.models import Forum @@ -14,12 +15,12 @@ def send_messages_notifications(delay: NotificationDelay): """Notifications are scheduled in the application and then processed later by this task""" + notifications = Notification.objects.filter(delay=delay, sent_at__isnull=True, post__isnull=False).select_related( + "post", "post__topic", "post__poster" + ) + def get_grouped_notifications(): - return ( - Notification.objects.filter(delay=delay, sent_at__isnull=True, post__isnull=False) - .select_related("post", "post__topic", "post__poster") - .group_by_recipient() - ) + return notifications.group_by_recipient() grouped_notifications = get_grouped_notifications() for recipient in grouped_notifications.keys(): @@ -40,6 +41,8 @@ def get_grouped_notifications(): template_id=SIB_NEW_MESSAGES_TEMPLATE, ) + notifications.bulk_update(sent_at=timezone.now()) + def add_user_to_list_when_register(): new_users = collect_new_users_for_onboarding() diff --git a/lacommunaute/notification/tests/tests_tasks.py b/lacommunaute/notification/tests/tests_tasks.py index 89fcaf3cc..8a599cfe1 100644 --- a/lacommunaute/notification/tests/tests_tasks.py +++ b/lacommunaute/notification/tests/tests_tasks.py @@ -83,6 +83,9 @@ def test_send_messages_notifications_asap(self): email_sent_track.datas, self.get_expected_email_payload(Notification.objects.filter(id=notification.id)) ) + self.assertIsNone(Notification.objects.filter(sent_at__isnull=True).first()) + self.assertEqual(Notification.objects.all().values("sent_at").distinct(), 1) + @respx.mock def test_send_messages_notifications_day(self): topic = TopicFactory(with_post=True) @@ -97,6 +100,9 @@ def test_send_messages_notifications_day(self): email_sent_track.datas, self.get_expected_email_payload(Notification.objects.filter(id=notification.id)) ) + self.assertIsNone(Notification.objects.filter(sent_at__isnull=True).first()) + self.assertEqual(Notification.objects.all().values("sent_at").distinct(), 1) + @respx.mock def test_send_messages_notifications_max_messages_preview(self): topic = TopicFactory(with_post=True) From 8fb4a395fc2c4be0dca84cdfd4d0564e13a05903 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Wed, 3 Jul 2024 17:52:01 +0200 Subject: [PATCH 2/2] =?UTF-8?q?fix(notification):=20mettre=20=C3=A0=20jour?= =?UTF-8?q?=20le=20champ=20sent=5Fat=20et=20tester?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lacommunaute/notification/tasks.py | 2 +- lacommunaute/notification/tests/tests_tasks.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lacommunaute/notification/tasks.py b/lacommunaute/notification/tasks.py index 11b848e00..fe081d1d7 100644 --- a/lacommunaute/notification/tasks.py +++ b/lacommunaute/notification/tasks.py @@ -41,7 +41,7 @@ def get_grouped_notifications(): template_id=SIB_NEW_MESSAGES_TEMPLATE, ) - notifications.bulk_update(sent_at=timezone.now()) + notifications.update(sent_at=timezone.now()) def add_user_to_list_when_register(): diff --git a/lacommunaute/notification/tests/tests_tasks.py b/lacommunaute/notification/tests/tests_tasks.py index 8a599cfe1..c3cf633f8 100644 --- a/lacommunaute/notification/tests/tests_tasks.py +++ b/lacommunaute/notification/tests/tests_tasks.py @@ -84,7 +84,7 @@ def test_send_messages_notifications_asap(self): ) self.assertIsNone(Notification.objects.filter(sent_at__isnull=True).first()) - self.assertEqual(Notification.objects.all().values("sent_at").distinct(), 1) + self.assertEqual(Notification.objects.all().values("sent_at").distinct().count(), 1) @respx.mock def test_send_messages_notifications_day(self): @@ -101,7 +101,7 @@ def test_send_messages_notifications_day(self): ) self.assertIsNone(Notification.objects.filter(sent_at__isnull=True).first()) - self.assertEqual(Notification.objects.all().values("sent_at").distinct(), 1) + self.assertEqual(Notification.objects.all().values("sent_at").distinct().count(), 1) @respx.mock def test_send_messages_notifications_max_messages_preview(self): @@ -127,7 +127,7 @@ def test_send_messages_notifications_max_messages_preview(self): @respx.mock def test_send_messages_notifications_num_queries(self): - expected_queries = 1 + expected_queries = 2 NotificationFactory(delay=NotificationDelay.ASAP)