diff --git a/apis_ontology/management/commands/import.py b/apis_ontology/management/commands/import.py new file mode 100644 index 0000000..0500687 --- /dev/null +++ b/apis_ontology/management/commands/import.py @@ -0,0 +1,77 @@ +import requests + +from django.core.management.base import BaseCommand + +from django.contrib.contenttypes.models import ContentType + +from apis_ontology.models import Event, Institution, Person, Place, Work, Title +from apis_core.apis_metainfo.models import Uri, RootObject + +SRC="https://apis.acdh.oeaw.ac.at/apis/api" + +class Command(BaseCommand): + help = "Import data from legacy APIS instance" + + def handle(self, *args, **options): + entities = { + "event": { + "dst": Event + }, + "institution": { + "dst": Institution, + }, + "person": { + "dst": Person, + }, + "place": { + "dst": Place, + }, + "work": { + "dst": Work, + } + } + + # Migrate entities + for entity, entitysettings in entities.items(): + nextpage = f"{SRC}/entities/{entity}/?format=json&limit=500" + while nextpage: + print(nextpage) + page = requests.get(nextpage) + data = page.json() + nextpage = data['next'] + for result in data["results"]: + result_id = result["id"] + if "kind" in result and result["kind"] is not None: + result["kind"] = result["kind"]["label"] + if "profession" in result and result["profession"] is not None: + result["profession"] = ",".join([x["label"] for x in result["profession"]]) + titlelist = [] + if "title" in result: + for title in result["title"]: + newtitle, created = Title.objects.get_or_create(name=title) + titlelist.append(newtitle) + del result["title"] + newentity, created = entitysettings["dst"].objects.get_or_create(pk=result_id) + for attribute in result: + if hasattr(newentity, attribute): + setattr(newentity, attribute, result[attribute]) + for title in titlelist: + newentity.title.add(title) + newentity.save() + + # Migrate URIs + nextpage = f"{SRC}/metainfo/uri/?format=json" + while nextpage: + print(nextpage) + page = requests.get(nextpage) + data = page.json() + nextpage = data['next'] + for result in data["results"]: + newuri, created = Uri.objects.get_or_create(uri=result["uri"]) + try: + result["root_object"] = RootObject.objects.get(pk=result["entity"]["id"]) + for attribute in result: + if hasattr(newuri, attribute): + setattr(newuri, attribute, result[attribute]) + except RootObject.DoesNotExist: + pass diff --git a/migrationscript.py b/migrationscript.py deleted file mode 100755 index ca5d9de..0000000 --- a/migrationscript.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/python3 - -import requests -import getpass - -SRC="https://apis.acdh.oeaw.ac.at/apis/api" -DST="https://oebl-pnp.acdh-dev.oeaw.ac.at" -#DST="http://localhost:8000" -DSTAPISAPI=f"{DST}/apis/api" -DSTCSTMAPI=f"{DST}/custom-api/writeuri" - - -from requests.auth import HTTPBasicAuth - -user = input("Username:") -password = getpass.getpass() - -basic = HTTPBasicAuth(user, password) - -entitytypes = ['event', 'institution', 'person', 'place', 'work'] -for entitytype in entitytypes: - nextpage = f"{SRC}/entities/{entitytype}/?format=json" - while nextpage: - page = requests.get(nextpage) - data = page.json() - nextpage = data['next'] - for result in data["results"]: - result_id = result["id"] - if "kind" in result and result["kind"] is not None: - result["kind"] = result["kind"]["label"] - if "profession" in result and result["profession"] is not None: - result["profession"] = ",".join([x["label"] for x in result["profession"]]) - # we first try to overwrite existing entries - response = requests.put(f"{DSTAPISAPI}/ontology/{entitytype}/{result_id}/", result, auth=basic) - # if no entry with that id exists, we create a new one - if response.status_code == 404: - response = requests.post(f"{DSTAPISAPI}/ontology/{entitytype}/", result, auth=basic) - if not response: - print(response.text) - print(result) - - -nextpage = f"{SRC}/metainfo/uri/?format=json" -while nextpage: - page = requests.get(nextpage) - data = page.json() - nextpage = data['next'] - for result in data["results"]: - result_id = result["id"] - result["root_object"] = result["entity"]["id"] - del result["entity"] - # we first try to overwrite existing entries - response = requests.put(f"{DSTCSTMAPI}/{result_id}/", result, auth=basic) - # if no entry with that id exists, we create a new one - if response.status_code == 404: - response = requests.post(f"{DSTCSTMAPI}/", result, auth=basic) - if not response: - print(response.text) - print(result)