diff --git a/apis_core/apis_entities/tests.py b/apis_core/apis_entities/tests.py index 866bd13..677505b 100644 --- a/apis_core/apis_entities/tests.py +++ b/apis_core/apis_entities/tests.py @@ -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", @@ -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()) diff --git a/archemd/arche_md_utils.py b/archemd/arche_md_utils.py index d194443..7b36b84 100644 --- a/archemd/arche_md_utils.py +++ b/archemd/arche_md_utils.py @@ -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 @@ -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" diff --git a/archemd/tests.py b/archemd/tests.py index a79ca8b..85e7ff6 100644 --- a/archemd/tests.py +++ b/archemd/tests.py @@ -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) diff --git a/archemd/urls.py b/archemd/urls.py index 732495e..bb6aaa3 100644 --- a/archemd/urls.py +++ b/archemd/urls.py @@ -2,6 +2,8 @@ from .views import entity_as_arche +app_name = "archemd" + urlpatterns = [ - path("", entity_as_arche), + path("", entity_as_arche, name="arche"), ] diff --git a/normdata/templates/normdata/create_from_gnd.html b/normdata/templates/normdata/create_from_gnd.html index 14770a1..d4cc89e 100644 --- a/normdata/templates/normdata/create_from_gnd.html +++ b/normdata/templates/normdata/create_from_gnd.html @@ -7,7 +7,7 @@

Neue Entität aus GND/WikiData/GeoNames importieren<
{% csrf_token %} {{ form|crispy }} - +
diff --git a/normdata/utils.py b/normdata/utils.py index 07ef6c2..f4a6852 100644 --- a/normdata/utils.py +++ b/normdata/utils.py @@ -1,12 +1,19 @@ 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: @@ -14,44 +21,98 @@ def get_uri_domain(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): @@ -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 diff --git a/pmb/urls.py b/pmb/urls.py index 8e3dbf1..a8a7d7d 100644 --- a/pmb/urls.py +++ b/pmb/urls.py @@ -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)