Skip to content

Commit

Permalink
feat: move migration script to django command
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Sep 27, 2023
1 parent bc9cde0 commit bd36414
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 59 deletions.
77 changes: 77 additions & 0 deletions apis_ontology/management/commands/import.py
Original file line number Diff line number Diff line change
@@ -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
59 changes: 0 additions & 59 deletions migrationscript.py

This file was deleted.

0 comments on commit bd36414

Please sign in to comment.