rest_framework_dso.renderers module

The output rendering for the DSO library.

This makes sure the response gets the desired application/hal+json

class rest_framework_dso.renderers.BrowsableAPIRenderer

Bases: RendererMixin, BrowsableAPIRenderer

get_breadcrumbs(request)

Given a request returns a list of breadcrumbs, which are each a tuple of (name, url).

Extends the buildin function by using instance title from _links for the last breadcrumb instead of the generic “Instance”.

get_content(renderer, data, accepted_media_type, renderer_context)

Fix showing generator content for browsable API, convert back to one string

get_context(data, accepted_media_type, renderer_context)

Returns the context used to render.

render(data, accepted_media_type=None, renderer_context=None)

Render the HTML for the browsable API representation.

template = 'dso_api/dynamic_api/api.html'
class rest_framework_dso.renderers.CSVRenderer

Bases: RendererMixin, CSVStreamingRenderer

Overwritten CSV renderer to provide proper headers.

In the view methods (e.g. get_renderer_context()), the serializer layout is not accessible. Hence the header is reformatted within a custom output renderer.

compatible_paginator_classes = [<class 'rest_framework_dso.pagination.DSOHTTPHeaderPageNumberPagination'>]
content_disposition: Optional[str] = 'attachment; filename="{filename}.csv"'
render(data, media_type=None, renderer_context=None)

Renders serialized data into CSV to be used with Django StreamingHttpResponse. We need to return a generator here, so Django can iterate over it, rendering and returning each line.

>>> renderer = CSVStreamingRenderer()
>>> renderer.header = ['a', 'b']
>>> data = [{'a': 1, 'b': 2}]
>>> from django.http import StreamingHttpResponse
>>> response = StreamingHttpResponse(renderer.render(data),
                                     content_type='text/csv')
>>> response['Content-Disposition'] = 'attachment; filename="f.csv"'
>>> # return response
render_exception(exception: Exception)

Inform clients that the stream was interrupted by an exception. The actual exception is still raised and logged. Purposefully little information is given to the client.

supports_detail_embeds = True
supports_inline_embeds = True
supports_list_embeds = False
supports_m2m = False
tune_serializer(serializer: Serializer)

Allow to fine-tune the serializer (e.g. remove unused fields). This hook is used so the ‘fields’ are properly set before the peek_iterable() is called - as that would return the first item as original.

unlimited_page_size = True
class rest_framework_dso.renderers.GeoJSONRenderer

Bases: RendererMixin, JSONRenderer

Convert the output into GeoJSON notation.

charset = 'utf-8'
compatible_paginator_classes = [<class 'rest_framework_dso.pagination.DelegatedPageNumberPagination'>]
content_disposition: Optional[str] = 'attachment; filename="{filename}.json"'
default_crs = CRS(domain='ogc', authority='EPSG', version='', crsid='4326', srid=4326, backend=None, origin=4326, has_custom_backend=False)
format = 'geojson'
media_type = 'application/geo+json'
render(data, accepted_media_type=None, renderer_context=None)

Render data into JSON, returning a bytestring.

supports_detail_embeds = False
tune_serializer(serializer: Serializer)

Remove unused fields from the serializer:

unlimited_page_size = True
class rest_framework_dso.renderers.HALJSONRenderer

Bases: RendererMixin, JSONRenderer

compatible_paginator_classes = [<class 'rest_framework_dso.pagination.DSOPageNumberPagination'>]
media_type = 'application/hal+json'
render(data, accepted_media_type=None, renderer_context=None)

Render the data as streaming.

class rest_framework_dso.renderers.RendererMixin

Bases: object

Extra attributes on renderers in this project.

chunk_size = 4096
compatible_paginator_classes = None
content_disposition: Optional[str] = None
default_crs = None
finalize_response(response, renderer_context: dict)

Make final adjustments to the response.

get_http_headers(renderer_context: dict)

Return the http headers for the response.

paginator = None
render_exception(exception)

Inform the client that the stream processing was interrupted with an exception. The exception can be rendered in the format fits with the output.

Purposefully, not much information is given, so avoid informing clients. The actual exception is still raised and logged server-side.

setup_pagination(paginator: DelegatedPageNumberPagination)

Used by DelegatedPageNumberPagination

supports_detail_embeds = True
supports_inline_embeds = False
supports_list_embeds = True
supports_m2m = True
tune_serializer(serializer: Serializer)

Allow to fine-tune the serializer (e.g. remove unused fields). This hook is used so the ‘fields’ are properly set before the peek_iterable() is called - as that would return the first item as original.

unlimited_page_size = False
rest_framework_dso.renderers.get_data_serializer(data) Optional[Serializer]

Find the serializer associated with the incoming ‘data’