diff --git a/apis_core/apis_entities/tests.py b/apis_core/apis_entities/tests.py index ed93e38..6752f69 100644 --- a/apis_core/apis_entities/tests.py +++ b/apis_core/apis_entities/tests.py @@ -189,7 +189,7 @@ def test_013_import_normdata_no_wikidata(self): "normdata:import_from_normdata", ) response = client.post(url, payload, follow=True) - self.assertEqual(response.status_code, 404) + self.assertEqual(response.status_code, 200) def test_014_wikidata_place_exist(self): entity = get_or_create_place_from_wikidata( @@ -197,18 +197,23 @@ def test_014_wikidata_place_exist(self): ) ic(entity) for x in entity.uri_set.all(): - ic(x) - ic(x.uri) entity = get_or_create_place_from_wikidata(x.uri) - ic(entity) self.assertTrue(entity) def test_015_wikidata_person_exist(self): entity = import_from_normdata("http://lobid.org/gnd/133430553", "person") - ic(entity) for x in entity.uri_set.all(): - ic(x) - ic(x.uri) entity = get_or_create_person_from_wikidata(x.uri) - ic(entity) self.assertTrue(entity) + + def test_016_import_nonsense_geonames(self): + client.login(**USER) + payload = { + "normdata_url": "https://www.geonames.org/2461123321492/graret-um-igufen.html", + "entity_type": "place", + } + url = reverse( + "normdata:import_from_normdata", + ) + response = client.post(url, payload, follow=True) + self.assertEqual(response.status_code, 404) diff --git a/normdata/tests.py b/normdata/tests.py new file mode 100644 index 0000000..6a4353e --- /dev/null +++ b/normdata/tests.py @@ -0,0 +1,13 @@ +from django.test import TestCase + +from normdata.utils import get_or_create_place_from_geonames + +GEONAMES_URL = "https://www.geonames.org/2461464/graret-oum-sedra.html" + + +class NormdataTestCase(TestCase): + def test_001_get_or_create_place_from_geonames(self): + entity = get_or_create_place_from_geonames(GEONAMES_URL) + self.assertEqual(entity.name, "Graret Oum Sedra") + entity = get_or_create_place_from_geonames(GEONAMES_URL) + entity.delete() diff --git a/normdata/utils.py b/normdata/utils.py index 8a13800..d0a2c55 100644 --- a/normdata/utils.py +++ b/normdata/utils.py @@ -1,9 +1,11 @@ +from acdh_geonames_utils.gn_client import gn_as_object 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 icecream import ic from apis_core.apis_entities.models import Person, Place from apis_core.apis_metainfo.models import Uri @@ -21,6 +23,28 @@ def get_uri_domain(uri): return x[1] +def get_or_create_place_from_geonames(uri): + uri = get_normalized_uri(uri) + try: + entity = Uri.objects.get(uri=uri).entity + entity = Place.objects.get(id=entity.id) + return entity + except ObjectDoesNotExist: + geonames_obj = gn_as_object(uri) + apis_entity = { + "name": geonames_obj["name"], + "lat": geonames_obj["latitude"], + "lng": geonames_obj["longitude"], + } + entity = Place.objects.create(**apis_entity) + Uri.objects.create( + uri=uri, + domain="geonames", + entity=entity, + ) + return entity + + def get_or_create_place_from_wikidata(uri): try: entity = Uri.objects.get(uri=uri).entity @@ -132,7 +156,12 @@ def import_from_normdata(raw_url, entity_type): try: wikidata_url = geonames_to_wikidata(normalized_url)["wikidata"] except (IndexError, KeyError): - wikidata_url = False + try: + entity = get_or_create_place_from_geonames(normalized_url) + return entity + except Exception as e: + ic(e) + wikidata_url = False elif domain == "wikidata": wikidata_url = normalized_url else: diff --git a/normdata/views.py b/normdata/views.py index 5ee028c..015e014 100644 --- a/normdata/views.py +++ b/normdata/views.py @@ -1,7 +1,6 @@ from django.http import HttpResponseNotFound from django.shortcuts import redirect from django.views.generic.edit import FormView -from icecream import ic from .forms import NormDataImportForm from .utils import import_from_normdata @@ -18,9 +17,6 @@ def form_valid(self, form): if temp_ent is not None: entity = temp_ent.get_child_entity() redirect_url = entity.get_edit_url() - ic(temp_ent) - ic(entity) - ic(redirect_url) return redirect(redirect_url) else: return HttpResponseNotFound( diff --git a/requirements.txt b/requirements.txt index b3614e7..c55562c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ apis-override-select2js==0.1 acdh-django-browsing +acdh_geonames_utils acdh-id-reconciler>=0.2,<1 acdh-tei-pyutils>=0.34,<1 acdh-wikidata-pyutils>=0.2.1,<1