Skip to content

Commit

Permalink
get own endpoint for flexibility reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Feb 4, 2025
1 parent 735ba89 commit 30dd3b9
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 48 deletions.
15 changes: 5 additions & 10 deletions openatlas/api/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
from openatlas import app
from openatlas.api.endpoints.parser import Parser
from openatlas.api.formats.csv import (
build_dataframe_with_relations, build_dataframe, build_link_dataframe)
build_dataframe,
build_dataframe_with_relations,
build_link_dataframe)
from openatlas.api.formats.loud import get_loud_entities
from openatlas.api.resources.resolve_endpoints import (
download, parse_loud_context)
from openatlas.api.resources.templates import (
geojson_collection_template, geojson_pagination, linked_place_pagination,
linked_places_template, loud_pagination, loud_template,
presentation_template)
from openatlas.api.resources.util import get_location_link
linked_places_template, loud_pagination, loud_template)
from openatlas.api.resources.util import get_location_link
from openatlas.models.entity import Entity, Link


Expand Down Expand Up @@ -224,10 +225,6 @@ def get_entities_formatted(self) -> None:
entities = [
self.parser.get_linked_places_entity(item)
for item in self.entities_with_links.values()]
case 'presentation':
entities = [
self.parser.get_presentation_view(item)
for item in self.entities_with_links.values()]
case _ if self.parser.format \
in app.config['RDF_FORMATS']: # pragma: no cover
parsed_context = parse_loud_context()
Expand Down Expand Up @@ -281,8 +278,6 @@ def get_entities_template(self, result: dict[str, Any]) -> dict[str, Any]:
template = loud_template(result)
if not self.single:
template = loud_pagination()
case 'presentation':
template = presentation_template(result)
case 'lp' | 'lpx' | _:
template = linked_places_template(self.parser)
if not self.single:
Expand Down
24 changes: 18 additions & 6 deletions openatlas/api/endpoints/entities.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Any

from flask import Response, g
from flask_restful import Resource
from flask_restful import Resource, marshal

from openatlas.api.endpoints.endpoint import Endpoint
from openatlas.api.formats.presentation_view import get_presentation_view
from openatlas.api.resources.api_entity import ApiEntity
from openatlas.api.resources.error import (
InvalidLimitError, NotATypeError, QueryEmptyError)
from openatlas.api.resources.parser import entity_, properties, query
from openatlas.api.resources.templates import presentation_template
from openatlas.api.resources.util import (
get_entities_from_type_with_subs, get_entities_linked_to_special_type,
get_entities_linked_to_special_type_recursive, get_linked_entities_api)
Expand All @@ -20,6 +22,7 @@ def get(class_: str) -> tuple[Resource, int] | Response | dict[str, Any]:
ApiEntity.get_by_cidoc_classes([class_]),
entity_.parse_args()).resolve_entities()


class GetBySystemClass(Resource):
@staticmethod
def get(class_: str) -> tuple[Resource, int] | Response | dict[str, Any]:
Expand All @@ -44,6 +47,15 @@ def get(id_: int) -> tuple[Resource, int] | Response | dict[str, Any]:
entity_.parse_args()).resolve_entities()


class GetEntityPresentationView(Resource):
@staticmethod
def get(id_: int) -> tuple[Resource, int] | Response | dict[str, Any]:
result = get_presentation_view(
ApiEntity.get_by_id(id_, types=True, aliases=True),
entity_.parse_args())
return marshal(result, presentation_template(result))


class GetLinkedEntitiesByPropertyRecursive(Resource):
@staticmethod
def get(id_: int) -> Response | dict[str, Any]:
Expand Down Expand Up @@ -105,11 +117,11 @@ class GetQuery(Resource):
def get() -> tuple[Resource, int] | Response | dict[str, Any]:
parser = query.parse_args()
if not any([
parser['entities'],
parser['cidoc_classes'],
parser['view_classes'],
parser['system_classes'],
parser['linked_entities']]):
parser['entities'],
parser['cidoc_classes'],
parser['view_classes'],
parser['system_classes'],
parser['linked_entities']]):
raise QueryEmptyError
entities = []
if parser['entities']:
Expand Down
22 changes: 0 additions & 22 deletions openatlas/api/endpoints/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
from openatlas import app
from openatlas.api.formats.linked_places import (
get_lp_file, get_lp_links, get_lp_time)
from openatlas.api.formats.presentation_view import get_presentation_files, \
get_presentation_types
from openatlas.api.resources.error import (
EntityDoesNotExistError, InvalidSearchSyntax, InvalidSearchValueError,
LastEntityError, UrlNotValid)
Expand Down Expand Up @@ -190,26 +188,6 @@ def get_geom(self, entity: Entity, ) -> list[Any]:
return geoms
return []

def get_presentation_view(
self,
entity_dict: dict[str, Any]) -> dict[str, Any]:
entity = entity_dict['entity']
links = entity_dict['links']
links_inverse = entity_dict['links_inverse']

data = {
"id": entity.id,
"systemClass": entity.class_.name,
"title": entity.name,
"description": entity.description,
"aliases": list(entity.aliases.values()),
"geometries": get_geometric_collection(entity, links, self),
"when": get_lp_time(entity),
"types": get_presentation_types(entity, links),
"externalReferenceSystems": get_reference_systems(links_inverse),
"files": get_presentation_files(links_inverse)
}
return data

