Skip to content

Commit

Permalink
Merge pull request #34 from arthur-schnitzler/32-import-place-from-ge…
Browse files Browse the repository at this point in the history
…onames-if-no-matching-wikidata-id-exists

get_or_create form geonames
  • Loading branch information
csae8092 authored Dec 31, 2023
2 parents f1239c5 + f719b7e commit bab60a8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
21 changes: 13 additions & 8 deletions apis_core/apis_entities/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,26 +189,31 @@ 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(
"http://www.wikidata.org/entity/Q1741"
)
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)
13 changes: 13 additions & 0 deletions normdata/tests.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 30 additions & 1 deletion normdata/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions normdata/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit bab60a8

Please sign in to comment.