def migrate_person_to_people(legacy_person): print(term.green('\tMigrating person ID {0}'.format(legacy_person.pk))) legacy_id = "legacy_person.{0}".format(legacy_person.pk) d = { 'last_name': legacy_person.surname, 'first_name': legacy_person.firstname, 'earliest_year': legacy_person.startdate, 'earliest_year_approximate': convert_yn_to_boolean(legacy_person.startdate_approx), 'latest_year': legacy_person.enddate, 'latest_year_approximate': convert_yn_to_boolean(legacy_person.enddate_approx), 'legacy_id': legacy_id } p = Person(**d) p.save() notes = ( (PersonNote.VARIANT_NAME_NOTE, legacy_person.aliases), ) for n in notes: if not n[1]: continue d = { 'note': n[1], 'type': n[0], 'person': p } pn = PersonNote(**d) pn.save()
def attach_composers_to_composition(entry): print(term.green("\tAttaching composer {0} to composition {1} with PK {2}".format(entry.composerkey, entry.compositionkey, entry.pk))) composition_pk = entry.compositionkey composition = Composition.objects.get(pk=composition_pk) # Set the composition to anonymous if the composer is anonymous. if entry.composerkey == 0: composition.anonymous = True composition.save() return None composer_pk = entry.composerkey composer_lookup = "legacy_composer.{0}".format(int(composer_pk)) composer = Person.objects.get(legacy_id=composer_lookup) uncertain = convert_yn_to_boolean(entry.attribution_uncertain) notes = entry.notes_attribution d = { 'composer': composer, 'composition': composition, 'uncertain': uncertain, 'notes': notes } cc = CompositionComposer(**d) cc.save()
def convert_image(entry): print(term.green("\tMigrating image with ID {0} to a Page".format(entry.pk))) source = Source.objects.get(pk=int(entry.sourcekey)) folio = entry.folio if entry.folio else "FIXMEFOLIO" d = { 'numeration': folio, 'sort_order': entry.orderno, 'source': source, 'legacy_id': "legacy_image.{0}".format(entry.pk) } p = Page(**d) p.save() filename = determine_filename(entry) if filename: # create an image record. print(term.green("\t\tCreating an Image record for Image {0} ({1})".format(entry.pk, filename))) available = convert_yn_to_boolean(entry.availwebsite) imtype = ImageType.objects.get(pk=ImageType.PRIMARY) imd = { 'page': p, 'public': available, 'type': imtype, 'legacy_filename': filename, 'legacy_id': 'legacy_image.{0}'.format(int(entry.pk)) } im = Image(**imd) im.save() else: print(term.yellow("\t\tSkipping {0}".format(filename)))
def migrate_source_relationship(entry): print(term.green("\tMigrating Source Relationship ID {0}".format(entry.pk))) source_pk = entry.sourcekey source = Source.objects.get(pk=source_pk) ent_pk = entry.alpersonkey entity_lookup = "legacy_person.{0}".format(int(ent_pk)) try: entity = Person.objects.get(legacy_id=entity_lookup) except Person.DoesNotExist: entity = Organization.objects.get(legacy_id=entity_lookup) rtype_pk = entry.alpersonrelationshipkey rtype = SourceRelationshipType.objects.get(pk=rtype_pk) uncertain = convert_yn_to_boolean(entry.attribution_uncertain) d = { 'source': source, 'related_entity': entity, 'relationship_type': rtype, 'uncertain': uncertain } sp = SourceRelationship(**d) sp.save()
def migrate_source_copyists(legacy_source_copyist): print(term.green("\tMigrating Copyist {0} for Source {1}".format(legacy_source_copyist.alcopyistkey, legacy_source_copyist.sourcekey))) source = Source.objects.get(pk=int(legacy_source_copyist.sourcekey)) copyist_id = "legacy_copyist.{0}".format(int(legacy_source_copyist.alcopyistkey)) copyist = Person.objects.get(legacy_id=copyist_id) uncertain = convert_yn_to_boolean(legacy_source_copyist.attribution_uncertain) d = { 'source': source, 'copyist': copyist, 'uncertain': uncertain, 'type': legacy_source_copyist.alcopyisttypekey } sc = SourceCopyist(**d) sc.save()
def migrate_source_bibliography(entry): print(term.green("\tMigrating source bibliography entry {0}".format(entry.pk))) source_pk = entry.sourcekey source = Source.objects.get(pk=source_pk) bibliography_pk = entry.bibliographykey bibliography = Bibliography.objects.get(pk=bibliography_pk) notes = entry.notes if entry.notes != 'none' else None # enter None if the text value is 'none' primarystudy = convert_yn_to_boolean(entry.primarystudy) d = { 'source': source, 'bibliography': bibliography, 'notes': notes, 'pages': entry.page, 'primary_study': primarystudy } sb = SourceBibliography(**d) sb.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()
def migrate_source_to_source(legacy_source): print(term.green("\tMigrating Source {0} with ID {1}".format(legacy_source.shelfmark, legacy_source.pk))) archive_pk = int(legacy_source.archivekey) archive = Archive.objects.get(pk=archive_pk) surface = __migrate_surface(legacy_source.surface) print(term.magenta("Converting surface {0} to type {1}".format(legacy_source.surface, surface))) start_date = __migrate_centuries_to_years(legacy_source.startdate, 1) end_date = __migrate_centuries_to_years(legacy_source.enddate) units = legacy_source.measurementunits if legacy_source.measurementunits else "mm" measurements = format_measurements(legacy_source.pagemeasurements, units) # Quite a few of these entries have trailing spaces source_type = None if legacy_source.sourcetype: source_type = legacy_source.sourcetype.strip() d = { 'id': legacy_source.pk, 'archive': archive, 'name': legacy_source.sourcename, 'shelfmark': legacy_source.shelfmark, 'type': source_type, 'surface': surface, 'start_date': start_date, 'end_date': end_date, 'date_statement': legacy_source.dateofsource, 'format': legacy_source.format, 'measurements': measurements, 'public': True, 'public_images': convert_yn_to_boolean(legacy_source.webpermission), 'inventory_provided': True # set this as true at this stage of the import; will be switched later when the inventory is actually imported. } s = Source(**d) s.save() other_identifiers = [ (SourceIdentifier.CCM, legacy_source.ccmabbrev), (SourceIdentifier.RISM, legacy_source.rismabbrev), (SourceIdentifier.RISM, legacy_source.altrismabbrev), (SourceIdentifier.EARP, legacy_source.earpdesignation), (SourceIdentifier.OLIM, legacy_source.olim_text_only) ] for i in other_identifiers: if i[1]: d = { 'identifier': i[1], 'type': i[0], 'source': s } sid = SourceIdentifier(**d) sid.save() if legacy_source.external_urls: split_list = legacy_source.external_urls.split('\r') finder = r'<a href="(?P<url>.*)"target="_blank">(?P<title>.*)</a>' for link in split_list: url_list = re.search(finder, link) if not url_list: continue title = url_list.group('title') url = url_list.group('url') utype = SourceURL.ANCILLARY if 'image' in title: utype = SourceURL.EXTERNAL_IMAGES d = { 'type': utype, 'link_text': title, 'link': url, 'source': s } surl = SourceURL(**d) surl.save() oth_num = None if legacy_source.othernumberings: oth_num = "Other numberings: {0}".format(legacy_source.othernumberings) general_description = None if legacy_source.generaldescription: general_description = legacy_source.generaldescription else: general_description = legacy_source.description_diamm contents_note = None if legacy_source.index and legacy_source.index.strip() != "none": contents_note = legacy_source.index notes = [ (SourceNote.DATE_NOTE, legacy_source.datecomments), (SourceNote.RISM_NOTE, legacy_source.description_rism), (SourceNote.PRIVATE_NOTE, legacy_source.notes), (SourceNote.LIMINARY_NOTE, legacy_source.liminarytext), (SourceNote.RULING_NOTE, legacy_source.stavegauge), (SourceNote.GENERAL_NOTE, general_description), (SourceNote.CCM_NOTE, legacy_source.description_ccm), (SourceNote.DEDICATION_NOTE, legacy_source.dedicationtext), (SourceNote.FOLIATION_NOTE, legacy_source.leafnumberingsystem), (SourceNote.FOLIATION_NOTE, legacy_source.leafnumberingdescription), (SourceNote.FOLIATION_NOTE, oth_num), (SourceNote.BINDING_NOTE, legacy_source.binding), (SourceNote.WATERMARK_NOTE, legacy_source.watermark), (SourceNote.PHYSICAL_NOTE, legacy_source.condition), (SourceNote.SURFACE_NOTE, legacy_source.surface), (SourceNote.CONTENTS_NOTE, contents_note), (SourceNote.NOTATION_NOTE, legacy_source.notation) ] for n in notes: if n[1]: d = { 'note': n[1], 'type': n[0], 'source': s } note = SourceNote(**d) note.save()