diff --git a/greg/tasks.py b/greg/tasks.py index f0464dc5feb75d0ff07dc1f162f67bb64b3f9f62..7ea1336b586baa147b1424c93135b288848cbca5 100644 --- a/greg/tasks.py +++ b/greg/tasks.py @@ -35,32 +35,35 @@ def notify_sponsors_roles_ending() -> list[str]: end_date__gte=today, end_date__lte=today + datetime.timedelta(days=settings.NOTIFIER_LIMIT), ) - unit2roles = defaultdict(list) + unit_to_roles = defaultdict(list) for role in ending_roles: - unit2roles[role.orgunit.id].append(role) + unit_to_roles[role.orgunit.id].append(role) # Map sponsors with email to units sponsors = Sponsor.objects.filter(work_email__isnull=False) - sp2unit = {s.id: s.get_allowed_units() for s in sponsors} + sponsor_to_unit = {s.id: s.get_allowed_units() for s in sponsors} # Map sponsors to ending roles # Make sure only the sponsor(s) closest to the role is notified - sp2roles: dict = defaultdict(list) - for unit_id, roles in unit2roles.items(): + sponsor_to_roles: dict = defaultdict(list) + for unit_id, roles in unit_to_roles.items(): for role in roles: role_notified = False - for sp, units in reversed(sp2unit.items()): + for sp, units in reversed(sponsor_to_unit.items()): for unit in units: - if unit_id == unit.id and ( - not role_notified or unit.parent not in units + if ( + unit.id + not in settings.EXCLUDED_UNITS # Implemented to save the principal from notification spam + and unit_id == unit.id + and (not role_notified or unit.parent not in units) ): - sp2roles[sp].append(role) + sponsor_to_roles[sp].append(role) role_notified = True # Send emails to sponsors remindermailer = RolesEnding() task_ids = [] - for sp, roles in sp2roles.items(): + for sp, roles in sponsor_to_roles.items(): task_ids.append( remindermailer.queue_mail( mail_to=sponsors.get(id=sp).work_email, # type: ignore diff --git a/greg/tests/test_tasks.py b/greg/tests/test_tasks.py index 762a8e775b25cd6aa3b5473b46398b62da8189fb..e97aa98d9403500e86471f30c1474e5027a54872 100644 --- a/greg/tests/test_tasks.py +++ b/greg/tests/test_tasks.py @@ -105,6 +105,10 @@ def test_notify_sponsors_roles_ending( mail.outbox = [] task_ids = notify_sponsors_roles_ending() assert len(task_ids) == 1 + # Test that sponsors of excluded units are not notified + with override_settings(EXCLUDED_UNITS=[sponsor_org_unit.id]): + task_ids = notify_sponsors_roles_ending() + assert len(task_ids) == 0 @pytest.mark.django_db @@ -128,6 +132,9 @@ def test_only_close_sponsors_get_email( mail.outbox = [] task_ids = notify_sponsors_roles_ending() assert len(task_ids) == 3 + with override_settings(EXCLUDED_UNITS=[c.id, f.id]): + task_ids = notify_sponsors_roles_ending() + assert len(task_ids) == 1 @pytest.fixture diff --git a/gregsite/settings/base.py b/gregsite/settings/base.py index d808104c92da95eae9cdf54097e6b66e5cdd715e..471a83f1bd76e4451fa8ea1045e19a49c7ef43a8 100644 --- a/gregsite/settings/base.py +++ b/gregsite/settings/base.py @@ -359,3 +359,6 @@ ALLOW_SO_NUMBERS = False # Which id-types need to be verified for a guest to appear under "Confirmed guests" ALLOWED_VERIFIED_ID_TYPES = ["norwegian_national_id_number", "passport_number"] + +# Which units are excluded when sending notification emails on expiring roles (primary key) +EXCLUDED_UNITS = []