-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from arthur-schnitzler/23-gnd-import-for-persons
23 gnd import for persons
- Loading branch information
Showing
15 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{ | ||
"python.formatting.provider": "black" | ||
"python.formatting.provider": "black", | ||
"cSpell.enableFiletypes": [ | ||
"!css", | ||
"!python", | ||
"!yaml", | ||
"!yml", | ||
"!html" | ||
], | ||
"isort.check": true, | ||
"isort.importStrategy": "fromEnvironment" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class NormdataConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "normdata" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django import forms | ||
|
||
|
||
class NormDataImportForm(forms.Form): | ||
normdata_url = forms.URLField( | ||
label="Normdata URL", | ||
help_text="Zum Beispiel: http://lobid.org/gnd/118566512 oder https://www.geonames.org/2772400/linz.html", | ||
max_length=100, | ||
) | ||
entity_type = forms.ChoiceField( | ||
label="Entität", | ||
help_text="Wähle die Art der Entität: Person, Ort, ...", | ||
choices=(("person", "Person"), ("place", "Ort")), | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{% extends "base.html" %} | ||
{% load crispy_forms_tags %} | ||
{% load static %} | ||
{% block content %} | ||
<div class="container"> | ||
<h1 class="p-3 text-center">Neue Entität aus GND/WikiData/GeoNames importieren</h1> | ||
<form action="." method="post"> | ||
{% csrf_token %} | ||
{{ form|crispy }} | ||
<input type="submit" value="Importieren"> | ||
</form> | ||
</div> | ||
|
||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.urls import path | ||
|
||
from . import views | ||
|
||
app_name = "normdata" | ||
|
||
|
||
urlpatterns = [ | ||
path( | ||
"import-from-normdata/", | ||
views.NormDataImportFormView.as_view(), | ||
name="import_from_normdata", | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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.core.exceptions import ObjectDoesNotExist | ||
|
||
from apis_core.apis_entities.models import Person, Place | ||
from apis_core.apis_metainfo.models import Uri | ||
from dumper.utils import DOMAIN_MAPPING | ||
|
||
|
||
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: | ||
Uri.objects.create( | ||
uri=get_normalized_uri(wd_entity.geonames_uri), | ||
domain="geonames", | ||
entity=entity, | ||
) | ||
return entity | ||
|
||
|
||
def import_from_normdata(raw_url, entity_type): | ||
normalized_url = get_normalized_uri(raw_url) | ||
try: | ||
entity = Uri.objects.get(uri=normalized_url).entity | ||
return entity | ||
except ObjectDoesNotExist: | ||
pass | ||
domain = get_uri_domain(normalized_url) | ||
if domain == "gnd": | ||
try: | ||
wikidata_url = gnd_to_wikidata(normalized_url)["wikidata"] | ||
except (IndexError, KeyError): | ||
wikidata_url = False | ||
elif domain == "geonames": | ||
try: | ||
wikidata_url = geonames_to_wikidata(normalized_url)["wikidata"] | ||
except (IndexError, KeyError): | ||
wikidata_url = False | ||
elif domain == "wikidata": | ||
wikidata_url = normalized_url | ||
else: | ||
wikidata_url = False | ||
if wikidata_url: | ||
try: | ||
entity = Uri.objects.get(uri=normalized_url).entity | ||
return entity | ||
except ObjectDoesNotExist: | ||
entity = import_from_wikidata(wikidata_url, entity_type) | ||
else: | ||
entity = None | ||
return entity |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.urls import reverse | ||
from django.views.generic.edit import FormView | ||
|
||
from .forms import NormDataImportForm | ||
from .utils import import_from_normdata | ||
|
||
|
||
class NormDataImportFormView(FormView): | ||
template_name = "normdata/create_from_gnd.html" | ||
form_class = NormDataImportForm | ||
|
||
def get_success_url(self): | ||
return reverse("apis:apis_entities:person_list_view") | ||
|
||
def form_valid(self, form): | ||
raw_url = form.data["normdata_url"] | ||
entity_type = form.data["entity_type"] | ||
import_from_normdata(raw_url, entity_type) | ||
return super().form_valid(form) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters