diff --git a/greg/management/commands/import_from_orgreg.py b/greg/management/commands/import_from_orgreg.py index 732a68206ba4bb8599e3de7c7cc4d8972cff9367..2411ed04921d25484d88af2c451043c80b262504 100644 --- a/greg/management/commands/import_from_orgreg.py +++ b/greg/management/commands/import_from_orgreg.py @@ -28,6 +28,75 @@ class Command(BaseCommand): help = __doc__ processed: Dict[int, OrganizationalUnit] = {} + def _upsert_extra_identities( + self, + ou: OrgUnit, + ): + """Upsert any configured extra IDs from orgreg.""" + for extra_id in settings.ORGREG_EXTRA_IDS: + + matching_ids = [ + x + for x in ou.external_keys + if x.source_system == extra_id["source"] and x.type == extra_id["type"] + ] + + if not matching_ids: + logger.warning( + "No %s id from %s found in OrgReg for ou %s", + extra_id["type"], + extra_id["source"], + ou.ou_id, + ) + continue + + if len(matching_ids) > 1: + # External_ids + logger.warning( + "Found multiple ids matching type: %s source: %s in OrgReg. Using the first one: %s", + extra_id["type"], + extra_id["source"], + matching_ids[0].value, + ) + + identity_in_orgreg = matching_ids[0] + + # Check if the id exists + identify_in_db = ( + self.processed[ou.ou_id] + .identifiers.filter( + source=extra_id["source"], + name=extra_id["type"], + ) + .first() + ) + + if identify_in_db: + if identify_in_db.value != identity_in_orgreg.value: + logger.info( + "Updating id: source: %s, type: %s, old_id: %s, new_id %s", + extra_id["source"], + extra_id["type"], + identify_in_db.value, + identity_in_orgreg.value, + ) + identify_in_db.value = identity_in_orgreg.value + identify_in_db.save() + else: + OuIdentifier.objects.create( + name=extra_id["type"], + source=extra_id["source"], + value=identity_in_orgreg["value"], + orgunit=self.processed[ou.ou_id], + ) + logger.info( + "Added new id to ou: %s, type: %s, source: %s value: %s", + ou.ou_id, + extra_id["type"], + extra_id["source"], + identity_in_orgreg["value"], + ) + def _get_or_create_and_set_values( self, ou: OrgUnit, values: Mapping[str, Union[str, int, bool]] ): @@ -50,6 +119,7 @@ class Command(BaseCommand): created = False for k, v in values.items(): setattr(self.processed[ou.ou_id], k, v) + self._upsert_extra_identities(ou) self.processed[ou.ou_id].save() logger.info( "%s %s with %s", diff --git a/gregsite/settings/base.py b/gregsite/settings/base.py index 7a618734d1a449a891d4e8078810d3064a4500e1..11a32801be699f13e49a383c0945d8b91229f7da 100644 --- a/gregsite/settings/base.py +++ b/gregsite/settings/base.py @@ -11,7 +11,7 @@ https://docs.djangoproject.com/en/3.2/ref/settings/ """ from pathlib import Path -from typing import List, Literal +from typing import List # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -271,3 +271,7 @@ FEIDE_SOURCE = "feide" # Used by the OU import from orgreg to distinguish the OuIdentifiers from others ORGREG_SOURCE = "orgreg" ORGREG_NAME = "orgreg_id" + +# Extra ids to be imported from orgreg, list of dict with source/type. +# [{"source": "sapuio", "type": "legacy_stedkode"}] +ORGREG_EXTRA_IDS = []