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

Handle existing identities in import script

If an identity already exists in greg, but is tied to a different
person, we tried to add it, causing an integrity error. We now check if
the value already exists. If it is tied to the person we are adding info
for, we simply update it. Otherwise we skip this value.
parent 226bec2f
No related branches found
No related tags found
1 merge request!322Prepare for guest migration
Pipeline #131403 passed
......@@ -68,7 +68,7 @@ class Command(BaseCommand):
help="End date of roles. Default: today + 100 days",
)
parser.add_argument(
"--ignore_id_req",
"--ignore-id-req",
default=False,
action="store_true",
help="Imports people without required ids if set",
......@@ -205,10 +205,15 @@ class Command(BaseCommand):
logger.info("role_created", role=role, person=person, sponsor=sponsor)
return role
def upsert_identity(self, person: Person, id_data: dict) -> Identity:
"""Add or update identity"""
def upsert_identity(self, person: Person, id_data: dict) -> Optional[Identity]:
"""
Add or update identity
If the identity does not exist, we create it. If it exists on
the person we are working on, we update it. If it exists on
someone else we ignore it.
"""
match = Identity.objects.filter(
person=person,
type=id_data["id_type"],
value=id_data["external_id"],
).first()
......@@ -224,14 +229,16 @@ class Command(BaseCommand):
logger.info(
"identity_added", identity=identity.id, identity_type=identity.type
)
else:
return identity
if match.person == person:
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
return None
def _has_required_id(self, id_data: dict) -> bool:
"""Check that we have at least one of the required ids"""
......
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