Skip to content
Snippets Groups Projects
Commit 8a1c846a authored by lass's avatar lass
Browse files

Exclude certain ous from notifications

Also changed the name of some variables to make the code more readable
parent 1970d49e
No related branches found
No related tags found
1 merge request!403Exclude certain ous from notifications
Pipeline #192923 passed
...@@ -35,32 +35,35 @@ def notify_sponsors_roles_ending() -> list[str]: ...@@ -35,32 +35,35 @@ def notify_sponsors_roles_ending() -> list[str]:
end_date__gte=today, end_date__gte=today,
end_date__lte=today + datetime.timedelta(days=settings.NOTIFIER_LIMIT), end_date__lte=today + datetime.timedelta(days=settings.NOTIFIER_LIMIT),
) )
unit2roles = defaultdict(list) unit_to_roles = defaultdict(list)
for role in ending_roles: 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 # Map sponsors with email to units
sponsors = Sponsor.objects.filter(work_email__isnull=False) 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 # Map sponsors to ending roles
# Make sure only the sponsor(s) closest to the role is notified # Make sure only the sponsor(s) closest to the role is notified
sp2roles: dict = defaultdict(list) sponsor_to_roles: dict = defaultdict(list)
for unit_id, roles in unit2roles.items(): for unit_id, roles in unit_to_roles.items():
for role in roles: for role in roles:
role_notified = False role_notified = False
for sp, units in reversed(sp2unit.items()): for sp, units in reversed(sponsor_to_unit.items()):
for unit in units: for unit in units:
if unit_id == unit.id and ( if (
not role_notified or unit.parent not in units 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 role_notified = True
# Send emails to sponsors # Send emails to sponsors
remindermailer = RolesEnding() remindermailer = RolesEnding()
task_ids = [] task_ids = []
for sp, roles in sp2roles.items(): for sp, roles in sponsor_to_roles.items():
task_ids.append( task_ids.append(
remindermailer.queue_mail( remindermailer.queue_mail(
mail_to=sponsors.get(id=sp).work_email, # type: ignore mail_to=sponsors.get(id=sp).work_email, # type: ignore
......
...@@ -105,6 +105,10 @@ def test_notify_sponsors_roles_ending( ...@@ -105,6 +105,10 @@ def test_notify_sponsors_roles_ending(
mail.outbox = [] mail.outbox = []
task_ids = notify_sponsors_roles_ending() task_ids = notify_sponsors_roles_ending()
assert len(task_ids) == 1 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 @pytest.mark.django_db
......
...@@ -359,3 +359,6 @@ ALLOW_SO_NUMBERS = False ...@@ -359,3 +359,6 @@ ALLOW_SO_NUMBERS = False
# Which id-types need to be verified for a guest to appear under "Confirmed guests" # 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"] 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 = []
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment