Skip to content

Commit a3fb2b2

Browse files
committed
when browsing in browser, get non-html as html
- replace `trove.render._rendering` module with `trove.render.rendering` package (separate file for each rendering class) - make `ProtoRendering` an actual `typing.Protocol` and narrow types to only unicode `str` - add `trove.render.rendering.html_wrapped.HtmlWrappedRendering` that puts minimal `<!DOCTYPE html><pre>...</pre>` around an inner rendering - use `HtmlWrappedRendering` to wrap responses that aren't html or json, when `Accept` header allows html and query params omit `withFileName` - use mediatype constants more consistently (leaving off charset except for content-type header)
1 parent 48bb99d commit a3fb2b2

24 files changed

+174
-96
lines changed

tests/trove/render/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from trove.trovesearch.trovesearch_gathering import trovesearch_by_indexstrategy
99
from trove.render._base import BaseRenderer
10-
from trove.render._rendering import ProtoRendering
10+
from trove.render.rendering import ProtoRendering
1111
from trove.vocab.namespaces import RDF
1212
from tests.trove._input_output_tests import BasicInputOutputTestCase
1313
from ._inputs import UNRENDERED_RDF, UNRENDERED_SEARCH_RDF, RdfCase
@@ -66,9 +66,9 @@ def assert_outputs_equal(self, expected_output, actual_output) -> None:
6666
self._get_rendered_output(actual_output),
6767
)
6868

69-
def _get_rendered_output(self, rendering: ProtoRendering):
69+
def _get_rendered_output(self, rendering: ProtoRendering) -> str:
7070
# for now, they always iter strings (update if/when bytes are in play)
71-
return ''.join(rendering.iter_content()) # type: ignore[arg-type]
71+
return ''.join(map(str, rendering.iter_content()))
7272

7373

7474
class TrovesearchRendererTests(TroveRendererTests):

tests/trove/render/test_jsonapi_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest import mock
33

44
from trove.render.jsonapi import RdfJsonapiRenderer
5-
from trove.render._rendering import SimpleRendering
5+
from trove.render.rendering import SimpleRendering
66
from trove.vocab.namespaces import BLARG
77
from . import _base
88

tests/trove/render/test_jsonld_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

33
from trove.render.jsonld import RdfJsonldRenderer
4-
from trove.render._rendering import SimpleRendering
4+
from trove.render.rendering import SimpleRendering
55
from ._inputs import BLARG
66
from . import _base
77

tests/trove/render/test_simple_csv_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from trove.render.simple_csv import TrovesearchSimpleCsvRenderer
2-
from trove.render._rendering import SimpleRendering
2+
from trove.render.rendering import SimpleRendering
33
from . import _base
44

55

tests/trove/render/test_simple_json_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

33
from trove.render.simple_json import TrovesearchSimpleJsonRenderer
4-
from trove.render._rendering import SimpleRendering
4+
from trove.render.rendering import SimpleRendering
55
from trove.vocab.namespaces import BLARG
66
from . import _base
77

tests/trove/render/test_simple_tsv_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from trove.render.simple_tsv import TrovesearchSimpleTsvRenderer
2-
from trove.render._rendering import SimpleRendering
2+
from trove.render.rendering import SimpleRendering
33
from . import _base
44

55

tests/trove/render/test_turtle_renderer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from primitive_metadata import primitive_rdf as rdf
22

33
from trove.render.turtle import RdfTurtleRenderer
4-
from trove.render._rendering import SimpleRendering
4+
from trove.render.rendering import SimpleRendering
55
from . import _base
66

77

tests/trove/test_doctest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import trove.util.frozen
55
import trove.util.iris
66
import trove.util.propertypath
7+
import trove.vocab.mediatypes
78

89
_DOCTEST_OPTIONFLAGS = (
910
doctest.ELLIPSIS
@@ -15,6 +16,7 @@
1516
trove.util.frozen,
1617
trove.util.iris,
1718
trove.util.propertypath,
19+
trove.vocab.mediatypes,
1820
)
1921

2022

trove/render/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import Type
2-
31
from django import http
42

53
from trove import exceptions as trove_exceptions
4+
from trove.vocab.mediatypes import strip_mediatype_parameters
65
from ._base import BaseRenderer
76
from .jsonapi import RdfJsonapiRenderer
87
from .html_browse import RdfHtmlBrowseRenderer
@@ -25,10 +24,6 @@
2524
TrovesearchSimpleTsvRenderer,
2625
)
2726

28-
RendersType = Type[
29-
BaseRenderer | RdfHtmlBrowseRenderer | RdfJsonapiRenderer | RdfTurtleRenderer | RdfJsonldRenderer | TrovesearchSimpleCsvRenderer | TrovesearchSimpleJsonRenderer | TrovesearchSimpleTsvRenderer
30-
]
31-
3227
RENDERER_BY_MEDIATYPE = {
3328
_renderer_type.MEDIATYPE: _renderer_type
3429
for _renderer_type in RENDERERS
@@ -42,7 +37,9 @@ def get_renderer_type(request: http.HttpRequest) -> type[BaseRenderer]:
4237
_requested_mediatype = request.GET.get('acceptMediatype')
4338
if _requested_mediatype:
4439
try:
45-
_chosen_renderer_type = RENDERER_BY_MEDIATYPE[_requested_mediatype]
40+
_chosen_renderer_type = RENDERER_BY_MEDIATYPE[
41+
strip_mediatype_parameters(_requested_mediatype)
42+
]
4643
except KeyError:
4744
raise trove_exceptions.CannotRenderMediatype(_requested_mediatype)
4845
else:

trove/render/_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from trove.vocab import mediatypes
1414
from trove.vocab.trove import TROVE_API_THESAURUS
1515
from trove.vocab.namespaces import namespaces_shorthand
16-
from ._rendering import ProtoRendering, SimpleRendering
16+
from .rendering import ProtoRendering, SimpleRendering
1717

1818

1919
@dataclasses.dataclass
@@ -61,15 +61,15 @@ def render_document(self) -> ProtoRendering:
6161
except NotImplementedError:
6262
raise NotImplementedError(f'class "{type(self)}" must implement either `render_document` or `simple_render_document`')
6363
else:
64-
return SimpleRendering( # type: ignore[return-value] # until ProtoRendering(typing.Protocol) with py3.12
64+
return SimpleRendering(
6565
mediatype=self.MEDIATYPE,
6666
rendered_content=_content,
6767
)
6868

6969
@classmethod
7070
def render_error_document(cls, error: trove_exceptions.TroveError) -> ProtoRendering:
7171
# may override, but default to jsonapi
72-
return SimpleRendering( # type: ignore[return-value] # until ProtoRendering(typing.Protocol) with py3.12
72+
return SimpleRendering(
7373
mediatype=mediatypes.JSONAPI,
7474
rendered_content=json.dumps(
7575
{'errors': [{ # https://jsonapi.org/format/#error-objects

0 commit comments

Comments
 (0)