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

Merge branch 'cerebrum-guests' into 'master'

Prepare for guest migration

See merge request !322
parents 32b0fef5 e1c9f223
No related branches found
No related tags found
1 merge request!322Prepare for guest migration
Pipeline #131411 failed
......@@ -4,13 +4,12 @@ Import guestsdata from json.
import datetime
import json
from typing import Optional
import structlog
# from django.conf import settings
from django.core.management.base import BaseCommand, CommandParser
from django.db import IntegrityError
from django.utils.timezone import make_aware
from greg.models import (
......@@ -38,6 +37,7 @@ class Command(BaseCommand):
Identity.IdentityType.FEIDE_ID,
Identity.IdentityType.NORWEGIAN_NATIONAL_ID_NUMBER,
Identity.IdentityType.PASSPORT_NUMBER,
Identity.IdentityType.MIGRATION_ID,
]
REQUIRED_IDS = [
......@@ -49,6 +49,7 @@ class Command(BaseCommand):
super().__init__(*args, **kwargs)
self._orgunit_id_type = "legacy_stedkode"
self._end_date = datetime.datetime.today() + datetime.timedelta(days=100)
self._ignore_id_req = False
def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument(
......@@ -66,6 +67,12 @@ class Command(BaseCommand):
type=datetime.date.fromisoformat,
help="End date of roles. Default: today + 100 days",
)
parser.add_argument(
"--ignore-id-req",
default=False,
action="store_true",
help="Imports people without required ids if set",
)
def _find_person_from_ids(self, ids: dict) -> Optional[Person]:
"""
......@@ -198,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()
......@@ -217,24 +229,47 @@ 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"""
matching_ids = [x for x in id_data if x["id_type"] in self.REQUIRED_IDS]
return len(matching_ids) > 0
def create_migration_entry(self, person: Person, external_id: str):
"""Mark the person with entity id from cerebrum"""
existing_entry = Identity.objects.filter(
person=person, type=Identity.IdentityType.MIGRATION_ID, value=external_id
)
if existing_entry:
return
try:
Identity.objects.create(
person=person,
type=Identity.IdentityType.MIGRATION_ID,
source="cerebrum",
value=external_id,
verified=Identity.Verified.AUTOMATIC,
verified_at=make_aware(datetime.datetime.now()),
)
except IntegrityError as e:
logger.error("Migration ID in use by a different person")
raise e
def upsert_person(self, external_id: str, data: dict) -> Optional[Person]:
"""Add or update person"""
if not self._has_required_id(data["ids"]):
if not self._ignore_id_req and not self._has_required_id(data["ids"]):
logger.error("missing_required_id", external_id=external_id)
return None
......@@ -263,6 +298,7 @@ class Command(BaseCommand):
self.upsert_identity(person, identity)
for role in data["role"]:
self.upsert_role(person, role)
self.create_migration_entry(person, external_id)
return person
def handle(self, *args, **options):
......@@ -272,6 +308,7 @@ class Command(BaseCommand):
self._orgunit_id_type = options["orgunit_type"]
self._end_date = options["end_date"]
self._ignore_id_req = options["ignore_id_req"]
with open(options["file"], "r", encoding="UTF-8") as fp:
persons = json.load(fp)
......
# Generated by Django 4.0.2 on 2022-04-29 14:37
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("greg", "0021_identity_identity_type_value_unique"),
]
operations = [
migrations.AlterField(
model_name="identity",
name="type",
field=models.CharField(
choices=[
("feide_id", "Feide Id"),
("feide_email", "Feide Email"),
("passport_number", "Passport Number"),
("norwegian_national_id_number", "Norwegian National Id Number"),
("private_email", "Private Email"),
("private_mobile", "Private Mobile Number"),
("migration_id", "Migration Id"),
],
max_length=64,
),
),
]
......@@ -272,6 +272,8 @@ class Identity(BaseModel):
NORWEGIAN_NATIONAL_ID_NUMBER = "norwegian_national_id_number"
PRIVATE_EMAIL = "private_email"
PRIVATE_MOBILE_NUMBER = "private_mobile"
# Used to identify the id of this person in some other system
MIGRATION_ID = "migration_id"
class Verified(models.TextChoices):
AUTOMATIC = "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