From 29f21a0330a1c1647168f2a9febe2df8ee6ca7fe Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Mon, 13 Jan 2025 11:34:22 +0100 Subject: [PATCH] refactor(templates): restructure templates use inheritance and inclusion --- pfp_api/main.py | 4 ++-- pfp_api/templates/base.rq | 3 +++ pfp_api/templates/{person.j2 => partials/prefixes.rq} | 4 ---- pfp_api/templates/person.rq | 7 +++++++ pfp_api/templates/{persons.j2 => persons.rq} | 5 +++-- 5 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 pfp_api/templates/base.rq rename pfp_api/templates/{person.j2 => partials/prefixes.rq} (67%) create mode 100644 pfp_api/templates/person.rq rename pfp_api/templates/{persons.j2 => persons.rq} (65%) diff --git a/pfp_api/main.py b/pfp_api/main.py index 1302820..ccc854f 100644 --- a/pfp_api/main.py +++ b/pfp_api/main.py @@ -25,7 +25,7 @@ def root(): @app.get("/persons") def persons(query_parameters: Annotated[PersonParams, Query()]) -> Page[Person]: - template = env.get_template("persons.j2") + template = env.get_template("persons.rq") adapter = SPARQLModelAdapter( target="https://pfp-ts-backend.acdh-ch-dev.oeaw.ac.at/", query=template.render(dict(query_parameters)), @@ -36,7 +36,7 @@ def persons(query_parameters: Annotated[PersonParams, Query()]) -> Page[Person]: @app.get("/person/{person_id}") def person(person_id: str): - template = env.get_template("person.j2") + template = env.get_template("person.rq") try: person_id = urlsafe_b64decode(person_id).decode() except binascii.Error: diff --git a/pfp_api/templates/base.rq b/pfp_api/templates/base.rq new file mode 100644 index 0000000..cf2a64b --- /dev/null +++ b/pfp_api/templates/base.rq @@ -0,0 +1,3 @@ +{% include "partials/prefixes.rq" %} +{% block query %} +{% endblock query %} diff --git a/pfp_api/templates/person.j2 b/pfp_api/templates/partials/prefixes.rq similarity index 67% rename from pfp_api/templates/person.j2 rename to pfp_api/templates/partials/prefixes.rq index 2d962b5..7202632 100644 --- a/pfp_api/templates/person.j2 +++ b/pfp_api/templates/partials/prefixes.rq @@ -1,7 +1,3 @@ PREFIX crm: PREFIX rdfs: PREFIX pfp: -SELECT ?subject -WHERE { - ?subject pfp:proxy_for <{{ person_id }}> . -} diff --git a/pfp_api/templates/person.rq b/pfp_api/templates/person.rq new file mode 100644 index 0000000..aa45857 --- /dev/null +++ b/pfp_api/templates/person.rq @@ -0,0 +1,7 @@ +{% extends "base.rq" %} +{% block query %} +SELECT ?subject +WHERE { + ?subject pfp:proxy_for <{{ person_id }}> . +} +{% endblock query %} diff --git a/pfp_api/templates/persons.j2 b/pfp_api/templates/persons.rq similarity index 65% rename from pfp_api/templates/persons.j2 rename to pfp_api/templates/persons.rq index 4b5bc6c..30187b4 100644 --- a/pfp_api/templates/persons.j2 +++ b/pfp_api/templates/persons.rq @@ -1,5 +1,5 @@ -PREFIX crm: -PREFIX rdfs: +{% extends "base.rq" %} +{% block query %} SELECT ?person ?label ?graph WHERE { GRAPH ?graph { @@ -10,3 +10,4 @@ WHERE { {% endif %} } } +{% endblock query %}