-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/type_graphics
- Loading branch information
Showing
18 changed files
with
479 additions
and
40 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Upgrade 7.14.x to 7.15.0 | ||
-- Be sure to backup the database and read the upgrade notes before executing. | ||
|
||
BEGIN; | ||
|
||
-- Raise database version | ||
UPDATE web.settings SET value = '7.15.0' WHERE name = 'database_version'; | ||
|
||
-- (#1991) Import controlled vocabularies via API | ||
INSERT INTO web.settings (name, value) VALUES | ||
('vocabs_base_url', 'https://vocabs.acdh.oeaw.ac.at/'), | ||
('vocabs_endpoint', 'rest/v1/'), | ||
('vocabs_user', ''); | ||
|
||
END; |
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import Any, Optional | ||
|
||
import requests | ||
from flask import g | ||
|
||
from openatlas import app | ||
from openatlas.models.entity import Entity | ||
from openatlas.models.type import Type | ||
|
||
|
||
def get_or_create_type(hierarchy: Entity, type_name: str) -> Entity: | ||
if type_ := get_type_by_name(type_name): | ||
if type_.root[0] == hierarchy.id: | ||
return type_ | ||
type_entity = Entity.insert('type', type_name) | ||
type_entity.link('P127', hierarchy) | ||
return type_entity | ||
|
||
|
||
def get_type_by_name(type_name: str) -> Optional[Type]: | ||
type_ = None | ||
for type_id in g.types: | ||
if g.types[type_id].name == type_name: | ||
type_ = g.types[type_id] | ||
return type_ | ||
|
||
|
||
def get_exact_match() -> Entity: | ||
return get_or_create_type( | ||
Type.get_hierarchy('External reference match'), 'exact match') | ||
|
||
|
||
def get_reference_system(name: str) -> Entity: | ||
return [i for i in g.reference_systems.values() if i.name == name][0] | ||
|
||
|
||
def vocabs_requests( | ||
id_: Optional[str] = '', | ||
endpoint: Optional[str] = '', | ||
parameter: Optional[dict[str, str]] = '') -> dict[str, Any]: | ||
req = requests.get( | ||
f"{g.settings['vocabs_base_url']}{g.settings['vocabs_endpoint']}{id_}/" | ||
f"{endpoint}", | ||
params=parameter, | ||
timeout=60, | ||
auth=(app.config['VOCABS_USER'], app.config['VOCABS_PW'])) | ||
return req.json() |
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,128 @@ | ||
from typing import Any, Optional | ||
|
||
from flask import g | ||
|
||
from openatlas.api.import_scripts.util import get_exact_match, vocabs_requests | ||
from openatlas.database.reference_system import ReferenceSystem as Db | ||
from openatlas.models.entity import Entity | ||
from openatlas.models.reference_system import ReferenceSystem | ||
from openatlas.models.type import Type | ||
|
||
|
||
def import_vocabs_data( | ||
id_: str, | ||
form_data: dict[str, Any], | ||
details: dict[str, Any]) -> tuple[list, list]: | ||
return fetch_top_level(id_, details, form_data) | ||
|
||
|
||
def fetch_top_level( | ||
id_: str, | ||
details: dict[str, Any], | ||
form_data: dict[str, Any]) -> tuple[list, list]: | ||
req = vocabs_requests(id_, 'topConcepts', {'lang': form_data['language']}) | ||
count = [] | ||
duplicates = [] | ||
if ref := get_vocabs_reference_system(details): | ||
for entry in req['topconcepts']: | ||
if entry['uri'] in form_data['top_concepts'] \ | ||
and not Type.check_hierarchy_exists(entry['label']): | ||
hierarchy = Entity.insert( | ||
'type', | ||
entry['label'], | ||
f'Automatically imported from {details["title"]}') | ||
Type.insert_hierarchy( | ||
hierarchy, | ||
'custom', form_data['classes'], | ||
form_data['multiple']) | ||
entry['subs'] = import_children( | ||
entry['uri'], | ||
id_, | ||
form_data['language'], | ||
ref, | ||
hierarchy) | ||
count.append(entry) | ||
if Type.check_hierarchy_exists(entry['label']): | ||
duplicates.append(entry) | ||
return count, duplicates | ||
|
||
|
||
def import_children( | ||
uri: str, | ||
id_: str, | ||
lang: str, | ||
ref: ReferenceSystem, | ||
super_: Optional[Entity], ) -> list[dict[str, Any]]: | ||
req = vocabs_requests(id_, 'narrower', {'uri': uri, 'lang': lang}) | ||
exact_match_id = get_exact_match().id | ||
children = [] | ||
child = None | ||
for entry in req['narrower']: | ||
if not entry['prefLabel']: | ||
g.logger.log( | ||
'warn', | ||
'import', | ||
f'{entry["uri"]} has no prefLabel assigned to it') | ||
continue | ||
name = entry['uri'].rsplit('/', 1)[-1] | ||
if super_: | ||
child = Entity.insert( | ||
'type', | ||
entry['prefLabel'] # Switch if bug is solved | ||
# get_pref_label(entry['prefLabel'], id_, entry['uri']) | ||
) | ||
child.link('P127', super_) | ||
ref.link('P67', child, name, type_id=exact_match_id) | ||
entry['subs'] = import_children(entry['uri'], id_, lang, ref, child) | ||
children.append(entry) | ||
return children | ||
|
||
|
||
# Skosmos API has a problem, this code will work if bug is closed | ||
# | ||
# def get_pref_label(label: str, id_: str, uri: str) -> str: | ||
# if not label: | ||
# req = vocabs_requests(id_, 'label', {'uri': uri}) | ||
# label = req['prefLabel'] | ||
# return label | ||
|
||
|
||
def get_vocabs_reference_system(details: dict[str, Any], ) -> ReferenceSystem: | ||
title = details['title'] | ||
system = None | ||
for system_ in g.reference_systems.values(): | ||
if system_.name == f'{title} vocabulary': | ||
system = system_ | ||
if not system: | ||
system = ReferenceSystem.insert_system({ | ||
'name': f'{title} vocabulary', | ||
'description': f'Import of {title} vocabulary (autogenerated)', | ||
'website_url': g.settings['vocabs_base_url'], | ||
'resolver_url': f"{details['conceptUri'].rsplit('/', 1)[0]}/"}) | ||
Db.add_classes(system.id, ['type']) | ||
return system | ||
|
||
|
||
def get_vocabularies() -> list[dict[str, Any]]: | ||
req = vocabs_requests(endpoint='vocabularies', parameter={'lang': 'en'}) | ||
out = [] | ||
for voc in req['vocabularies']: | ||
out.append(voc | fetch_vocabulary_details(voc['uri'])) | ||
return out | ||
|
||
|
||
def fetch_vocabulary_details(id_: str) -> dict[str, str]: | ||
data = vocabs_requests(id_, parameter={'lang': 'en'}) | ||
return { | ||
'id': data['id'], | ||
'title': data['title'], | ||
'defaultLanguage': data['defaultLanguage'], | ||
'languages': data['languages'], | ||
'conceptUri': | ||
data['conceptschemes'][0]['uri'] if data['conceptschemes'] else ''} | ||
|
||
|
||
def fetch_top_concept_details(id_: str) -> list[tuple]: | ||
req = vocabs_requests(id_, 'topConcepts', parameter={'lang': 'en'}) | ||
return [ | ||
(concept['uri'], concept['label']) for concept in req['topconcepts']] |
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
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
Oops, something went wrong.