Skip to content
Snippets Groups Projects
Commit 1d6f4f4c authored by Jonas Braathen's avatar Jonas Braathen
Browse files

Change Cerebrum sponsor import to replace manual SponsorOrganizationalUnits with an automatic one

parent 64fd0eb4
No related branches found
No related tags found
1 merge request!366Change Cerebrum sponsor import to replace manual SponsorOrganizationalUnits with an automatic one
Pipeline #175788 passed
......@@ -40,18 +40,31 @@ class Command(BaseCommand):
def _upsert_sponsor_unit_link(self, sponsor: Sponsor, unit: OrganizationalUnit):
"""Ensure a link between sponsor and unit."""
sunit, created = SponsorOrganizationalUnit.objects.get_or_create(
sponsor=sponsor,
organizational_unit=unit,
source=self.CEREBRUM_SOURCE,
automatic=True,
defaults={"hierarchical_access": settings.CEREBRUM_HIERARCHICAL_ACCESS},
)
try:
sunit = SponsorOrganizationalUnit.objects.get(
sponsor=sponsor,
organizational_unit=unit,
)
created = False
except SponsorOrganizationalUnit.DoesNotExist:
sunit = SponsorOrganizationalUnit.objects.create(
sponsor=sponsor,
organizational_unit=unit,
source=self.CEREBRUM_SOURCE,
automatic=True,
hierarchical_access=settings.CEREBRUM_HIERARCHICAL_ACCESS,
)
created = True
if created:
logger.info("sponsor_ou_link_create", sponsor=sponsor.id, sunit=sunit.id)
else:
logger.info("sponsor_ou_link_found", sponsor=sponsor.id, sunit=sunit.id)
sunit.source = self.CEREBRUM_SOURCE
sunit.automatic = True
sunit.hierarchical_access = settings.CEREBRUM_HIERARCHICAL_ACCESS
changes = sunit.get_dirty_fields(check_relationship=True, verbose=True)
if changes:
logger.info("sponsor_ou_link_changed", changes=changes)
sunit.save()
return SponsorOrganizationalUnit.objects.get(id=sunit.id)
......
......@@ -150,3 +150,20 @@ def test_import_sponsors_from_cerebrum(
# One excluded from unit with stedkode in manual list
assert Sponsor.objects.all().count() == 2
assert SponsorOrganizationalUnit.objects.count() == 2
# when a SponsorOrganizationalUnit with source != 'cerebrum'
# and automatic = False exists, replace it with an automatic one
sunit_lookup = dict(
organizational_unit__identifiers__name="legacy_stedkode",
organizational_unit__identifiers__value="234567",
)
sunit = SponsorOrganizationalUnit.objects.filter(**sunit_lookup).first()
assert isinstance(sunit, SponsorOrganizationalUnit)
sunit.source = "manual"
sunit.automatic = False
sunit.save()
call_command("import_sponsors_from_cerebrum")
sunit = SponsorOrganizationalUnit.objects.filter(**sunit_lookup).first()
assert isinstance(sunit, SponsorOrganizationalUnit)
assert sunit.source == "cerebrum"
assert sunit.automatic
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