-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move migration script to django command
- Loading branch information
Showing
2 changed files
with
77 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.