Skip to content
Snippets Groups Projects
Commit d7aa8a5f authored by Andreas Ellewsen's avatar Andreas Ellewsen
Browse files

Merge branch 'import-guests-fix-matching' into 'master'

Make import_guests command more lenient on source

See merge request !319
parents cc646fec 967c1894
No related branches found
No related tags found
1 merge request!319Make import_guests command more lenient on source
Pipeline #129758 passed
...@@ -68,20 +68,32 @@ class Command(BaseCommand): ...@@ -68,20 +68,32 @@ class Command(BaseCommand):
) )
def _find_person_from_ids(self, ids: dict) -> Optional[Person]: def _find_person_from_ids(self, ids: dict) -> Optional[Person]:
"""Match IDs to find person.""" """
Match IDs to find person.
Tries to find a perfect match, returns matches for identifiers
of same type with same value from different source if present.
"""
for id_type in self.ID_TYPES: for id_type in self.ID_TYPES:
matching_ids = [x for x in ids if x["id_type"] == id_type] matching_ids = [x for x in ids if x["id_type"] == id_type]
for matching_id in matching_ids: for matching_id in matching_ids:
try: # Check without correct source
greg_id = Identity.objects.get( matches = Identity.objects.filter(
type=id_type, type=id_type,
value=matching_id["external_id"], value=matching_id["external_id"],
source=matching_id["source_system"], )
) if not matches:
except Identity.DoesNotExist: # No match, check next id
continue continue
else: # Found one or more matches, check if a perfect match exists
return greg_id.person perfect = matches.filter(
source=matching_id["source_system"],
).first()
if perfect:
# Perfect match! Return it
return perfect.person
# No perfect match, return first match from other source
return matches.first().person # type: ignore
return None return None
def _find_orgunit_from_external_id( def _find_orgunit_from_external_id(
...@@ -188,15 +200,12 @@ class Command(BaseCommand): ...@@ -188,15 +200,12 @@ class Command(BaseCommand):
def upsert_identity(self, person: Person, id_data: dict) -> Identity: def upsert_identity(self, person: Person, id_data: dict) -> Identity:
"""Add or update identity""" """Add or update identity"""
try: match = Identity.objects.filter(
identity = Identity.objects.get( person=person,
person=person, type=id_data["id_type"],
type=id_data["id_type"], value=id_data["external_id"],
value=id_data["external_id"], ).first()
source=id_data["source_system"], if not match:
verified="automatic",
)
except Identity.DoesNotExist:
identity = Identity.objects.create( identity = Identity.objects.create(
person=person, person=person,
type=id_data["id_type"], type=id_data["id_type"],
...@@ -208,7 +217,13 @@ class Command(BaseCommand): ...@@ -208,7 +217,13 @@ class Command(BaseCommand):
logger.info( logger.info(
"identity_added", identity=identity.id, identity_type=identity.type "identity_added", identity=identity.id, identity_type=identity.type
) )
else:
identity = match
if not identity.verified_at:
identity.source = id_data["source_system"]
identity.verified = "automatic"
identity.verified_at = make_aware(datetime.datetime.now())
identity.save()
return identity return identity
def _has_required_id(self, id_data: dict) -> bool: def _has_required_id(self, id_data: dict) -> bool:
......
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