From e58b129a6ad3933a0dc9e140b7311ea87038bb80 Mon Sep 17 00:00:00 2001
From: Judit Foglszinger <urbec@debian.org>
Date: Fri, 15 May 2020 16:10:48 +0700
Subject: [PATCH 2/3] importing identities

---
 backend/export.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/backend/export.py b/backend/export.py
index 7239aae..9426299 100644
--- a/backend/export.py
+++ b/backend/export.py
@@ -125,6 +125,9 @@ class ImporterV1:
         # Site checks
         self.inconsistencies = []
 
+        # Login Identities
+        self.identities = []
+
         self.context = {"importer": self}
 
     def import_person(self, data, shallow=False):
@@ -446,6 +449,20 @@ class ImporterV1:
 
         return inc
 
+    def import_identity(self, data):
+        s = snserializers.IdentityExportSerializer(data=data, context=self.context)
+        if not s.is_valid():
+            raise RuntimeError(
+                "Invalid Identity record {}: {}".format(data, s.errors)
+            )
+        p = s.validated_data.pop("person")
+        person = self.people[p] if p else None
+
+        identity = snmodels.Identity(**s.validated_data)
+        identity._rels = {"person": person}
+
+        return identity
+
     def import_all(self, data):
         # We do a double pass to create all people by lookup key, so that they
         # can be referenced in dependent objects
@@ -467,6 +484,9 @@ class ImporterV1:
         for inconsistency in data["inconsistencies"]:
             self.inconsistencies.append(self.import_inconsistency(inconsistency))
 
+        for identity in data["identities"]:
+            self.identities.append(self.import_identity(identity))
+
     def save(self):
         for person in self.people.values():
             person.save(audit_skip=True)
@@ -535,6 +555,10 @@ class ImporterV1:
             inconsistency.process = inconsistency._rels["process"]
             inconsistency.save()
 
+        for identity in self.identities:
+            identity.person = identity._rels["person"]
+            identity.save(audit_skip=True)
+
 
 # def parse_datetime(s):
 #    if s is None:
-- 
2.26.2

