diff --git a/semantic_kraus_api/main_v1.py b/semantic_kraus_api/main_v1.py index 7448c49..bbdebd9 100644 --- a/semantic_kraus_api/main_v1.py +++ b/semantic_kraus_api/main_v1.py @@ -37,8 +37,7 @@ async def query_entities(search: Search = Depends()): description="Endpoint that allows to retrive a person by id.", ) async def retrieve_person(query: GetEntity = Depends()): - query_dict = asdict(query) - res = get_query_from_triplestore_v2(query_dict, "get_person_v1.sparql") + res = get_query_from_triplestore(query, "get_person_v1.sparql") # res = FakeList(**{"results": flatten_rdf_data(res)}) if len(res) == 0: raise HTTPException(status_code=404, detail="Item not found") diff --git a/semantic_kraus_api/models_v1.py b/semantic_kraus_api/models_v1.py index e5bcb2b..b2c32d0 100644 --- a/semantic_kraus_api/models_v1.py +++ b/semantic_kraus_api/models_v1.py @@ -55,16 +55,12 @@ def __init__(__pydantic_self__, **data: typing.Any) -> None: class ProfessionalOccupation(SemanticKrausBackendBaseModel): """Used to provide professional occupation information""" - id: HttpUrl = Field(..., rdfconfig=FieldConfigurationRDF(path="professionalOccupation", anchor=True)) + id: HttpUrl = Field(..., rdfconfig=FieldConfigurationRDF(path="occ", anchor=True)) label: InternationalizedLabel = Field( - ..., rdfconfig=FieldConfigurationRDF(path="professionalOccupationLabel", default_dict_key="default") - ) - source: str = Field( - ..., - rdfconfig=FieldConfigurationRDF( - path="professionalOccupationSource", callback_function=pp_source_professional_occupation - ), + ..., rdfconfig=FieldConfigurationRDF(path="occupation", default_dict_key="default") ) + time: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="time")) + employer: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="employer")) class Entity(SemanticKrausBackendBaseModel): @@ -87,15 +83,32 @@ def __init__(__pydantic_self__, **data: typing.Any) -> None: super().__init__(**data) +class PersonName(SemanticKrausBackendBaseModel): + id: str = Field(..., rdfconfig=FieldConfigurationRDF(path="appellation", anchor=True)) + label: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="name")) + type: list[str] | None = Field(None, rdfconfig=FieldConfigurationRDF(path="typeLabel")) + + def __init__(__pydantic_self__, **data: typing.Any) -> None: + if type(data["typeLabel"]) == str: + data["typeLabel"] = [data["typeLabel"]] + super().__init__(**data) + + class Person(SemanticKrausBackendBaseModel): id: str = Field( ..., rdfconfig=FieldConfigurationRDF(path="person", anchor=True), ) - label: InternationalizedLabel | None = Field( - None, rdfconfig=FieldConfigurationRDF(path="label", default_dict_key="default") - ) + label: list[PersonName] | None professions: typing.List[ProfessionalOccupation] | None + graph: HttpUrl | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graph_subject")) + graph_label: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graph_subjectLabel")) + SameAs: typing.List[SameAs] | None + + def __init__(__pydantic_self__, **data: typing.Any) -> None: + if "graph_subject" in data: + data["graph_subjectLabel"] = graph_mapping.get(data["graph_subject"], None) + super().__init__(**data) class PaginatedResponseBase(SemanticKrausBackendBaseModel): diff --git a/semantic_kraus_api/sparql/get_person_v1.sparql b/semantic_kraus_api/sparql/get_person_v1.sparql index d97d339..a83f2b3 100644 --- a/semantic_kraus_api/sparql/get_person_v1.sparql +++ b/semantic_kraus_api/sparql/get_person_v1.sparql @@ -5,7 +5,7 @@ PREFIX skos: PREFIX rdfs: PREFIX crm: PREFIX owl: -SELECT ?person ?occupation ?time ?employer ?name ?graph ?type ?typeLabel WHERE { +SELECT ?person ?occupation ?time ?employer ?name ?graph ?type ?typeLabel ?appellation ?occ WHERE { GRAPH ?graph { BIND(<{{id}}> as ?person) ?person crm:P1_is_identified_by ?appellation. diff --git a/semantic_kraus_api/utils.py b/semantic_kraus_api/utils.py index 592cb8a..75e6b87 100644 --- a/semantic_kraus_api/utils.py +++ b/semantic_kraus_api/utils.py @@ -21,7 +21,7 @@ sparql.setCredentials(user=os.environ.get("SPARQL_USER"), passwd=os.environ.get("SPARQL_PASSWORD")) -def get_query_from_triplestore(search: Search | QueryBase , sparql_template: str): +def get_query_from_triplestore(search: Search | QueryBase, sparql_template: str): """creates the query from the template and the search parameters and returns the json from the triplestore. This is v2 and doesnt need the proto config anymore @@ -32,7 +32,7 @@ def get_query_from_triplestore(search: Search | QueryBase , sparql_template: str Returns: _type_: _description_ """ - if isinstance(search, Search): + if type(search).__module__ == "semantic_kraus_api.query_parameters": search = asdict(search) query_template = jinja_env.get_template(sparql_template).render(**search) sparql.setQuery(query_template)