Skip to content

Commit

Permalink
feat: replace custom querybuilder with jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 9, 2025
1 parent 7dabcf1 commit 50f34ff
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 35 deletions.
6 changes: 4 additions & 2 deletions pfp_api/main.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
11 changes: 0 additions & 11 deletions pfp_api/queries/__init__.py

This file was deleted.

22 changes: 0 additions & 22 deletions pfp_api/queries/person.rq

This file was deleted.

10 changes: 10 additions & 0 deletions pfp_api/templates/persons.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?label
WHERE {
?person a crm:E21_Person ;
rdfs:label ?label .
{% if label %}
FILTER CONTAINS(STR(?label), "{{ label }}")
{% endif %}
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
2 changes: 2 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 50f34ff

Please sign in to comment.