Skip to content

Commit

Permalink
feat: add Source model and import logic
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Sep 28, 2023
1 parent 12cf16b commit 47810da
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
23 changes: 22 additions & 1 deletion apis_ontology/management/commands/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@

from django.core.management.base import BaseCommand

from apis_ontology.models import Event, Institution, Person, Place, Work, Title, Profession
from apis_ontology.models import Event, Institution, Person, Place, Work, Title, Profession, Source
from apis_core.apis_metainfo.models import Uri, RootObject
from apis_core.apis_relations.models import Property, TempTriple

SRC="https://apis.acdh.oeaw.ac.at/apis/api"


def import_sources():
nextpage = f"{SRC}/metainfo/source/?format=json&limit=1000"
while nextpage:
print(nextpage)
page = requests.get(nextpage)
data = page.json()
nextpage = data['next']
for result in data["results"]:
print(result["url"])
newsource, created = Source.objects.get_or_create(id=result["id"])
for attribute in result:
if hasattr(newsource, attribute):
setattr(newsource, attribute, result[attribute])
newsource.save()


class Command(BaseCommand):
help = "Import data from legacy APIS instance"

def add_arguments(self, parser):
parser.add_argument("--entities", action="store_true")
parser.add_argument("--urls", action="store_true")
parser.add_argument("--relations", action="store_true")
parser.add_argument("--sources", action="store_true")
parser.add_argument("--all")


Expand All @@ -24,6 +41,10 @@ def handle(self, *args, **options):
options["entities"] = True
options["urls"] = True
options["relations"] = True
options["sources"] = True

if options["sources"]:
import_sources()

entities = {
"event": {
Expand Down
24 changes: 24 additions & 0 deletions apis_ontology/migrations/0005_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.1.11 on 2023-09-28 04:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('apis_ontology', '0004_remove_person_profession_profession_and_more'),
]

operations = [
migrations.CreateModel(
name='Source',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('orig_filename', models.CharField(blank=True, max_length=255)),
('indexed', models.BooleanField(default=False)),
('pubinfo', models.CharField(blank=True, max_length=400)),
('author', models.CharField(blank=True, max_length=255)),
('orig_id', models.PositiveIntegerField(blank=True, null=True)),
],
),
]
13 changes: 13 additions & 0 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ class Meta:
abstract = True


class Source(models.Model):
orig_filename = models.CharField(max_length=255, blank=True)
indexed = models.BooleanField(default=False)
pubinfo = models.CharField(max_length=400, blank=True)
author = models.CharField(max_length=255, blank=True)
orig_id = models.PositiveIntegerField(blank=True, null=True)

def __str__(self):
if self.author and self.orig_filename:
return f"{self.orig_filename}, stored by {self.author}"
return f"(ID: {self.id})".format(self.id)


class Title(models.Model):
name = models.CharField(max_length=255, blank=True)

Expand Down

0 comments on commit 47810da

Please sign in to comment.