Skip to content

Commit

Permalink
Merge pull request #11 from arthur-schnitzler/main
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
csae8092 authored Dec 31, 2023
2 parents 60e8211 + c780a98 commit 55fac00
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 47 deletions.
6 changes: 3 additions & 3 deletions apis_core/apis_entities/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_010_delete_views(self):
self.assertContains(response, item.id)
item.delete()

def test_010_import_nordmdata_view(self):
def test_011_import_nordmdata_view(self):
client.login(**USER)
payload = {
"normdata_url": "http://lobid.org/gnd/118566512",
Expand Down Expand Up @@ -165,10 +165,10 @@ def test_010_import_nordmdata_view(self):
response = client.post(url, payload, follow=True)
self.assertEqual(response.status_code, 200)

def test_011_import_normdata_form(self):
def test_012_import_normdata_form(self):
payload = {
"normdata_url": "http://lobid.org/gnd/118566512",
"entity_type": "person"
"entity_type": "person",
}
form = NormDataImportForm(data=payload)
self.assertTrue(form.is_valid())
5 changes: 2 additions & 3 deletions archemd/arche_md_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from rdflib import RDF, XSD, Graph, Literal, Namespace, URIRef

from apis_core.apis_metainfo.models import TempEntityClass
Expand Down Expand Up @@ -55,8 +54,8 @@ def __init__(self, entity_id):
self.item = TempEntityClass.objects.get(id=entity_id)
self.entity = self.item.get_child_entity()
self.entity_class_name = self.entity.__class__.__name__.lower()
self.detail_view_url = settings.PMB_DETAIL_VIEW_PATTERN.format(
self.entity_class_name, self.entity_id
self.detail_view_url = (
f"https://pmb.acdh.oeaw.ac.at{self.entity.get_absolute_url()}"
)
if self.entity_class_name == "institution":
self.arche_class = "Organization"
Expand Down
52 changes: 50 additions & 2 deletions archemd/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,51 @@
# from django.test import TestCase
from django.apps import apps
from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse

# Create your tests here.
from apis_core.apis_entities.models import Person, Place
from archemd.arche_md_utils import ArcheMd
from normdata.utils import import_from_normdata

client = Client()
USER = {"username": "testuser", "password": "somepassword"}
BAHR = {"name": "Bahr", "first_name": "Hermann", "start_date_written": "1900"}

ENTITY_TYPES = ["person", "place", "event", "work", "institution"]

MODELS = list(apps.all_models["apis_entities"].values())

URIS = [
("https://www.geonames.org/2772400/linz.html", "place"),
("https://www.wikidata.org/wiki/Q100965214", "person"),
("http://lobid.org/gnd/1028192029", "person"),
]


class ArcheMedTestCase(TestCase):
def setUp(self):
User.objects.create_user(**USER)
Person.objects.create(**BAHR)
for x in URIS:
import_from_normdata(x[0], x[1])

def test_01_archemd(self):
item = Person.objects.last()
arche_md = ArcheMd(item.id)
g = arche_md.return_graph()
self.assertTrue(g)

def test_02_archemd_view(self):
for item in Person.objects.all():
url = reverse("archemd:arche", kwargs={"pk": item.id})
response = client.get(url)
self.assertTrue(response.status_code, 200)
for item in Place.objects.all():
url = reverse("archemd:arche", kwargs={"pk": item.id})
response = client.get(url)
self.assertTrue(response.status_code, 200)

def test_03_archemd_doesnotexist(self):
url = reverse("archemd:arche", kwargs={"pk": 99999})
response = client.get(url)
self.assertTrue(response.status_code, 404)
4 changes: 3 additions & 1 deletion archemd/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .views import entity_as_arche

app_name = "archemd"

urlpatterns = [
path("<int:pk>", entity_as_arche),
path("<int:pk>", entity_as_arche, name="arche"),
]
2 changes: 1 addition & 1 deletion normdata/templates/normdata/create_from_gnd.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1 class="p-3 text-center">Neue Entität aus GND/WikiData/GeoNames importieren<
<form action="." method="post">
{% csrf_token %}
{{ form|crispy }}
<input type="submit" value="Importieren">
<input type="submit" class="btn btn-primary" value="Importieren">
</form>
</div>

Expand Down
136 changes: 100 additions & 36 deletions normdata/utils.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,118 @@
from acdh_id_reconciler import geonames_to_wikidata, gnd_to_wikidata
from acdh_wikidata_pyutils import WikiDataPerson, WikiDataPlace
from AcdhArcheAssets.uri_norm_rules import get_normalized_uri
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.utils import IntegrityError

from apis_core.apis_entities.models import Person, Place
from apis_core.apis_metainfo.models import Uri
from apis_core.apis_relations.models import PersonPlace
from apis_core.apis_vocabularies.models import PersonPlaceRelation
from dumper.utils import DOMAIN_MAPPING

BIRTH_REL = getattr(settings, "BIRTH_REL")
DEATH_REL = getattr(settings, "DEATH_REL")


def get_uri_domain(uri):
for x in DOMAIN_MAPPING:
if x[0] in uri:
return x[1]


def import_from_wikidata(wikidata_url, entity_type):
if entity_type == "person":
wd_entity = WikiDataPerson(wikidata_url)
apis_entity = wd_entity.get_apis_entity()
entity = Person.objects.create(**apis_entity)
Uri.objects.create(
uri=get_normalized_uri(wikidata_url),
domain="wikidata",
entity=entity,
)
if wd_entity.gnd_uri:
Uri.objects.create(
uri=get_normalized_uri(wd_entity.gnd_uri),
domain="gnd",
entity=entity,
)
else:
wd_entity = WikiDataPlace(wikidata_url)
apis_entity = wd_entity.get_apis_entity()
entity = Place.objects.create(**apis_entity)
Uri.objects.create(
uri=get_normalized_uri(wikidata_url),
domain="wikidata",
entity=entity,
)
if wd_entity.gnd_uri:
Uri.objects.create(
uri=get_normalized_uri(wd_entity.gnd_uri),
domain="gnd",
entity=entity,
)
if wd_entity.geonames_uri:
def get_or_create_place_from_wikidata(uri):
try:
entity = Uri.objects.get(uri=uri).entity
entity = Place.objects.get(id=entity.id)
return entity
except ObjectDoesNotExist:
wd_entity = WikiDataPlace(uri)
try:
entity = Uri.objects.get(uri=wd_entity.geonames_uri).entity
entity = Place.objects.get(id=entity.id)
return entity
except ObjectDoesNotExist:
try:
entity = Uri.objects.get(uri=wd_entity.gnd_uri).entity
entity = Place.objects.get(id=entity.id)
return entity
except ObjectDoesNotExist:
apis_entity = wd_entity.get_apis_entity()
entity = Place.objects.create(**apis_entity)
Uri.objects.create(
uri=get_normalized_uri(uri),
domain="wikidata",
entity=entity,
)
try:
if wd_entity.gnd_uri:
Uri.objects.create(
uri=get_normalized_uri(wd_entity.gnd_uri),
domain="gnd",
entity=entity,
)
except IntegrityError:
pass
try:
if wd_entity.geonames_uri:
Uri.objects.create(
uri=get_normalized_uri(wd_entity.geonames_uri),
domain="geonames",
entity=entity,
)
except IntegrityError:
pass
return entity


def get_or_create_person_from_wikidata(uri):
try:
entity = Uri.objects.get(uri=uri).entity
entity = Person.objects.get(id=entity.id)
return entity
except ObjectDoesNotExist:
wd_entity = WikiDataPerson(uri)
try:
entity = Uri.objects.get(uri=wd_entity.gnd_uri).entity
entity = Person.objects.get(id=entity.id)
return entity
except ObjectDoesNotExist:
apis_entity = wd_entity.get_apis_entity()
entity = Person.objects.create(**apis_entity)
Uri.objects.create(
uri=get_normalized_uri(wd_entity.geonames_uri),
domain="geonames",
uri=get_normalized_uri(uri),
domain="wikidata",
entity=entity,
)
return entity
if wd_entity.gnd_uri:
try:
Uri.objects.create(
uri=get_normalized_uri(wd_entity.gnd_uri),
domain="gnd",
entity=entity,
)
except IntegrityError:
pass
if wd_entity.place_of_birth:
relation_type = PersonPlaceRelation.objects.get(id=BIRTH_REL[0])
place = get_or_create_place_from_wikidata(wd_entity.place_of_birth)
rel, _ = PersonPlace.objects.get_or_create(
related_person=entity,
related_place=place,
relation_type=relation_type,
start_date_written=apis_entity["start_date_written"],
)
if wd_entity.place_of_death:
relation_type = PersonPlaceRelation.objects.get(id=DEATH_REL[0])
place = get_or_create_place_from_wikidata(wd_entity.place_of_death)
rel, _ = PersonPlace.objects.get_or_create(
related_person=entity,
related_place=place,
relation_type=relation_type,
start_date_written=apis_entity["end_date_written"],
)
return entity


def import_from_normdata(raw_url, entity_type):
Expand Down Expand Up @@ -81,7 +142,10 @@ def import_from_normdata(raw_url, entity_type):
entity = Uri.objects.get(uri=normalized_url).entity
return entity
except ObjectDoesNotExist:
entity = import_from_wikidata(wikidata_url, entity_type)
if entity_type == "place":
entity = get_or_create_place_from_wikidata(wikidata_url)
else:
entity = get_or_create_person_from_wikidata(wikidata_url)
else:
entity = None
return entity
2 changes: 1 addition & 1 deletion pmb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
path("apis/", include("apis_core.urls", namespace="apis")),
path("normdata/", include("normdata.urls", namespace="normdata")),
path("admin/", admin.site.urls),
path("arche/", include("archemd.urls")),
path("arche/", include("archemd.urls", namespace="archemd")),
path("", include("dumper.urls", namespace="dumper")),
path("browsing", include("browsing.urls", namespace="browsing")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

0 comments on commit 55fac00

Please sign in to comment.