Skip to content
Snippets Groups Projects
Commit 37fcabe5 authored by Sivert Kronen Hatteberg's avatar Sivert Kronen Hatteberg
Browse files

Use get_or_create

parent 28147c7b
No related branches found
No related tags found
1 merge request!149Greg 95 cerebrum sponsor import
Pipeline #100424 passed
This commit is part of merge request !149. Comments created here will be created in the context of that merge request.
......@@ -46,23 +46,19 @@ class Command(BaseCommand):
def _upsert_sponsor_unit_link(self, sponsor: Sponsor, unit: OrganizationalUnit):
"""Ensure a link between sponsor and unit."""
try:
sunit = SponsorOrganizationalUnit.objects.get(
sponsor=sponsor,
organizational_unit=unit,
source=self.CEREBRUM_SOURCE,
automatic=True,
)
logger.info("sponsor_ou_link_found", sponsor=sponsor.id, sunit=sunit.id)
except SponsorOrganizationalUnit.DoesNotExist:
sunit = SponsorOrganizationalUnit.objects.create(
sponsor=sponsor,
organizational_unit=unit,
hierarchical_access=settings.CEREBRUM_HIERARCHICAL_ACCESS,
automatic=True,
source=self.CEREBRUM_SOURCE,
)
sunit, created = SponsorOrganizationalUnit.objects.get_or_create(
sponsor=sponsor,
organizational_unit=unit,
source=self.CEREBRUM_SOURCE,
automatic=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.hierarchical_access = settings.CEREBRUM_HIERARCHICAL_ACCESS
sunit.save()
return SponsorOrganizationalUnit.objects.get(id=sunit.id)
def _remove_sponsor_unit_link(self, sunit: SponsorOrganizationalUnit):
......
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