Skip to content

Commit

Permalink
HTML redirect to Prez UI (#279)
Browse files Browse the repository at this point in the history
* Enable HTML redirects through PREZ_UI_URL

* Redirect unknown resources to Prez UI /object

* Fix whitespace

* Use /404 for not found
  • Loading branch information
avillar authored Oct 15, 2024
1 parent 39f856b commit a89dc93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions prez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class Settings(BaseSettings):
configuration_mode: bool = False
temporal_predicate: Optional[URIRef] = SDO.temporal
endpoint_to_template_query_filename: Optional[Dict[str, str]] = {}
prez_ui_url: Optional[str] = None

@field_validator("prez_version")
@classmethod
Expand Down
5 changes: 3 additions & 2 deletions prez/services/link_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
log = logging.getLogger(__name__)


async def add_prez_links(graph: Graph, repo: Repo, endpoint_structure):
async def add_prez_links(graph: Graph, repo: Repo, endpoint_structure, uris: list[URIRef] | None = None):
"""
Adds internal links to the given graph for all URIRefs that have a class and endpoint associated with them.
"""
t_start = time.time()
# get all URIRefs - if Prez can find a class and endpoint for them, an internal link will be generated.
uris = [uri for uri in graph.all_nodes() if isinstance(uri, URIRef)]
if uris is None:
uris = [uri for uri in graph.all_nodes() if isinstance(uri, URIRef)]
uri_to_klasses = {}
t = time.time()
# for uri in uris:
Expand Down
15 changes: 14 additions & 1 deletion prez/services/objects.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import io
import json
import logging
import re
import time
import urllib.parse
from urllib.parse import urlencode

from fastapi.responses import PlainTextResponse
from fastapi.responses import PlainTextResponse, RedirectResponse
from rdf2geojson import convert
from rdflib import RDF, URIRef, RDFS
from rdflib.namespace import GEO
Expand Down Expand Up @@ -89,6 +91,17 @@ async def object_function(
query_start_time = time.time()
item_graph, _ = await data_repo.send_queries([query], [])
log.debug(f"Query time: {time.time() - query_start_time}")
if settings.prez_ui_url:
# If HTML or no specific media type requested
if pmts.requested_mediatypes[0][0] in ('text/html', '*/*'):
item_uri = URIRef(profile_nodeshape.focus_node.value)
await add_prez_links(item_graph, data_repo, endpoint_structure, [item_uri])
prez_link = item_graph.value(subject=item_uri, predicate=URIRef("https://prez.dev/link"), any=True)
prez_ui_url = re.sub(r'/+$', '', settings.prez_ui_url)
if prez_link:
return RedirectResponse(prez_ui_url + str(prez_link))
else:
return RedirectResponse(prez_ui_url + '/404?uri=' + urllib.parse.quote_plus(item_uri))
if "anot+" in pmts.selected["mediatype"]:
await add_prez_links(item_graph, data_repo, endpoint_structure)
return await return_from_graph(
Expand Down

0 comments on commit a89dc93

Please sign in to comment.