diff --git a/pfp_api/main.py b/pfp_api/main.py index aaca18c..8a7d6f2 100644 --- a/pfp_api/main.py +++ b/pfp_api/main.py @@ -1,13 +1,14 @@ +from jinja2 import Environment, PackageLoader from typing import Annotated from fastapi import FastAPI, Query, Response, status from rdfproxy import Page, SPARQLModelAdapter, QueryParameters from pfp_api.models import Person -from pfp_api.queries import QueryBuilder app = FastAPI() +env = Environment(loader=PackageLoader("pfp_api")) class PersonParams(QueryParameters): @@ -21,9 +22,10 @@ def root(): @app.get("/persons") def persons(query_parameters: Annotated[PersonParams, Query()]) -> Page[Person]: + template = env.get_template("persons.j2") adapter = SPARQLModelAdapter( target="https://pfp-ts-backend.acdh-ch-dev.oeaw.ac.at/", - query=str(QueryBuilder("person.rq")), + query=template.render(dict(query_parameters)), model=Person, ) return adapter.query(query_parameters) diff --git a/pfp_api/queries/__init__.py b/pfp_api/queries/__init__.py deleted file mode 100644 index 80b6d76..0000000 --- a/pfp_api/queries/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -from pathlib import Path - - -class QueryBuilder: - filename: Path = None - - def __init__(self, filename: str): - self.filename = Path(__file__).parent / filename - - def __str__(self): - return self.filename.read_text() diff --git a/pfp_api/queries/person.rq b/pfp_api/queries/person.rq deleted file mode 100644 index 12c5ef6..0000000 --- a/pfp_api/queries/person.rq +++ /dev/null @@ -1,22 +0,0 @@ -PREFIX crm: -PREFIX owl2: -PREFIX owl: -PREFIX rdfs: - -SELECT ?person ?person2 ?label ?label2 ?g ?g2 ?uri -WHERE { - GRAPH ?g { - ?person a crm:E21_Person ; - owl2:sameAs|owl:sameAs ?uri . - OPTIONAL { ?person rdfs:label ?label } - } - - GRAPH ?g2 { - ?person2 a crm:E21_Person ; - owl2:sameAs|owl:sameAs ?uri . - OPTIONAL { ?person2 rdfs:label ?label2 } - } - - FILTER(?g != ?g2) - FILTER(STR(?g) < STR(?g2)) -} diff --git a/pfp_api/templates/persons.j2 b/pfp_api/templates/persons.j2 new file mode 100644 index 0000000..7d3cf90 --- /dev/null +++ b/pfp_api/templates/persons.j2 @@ -0,0 +1,10 @@ +PREFIX crm: +PREFIX rdfs: +SELECT ?person ?label +WHERE { +?person a crm:E21_Person ; + rdfs:label ?label . + {% if label %} + FILTER CONTAINS(STR(?label), "{{ label }}") + {% endif %} +} diff --git a/pyproject.toml b/pyproject.toml index a129be5..430eb8e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ readme = "README.md" requires-python = ">=3.11" dependencies = [ "fastapi[standard]>=0.115.6", + "jinja2>=3.1.5", "rdfproxy>=0.2.0", ] diff --git a/uv.lock b/uv.lock index 3d8019e..c1e8a75 100644 --- a/uv.lock +++ b/uv.lock @@ -281,6 +281,7 @@ version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "fastapi", extra = ["standard"] }, + { name = "jinja2" }, { name = "rdfproxy" }, ] @@ -292,6 +293,7 @@ dev = [ [package.metadata] requires-dist = [ { name = "fastapi", extras = ["standard"], specifier = ">=0.115.6" }, + { name = "jinja2", specifier = ">=3.1.5" }, { name = "rdfproxy", specifier = ">=0.2.0" }, ]