def migrate_to_organization(modeladmin, request, queryset): """ Migrates a person to an organzation. Also migrates any relationships that may point to that person. """ for entity in queryset: d = { 'legacy_id': entity.legacy_id, 'name': entity.last_name } o = Organization(**d) o.save() # Check the original entry's relationships and migrate them. source_relationships = entity.sources_related.all() for rel in source_relationships: rel.related_entity = o rel.save() source_copyist = entity.sources_copied.all() for rel in source_copyist: rel.copyist = o rel.save() source_provenance = entity.sources_provenance.all() for rel in source_provenance: rel.entity = o rel.save() # delete the original entity. entity.delete()
def migrate_organizations(entry): print(term.green("\tMigrating person record {0} to organization".format(entry.pk))) affiliation_name = None otype = OrganizationType.objects.get(pk=1) legacy_id = "legacy_person.{0}".format(int(entry.pk)) if entry.alaffiliationkey: affiliation = LegacyAffiliation.objects.get(pk=entry.alaffiliationkey) affiliation_name = affiliation.affiliation orgname = None if entry.surname: orgname = entry.surname.strip() d = { 'name': orgname, 'note': affiliation_name, 'variant_names': entry.aliases, 'type': otype, 'legacy_id': legacy_id } o = Organization(**d) o.save()
def migrate_source_provenance(entry): print(term.green("\tMigrating Source Provenance ID {0}".format(int(entry.pk)))) note = "Original: " source = Source.objects.get(pk=int(entry.sourcekey)) legacy_id = "legacy_provenance.{0}".format(int(entry.alprovenancekey)) country = None try: country = GeographicArea.objects.get(legacy_id__name=legacy_id) except GeographicArea.DoesNotExist: raise ("Could not determine provenance country with ID {0}. Bailing".format(int(entry.alprovenancekey))) note = "{0} {1} ({2})".format(note, country.name, entry.alprovenancekey) city = None if entry.city: cname = entry.city.rstrip("?") lid = "legacy_source_provenance_city.{0}_{1}".format(entry.alprovenancekey, cname.lower()) try: city = GeographicArea.objects.get(legacy_id__name=lid) except GeographicArea.DoesNotExist: try: print(term.red("\tCity may not exist. Checking by name {0}, {1}.".format(cname, country.name))) city = GeographicArea.objects.get(name=cname, parent=country) lgid = LegacyId(name=lid) lgid.save() city.legacy_id.add(lgid) city.save() except GeographicArea.DoesNotExist: print(term.red("\tCity {0} does not exist. Attempting to create.".format(cname))) d = {"type": GeographicArea.CITY, "name": cname, "parent": country} city = GeographicArea(**d) city.save() lgid = LegacyId(name=lid) lgid.save() city.legacy_id.add(lgid) city.save() note = note + " " + entry.city organization = None if entry.institution: orgname = entry.institution.strip("? ") try: organization = Organization.objects.get(name=orgname) except Organization.DoesNotExist: print(term.red("\tOrganization {0} does not exist. Attempting to create.".format(orgname))) location = city if city else country d = { "name": orgname, "location": location, "legacy_id": "legacy_source_provenance.{0}".format(int(entry.pk)), } organization = Organization(**d) organization.save() note = note + " " + entry.institution region = None if entry.region: regname = entry.region.strip("?") legacy_id = "legacy_source_provenance_region.{0}_{1}".format(entry.alprovenancekey, regname.lower()) try: region = GeographicArea.objects.get(legacy_id__name=legacy_id) except GeographicArea.DoesNotExist: try: region = GeographicArea.objects.get(name=regname) lgid = LegacyId(name=legacy_id) lgid.save() region.legacy_id.add(lgid) except GeographicArea.DoesNotExist: d = {"name": regname, "type": GeographicArea.REGION, "parent": country} region = GeographicArea(**d) region.save() lgid = LegacyId(name=legacy_id) lgid.save() region.legacy_id.add(lgid) region.save() note = note + " " + entry.region protectorate = None if entry.protectorate: pname = entry.protectorate.strip("?") legacy_id = "legacy_source_provenance_protectorate.{0}_{1}".format(entry.alprovenancekey, pname.lower()) try: protectorate = GeographicArea.objects.get(legacy_id__name=legacy_id) except GeographicArea.DoesNotExist: try: protectorate = GeographicArea.objects.get(name=pname, type=GeographicArea.REGION) lgid = LegacyId(name=legacy_id) lgid.save() protectorate.legacy_id.add(lgid) protectorate.save() except GeographicArea.DoesNotExist: d = {"name": pname, "type": GeographicArea.REGION, "parent": country} protectorate = GeographicArea(**d) protectorate.save() lgid = LegacyId(name=legacy_id) lgid.save() protectorate.legacy_id.add(lgid) protectorate.save() note = note + " " + entry.protectorate sp = { "source": source, "country": country, "country_uncertain": convert_yn_to_boolean(entry.uncertain), "city": city, "city_uncertain": convert_yn_to_boolean(entry.city_uncertain), "protectorate": protectorate, "region": region, "region_uncertain": convert_yn_to_boolean(entry.region_uncertain), "note": note, } if organization: # Generic Foreign Keys don't like None sp.update({"entity": organization, "entity_uncertain": convert_yn_to_boolean(entry.institution_uncertain)}) spe = SourceProvenance(**sp) spe.save()