diff --git a/prez/renderers/renderer.py b/prez/renderers/renderer.py index 5a4faf00..e070b283 100644 --- a/prez/renderers/renderer.py +++ b/prez/renderers/renderer.py @@ -7,20 +7,18 @@ from fastapi import status from fastapi.exceptions import HTTPException from fastapi.responses import StreamingResponse -from rdflib import Graph, URIRef, Namespace, RDF +from rdflib import RDF, Graph, Namespace, URIRef from starlette.requests import Request from starlette.responses import Response from prez.models.profiles_and_mediatypes import ProfilesMediatypesInfo from prez.models.profiles_item import ProfileItem from prez.renderers.csv_renderer import render_csv_dropdown -from prez.renderers.json_renderer import render_json_dropdown, NotFoundError +from prez.renderers.json_renderer import NotFoundError, render_json_dropdown from prez.services.curie_functions import get_curie_id_for_uri from prez.sparql.methods import Repo -from prez.sparql.objects_listings import ( - generate_item_construct, - get_annotation_properties, -) +from prez.sparql.objects_listings import (generate_item_construct, + get_annotation_properties) log = logging.getLogger(__name__) @@ -33,7 +31,17 @@ async def return_from_graph( selected_class: URIRef, repo: Repo, ): - profile_headers["Content-Disposition"] = "inline" + iri = graph.value(None, RDF.type, selected_class) + if iri: + filename = get_curie_id_for_uri(URIRef(str(iri))) + elif selected_class: + filename = selected_class.split("#")[-1].split("/")[-1] + else: + filename = None + if filename: + profile_headers["Content-Disposition"] = f"inline; filename={filename}" + else: + profile_headers["Content-Disposition"] = "inline" if str(mediatype) in RDF_MEDIATYPES: return await return_rdf(graph, mediatype, profile_headers) @@ -48,21 +56,13 @@ async def return_from_graph( jsonld_data = await render_json_dropdown(graph, profile, selected_class) if str(mediatype) == "text/csv": - iri = graph.value(None, RDF.type, selected_class) - if iri: - filename = get_curie_id_for_uri(URIRef(str(iri))) - else: - filename = selected_class.split("#")[-1].split("/")[-1] stream = render_csv_dropdown(jsonld_data["@graph"]) - response = StreamingResponse(stream, media_type=mediatype) - response.headers[ - "Content-Disposition" - ] = f"attachment;filename={filename}.csv" + response = StreamingResponse(stream, media_type=mediatype, headers=profile_headers) return response # application/json stream = io.StringIO(json.dumps(jsonld_data)) - return StreamingResponse(stream, media_type=mediatype) + return StreamingResponse(stream, media_type=mediatype, headers=profile_headers) except NotFoundError as err: raise HTTPException(status.HTTP_404_NOT_FOUND, str(err)) @@ -91,7 +91,6 @@ async def return_rdf(graph, mediatype, profile_headers): format=RDF_SERIALIZER_TYPES_MAP[str(mediatype)], encoding="utf-8" ) ) - profile_headers["Content-Disposition"] = "inline" return StreamingResponse(content=obj, media_type=mediatype, headers=profile_headers) diff --git a/prez/routers/vocprez.py b/prez/routers/vocprez.py index 61d56278..4d97df4b 100644 --- a/prez/routers/vocprez.py +++ b/prez/routers/vocprez.py @@ -1,28 +1,23 @@ import logging -from fastapi import APIRouter, Request -from fastapi import Depends +from fastapi import APIRouter, Depends, Request from fastapi.responses import RedirectResponse -from rdflib import URIRef, SKOS +from rdflib import SKOS, URIRef from starlette.responses import PlainTextResponse from prez.bnode import get_bnode_depth from prez.dependencies import get_repo, get_system_repo from prez.models.profiles_and_mediatypes import ProfilesMediatypesInfo -from prez.queries.vocprez import ( - get_concept_scheme_query, - get_concept_scheme_top_concepts_query, - get_concept_narrowers_query, -) -from prez.renderers.renderer import ( - return_from_graph, -) +from prez.queries.vocprez import (get_concept_narrowers_query, + get_concept_scheme_query, + get_concept_scheme_top_concepts_query) +from prez.renderers.renderer import return_from_graph from prez.response import StreamingTurtleAnnotatedResponse from prez.routers.identifier import get_iri_route -from prez.services.objects import object_function -from prez.services.listings import listing_function -from prez.services.link_generation import _add_prez_links from prez.services.curie_functions import get_curie_id_for_uri +from prez.services.link_generation import _add_prez_links +from prez.services.listings import listing_function +from prez.services.objects import object_function from prez.sparql.methods import Repo from prez.sparql.resource import get_resource