Skip to content
Snippets Groups Projects
Commit 4cb32b07 authored by Marte Fossum's avatar Marte Fossum
Browse files

Greg 306 role ends email

parent f10d59b8
No related branches found
No related tags found
1 merge request!365Greg 306 role ends email
...@@ -44,11 +44,18 @@ def notify_sponsors_roles_ending() -> list[str]: ...@@ -44,11 +44,18 @@ def notify_sponsors_roles_ending() -> list[str]:
sp2unit = {s.id: list(s.get_allowed_units()) for s in sponsors} sp2unit = {s.id: list(s.get_allowed_units()) for s in sponsors}
# Map sponsors to ending roles # Map sponsors to ending roles
sp2roles = defaultdict(list) # Make sure only the sponsor(s) closest to the role is notified
for sp, units in sp2unit.items(): sp2roles: dict = defaultdict(list)
for unit in units: for unit_id, roles in unit2roles.items():
for role in unit2roles[unit.id]: for role in roles:
sp2roles[sp].append(role) role_notified = False
for sp, units in reversed(sp2unit.items()):
for unit in units:
if unit_id == unit.id and (
not role_notified or unit.parent not in units
):
sp2roles[sp].append(role)
role_notified = True
# Send emails to sponsors # Send emails to sponsors
remindermailer = RolesEnding() remindermailer = RolesEnding()
......
...@@ -4,9 +4,17 @@ import pytest ...@@ -4,9 +4,17 @@ import pytest
from django.conf import settings from django.conf import settings
from django.core import mail from django.core import mail
from django.test import override_settings from django.test import override_settings
from orgreg_client import OrgUnit, OrgUnitList from orgreg_client.models import OrgUnit, OrgUnitList
from greg.models import OrganizationalUnit, OuIdentifier from greg.models import (
OrganizationalUnit,
OuIdentifier,
Person,
Role,
RoleType,
Sponsor,
SponsorOrganizationalUnit,
)
from greg.tasks import import_from_orgreg, notify_sponsors_roles_ending from greg.tasks import import_from_orgreg, notify_sponsors_roles_ending
from gregui.models import EmailTemplate from gregui.models import EmailTemplate
...@@ -27,6 +35,69 @@ To extend the guest role please use the web interface.""", ...@@ -27,6 +35,69 @@ To extend the guest role please use the web interface.""",
return EmailTemplate.objects.get(id=et.id) return EmailTemplate.objects.get(id=et.id)
@pytest.fixture
def org_tree_with_sponsors(
person_foo: Person, role_type_test_guest: RoleType, role_type_foo: RoleType
):
# Create all units
a = OrganizationalUnit.objects.create()
b = OrganizationalUnit.objects.create(parent=a)
c = OrganizationalUnit.objects.create(parent=b)
# Create all sponsors
d = Sponsor.objects.create(
feide_id="d@example.org",
first_name="D",
last_name="Guy",
work_email="d_guy@example.com",
)
e = Sponsor.objects.create(
feide_id="e@example.org",
first_name="E",
last_name="Guy",
work_email="e_guy@example.com",
)
f = Sponsor.objects.create(
feide_id="f@example.org",
first_name="F",
last_name="Guy",
work_email="f_guy@example.com",
)
g = Sponsor.objects.create(
feide_id="g@example.org",
first_name="G",
last_name="Guy",
work_email="g_guy@example.com",
)
# Connect them to units
SponsorOrganizationalUnit.objects.create(
sponsor=d, organizational_unit=a, hierarchical_access=True
)
SponsorOrganizationalUnit.objects.create(
sponsor=e, organizational_unit=b, hierarchical_access=True
)
# Create person with role on unit c and b
Role.objects.create(
person=person_foo,
type=role_type_test_guest,
start_date=datetime.date.today(),
end_date=datetime.date.today() + datetime.timedelta(days=5),
sponsor=f,
orgunit=c,
)
Role.objects.create(
person=person_foo,
type=role_type_foo,
start_date=datetime.date.today(),
end_date=datetime.date.today() + datetime.timedelta(days=5),
sponsor=e,
orgunit=b,
)
return a, b, c, d, e, f, g
@pytest.mark.django_db @pytest.mark.django_db
def test_notify_sponsors_roles_ending( def test_notify_sponsors_roles_ending(
role_end_reminder_template, role_person_foo2, sponsor_org_unit role_end_reminder_template, role_person_foo2, sponsor_org_unit
...@@ -36,6 +107,29 @@ def test_notify_sponsors_roles_ending( ...@@ -36,6 +107,29 @@ def test_notify_sponsors_roles_ending(
assert len(task_ids) == 1 assert len(task_ids) == 1
@pytest.mark.django_db
def test_sponsor_get_email(role_end_reminder_template, org_tree_with_sponsors):
mail.outbox = []
task_ids = notify_sponsors_roles_ending()
assert len(task_ids) == 1
@pytest.mark.django_db
def test_only_close_sponsors_get_email(
role_end_reminder_template, org_tree_with_sponsors
):
_, _, c, _, _, f, g = org_tree_with_sponsors
SponsorOrganizationalUnit.objects.create(
sponsor=f, organizational_unit=c, hierarchical_access=True
)
SponsorOrganizationalUnit.objects.create(
sponsor=g, organizational_unit=c, hierarchical_access=True
)
mail.outbox = []
task_ids = notify_sponsors_roles_ending()
assert len(task_ids) == 3
@pytest.fixture @pytest.fixture
def old_unit(): def old_unit():
ou = OrganizationalUnit.objects.create(name_nb="a", name_en="b") ou = OrganizationalUnit.objects.create(name_nb="a", name_en="b")
......
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