From 36a979954c95ea307d55bc4f1c147c2b2ba68206 Mon Sep 17 00:00:00 2001 From: moanos Date: Sat, 18 Jan 2025 18:53:55 +0100 Subject: [PATCH] feat: make sure owner also gets notified, add test --- src/fellchensammlung/tasks.py | 4 +-- src/fellchensammlung/tools/notifications.py | 8 +++-- src/tests/test_tools_notifications.py | 34 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/tests/test_tools_notifications.py diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index 8d1eb9f..a238560 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -7,7 +7,7 @@ from .mail import send_notification_email from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices, deactivate_404_adoption_notices from .tools.misc import healthcheck_ok from .models import Location, AdoptionNotice, Timestamp -from .tools.notifications import notify_moderators_of_AN_to_be_checked +from .tools.notifications import notify_of_AN_to_be_checked from .tools.search import notify_search_subscribers @@ -46,7 +46,7 @@ def post_adoption_notice_save(pk): logging.info(f"Location was added to Adoption notice {pk}") notify_search_subscribers(instance, only_if_active=True) - notify_moderators_of_AN_to_be_checked(instance) + notify_of_AN_to_be_checked(instance) @celery_app.task(name="tools.healthcheck") def task_healthcheck(): diff --git a/src/fellchensammlung/tools/notifications.py b/src/fellchensammlung/tools/notifications.py index fa26666..63fd1f0 100644 --- a/src/fellchensammlung/tools/notifications.py +++ b/src/fellchensammlung/tools/notifications.py @@ -1,11 +1,13 @@ from fellchensammlung.models import User, AdoptionNoticeNotification, TrustLevel -def notify_moderators_of_AN_to_be_checked(adoption_notice): +def notify_of_AN_to_be_checked(adoption_notice): if adoption_notice.is_disabled_unchecked: - for moderator in User.objects.filter(trust_level__gt=TrustLevel.MODERATOR): + users_to_notify = set(User.objects.filter(trust_level__gt=TrustLevel.MODERATOR)) + users_to_notify.add(adoption_notice.owner) + for user in users_to_notify: AdoptionNoticeNotification.objects.create(adoption_notice=adoption_notice, - user=moderator, + user=user, title=f" Prüfe Vermittlung {adoption_notice}", text=f"{adoption_notice} muss geprüft werden bevor sie veröffentlicht wird.", ) \ No newline at end of file diff --git a/src/tests/test_tools_notifications.py b/src/tests/test_tools_notifications.py new file mode 100644 index 0000000..d570c81 --- /dev/null +++ b/src/tests/test_tools_notifications.py @@ -0,0 +1,34 @@ +from django.test import TestCase +from model_bakery import baker + +from fellchensammlung.models import User, TrustLevel, Species, Location, AdoptionNotice, AdoptionNoticeNotification +from fellchensammlung.tools.notifications import notify_of_AN_to_be_checked + + +class TestNotifications(TestCase): + @classmethod + def setUpTestData(cls): + cls.test_user0 = User.objects.create_user(username='testuser0', + first_name="Admin", + last_name="BOFH", + password='12345') + + cls.test_user1 = User.objects.create_user(username='testuser1', + first_name="Max", + last_name="Müller", + password='12345') + cls.test_user2 = User.objects.create_user(username='testuser2', + first_name="Miriam", + last_name="Müller", + password='12345') + cls.test_user0.trust_level = TrustLevel.ADMIN + cls.test_user0.save() + + cls.adoption1 = baker.make(AdoptionNotice, name="TestAdoption1", owner=cls.test_user1,) + cls.adoption1.set_unchecked() # Could also emit notification + + def test_notify_of_AN_to_be_checked(self): + notify_of_AN_to_be_checked(self.adoption1) + self.assertTrue(AdoptionNoticeNotification.objects.filter(user=self.test_user0).exists()) + self.assertTrue(AdoptionNoticeNotification.objects.filter(user=self.test_user1).exists()) + self.assertFalse(AdoptionNoticeNotification.objects.filter(user=self.test_user2).exists()) \ No newline at end of file