def get_linked_places_entity(
self,
Expand Down
3 changes: 1 addition & 2 deletions openatlas/api/formats/linked_places.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
date_to_str, get_crm_relation, get_crm_relation_label_x,
get_crm_relation_x, get_iiif_manifest_and_path, get_license_name,
to_camel_case)
from openatlas.display.util import (
check_iiif_activation, check_iiif_file_exist, get_file_path)
from openatlas.display.util import get_file_path
from openatlas.models.entity import Entity, Link

if TYPE_CHECKING: # pragma: no cover
Expand Down
61 changes: 59 additions & 2 deletions openatlas/api/formats/presentation_view.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import mimetypes
from collections import defaultdict
from typing import Any

from flask import g, url_for

from openatlas.api.resources.util import get_iiif_manifest_and_path, \
from openatlas.api.endpoints.parser import Parser
from openatlas.api.formats.linked_places import get_lp_time
from openatlas.api.resources.util import get_geometric_collection, \
get_iiif_manifest_and_path, \
get_license_name, get_location_link, \
get_value_for_types
get_reference_systems, get_value_for_types
from openatlas.display.util import get_file_path
from openatlas.models.entity import Entity, Link

Expand Down Expand Up @@ -58,3 +62,56 @@ def get_presentation_files(links_inverse: list[Link]) -> list[dict[str, str]]:
data.update(get_iiif_manifest_and_path(img_id))
files.append(data)
return files


def get_presentation_view(entity: Entity, parser: Parser) -> dict[str, Any]:

links = Entity.get_links_of_entities(entity.id)
links_inverse= Entity.get_links_of_entities(entity.id, inverse=True)

data = {
"id": entity.id,
"systemClass": entity.class_.name,
"title": entity.name,
"description": entity.description,
"aliases": list(entity.aliases.values()),
"geometries": get_geometric_collection(entity, links, parser),
"when": get_lp_time(entity),
"types": get_presentation_types(entity, links),
"externalReferenceSystems": get_reference_systems(links_inverse),
"files": get_presentation_files(links_inverse)}
relations = defaultdict(list)
excluded= ['type', 'object_location', 'file', 'appellation', 'reference_system']
for l in links:
if l.range.class_.name in excluded:
continue
relations[l.range.class_.name].append({
"id": l.range.id,
"systemClass": l.range.class_.name,
"title": l.range.name,
"description": l.range.description,
"aliases": list(l.range.aliases.values()),
"geometries": get_geometric_collection(l.range, links, parser),
"when": get_lp_time(l.range),
"standardType": {
'id': l.range.standard_type.id,
'title': l.range.standard_type.name},
} if l.range.standard_type else {})
for l in links_inverse:
if l.range.class_.name in excluded:
continue
relations[l.domain.class_.name].append({
"id": l.domain.id,
"systemClass": l.domain.class_.name,
"title": l.domain.name,
"description": l.domain.description,
"aliases": list(l.domain.aliases.values()),
"geometries": get_geometric_collection(l.domain, links, parser),
"when": get_lp_time(l.domain),
"standardType": {
'id': l.domain.standard_type.id ,
'title': l.domain.standard_type.name},
}if l.domain.standard_type else {})
data.update({'relations': relations})

return data
2 changes: 2 additions & 0 deletions openatlas/api/resources/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,14 @@ def loud_template(result: dict[str, Any]) -> dict[str, Any]:
template[item] = fields.Raw
return template


def presentation_template(result: dict[str, Any]) -> dict[str, Any]:
template = {}
for item in result:
template[item] = fields.Raw
return template


def subunit_template(id_: str) -> dict[str, List]:
timespan = {
'earliestBegin': fields.String,
Expand Down
8 changes: 4 additions & 4 deletions openatlas/api/resources/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def replace_empty_list_values_in_dict_with_none(

def get_linked_entities_api(id_: int | list[int]) -> list[Entity]:
domain = [link_.range for link_ in Entity.get_links_of_entities(id_)]
range_ = [
link_.domain for link_ in
Entity.get_links_of_entities(id_, inverse=True)]
return [*range_, *domain]
range_links = Entity.get_links_of_entities(id_, inverse=True)
range_ = [link_.domain for link_ in range_links]
entity = range_links[0].range
return [entity, *range_, *domain]


def get_linked_entities_id_api(id_: int) -> list[Entity]:
Expand Down
7 changes: 5 additions & 2 deletions openatlas/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
DisplayImage, LicensedFileOverview)
from openatlas.api.endpoints.entities import (
GetByCidocClass, GetBySystemClass, GetByViewClass,
GetEntitiesLinkedToEntity, GetEntity, GetLatest,
GetEntitiesLinkedToEntity, GetEntity, GetEntityPresentationView, GetLatest,
GetLinkedEntitiesByPropertyRecursive, GetQuery, GetTypeEntities,
GetTypeEntitiesAll)
from openatlas.api.endpoints.iiif import (
Expand All @@ -30,7 +30,10 @@
'linked_entities_by_properties_recursive'],
[GetEntitiesLinkedToEntity,
'/entities_linked_to_entity/<int:id_>',
'entities_linked_to_entity']]
'entities_linked_to_entity'],
[GetEntityPresentationView,
'/entity_presentation_view/<int:id_>',
'entity_presentation_view']]

admin = [
[SystemClassCount, '/system_class_count/', 'system_class_count'],
Expand Down

0 comments on commit 30dd3b9

Please sign in to comment.