Renderer

class pyldapi.Renderer(request, instance_uri, profiles, default_profile_token)[source]

Abstract class as a parent for classes that validate the profiles & mediatypes for an API-delivered resource (typically either registers or objects) and also creates an ‘alternates profile’ for them, based on all available profiles & mediatypes.

__init__(request, instance_uri, profiles, default_profile_token)[source]

Constructor

Parameters:
  • request (flask.request) – Flask request object that triggered this class object’s creation.
  • instance_uri (str) – The URI that triggered this API endpoint (can be via redirects but the main URI is needed).
  • profiles (dict (of View class objects)) – A dictionary of profiles available for this resource.
  • default_profile_token – The ID of the default profile (key of a profile in the dictionary of :class:

.Profile objects) :type default_profile_token: str (a key in profiles) :param alternates_template: The Jinja2 template to use for rendering the HTML alternates view. If None, then it will default to try and use a template called alternates.html. :type alternates_template: str

See also

See the View class on how to create a dictionary of profiles.

render(alt_template: str = 'alt.html', additional_alt_template_context=None, alt_template_context_replace=False)[source]

Use the received profile and mediatype to create a response back to the client.

TODO: Ashley, are you able to update this description with your new changes please? What is the method for rendering other profiles now? - Edmond

This is an abstract method.

Note

The pyldapi.Renderer.render requires you to implement your own business logic to render

custom responses back to the client using flask.render_template() or flask.Response object.

Example Implementation of pyldapi.Renderer.render()

# context: a custom Renderer class which inherits from pyldapi.Renderer

def render(self):
    if self.site_no is None:
        return Response('Site {} not found.'.format(self.site_no), status=404, mimetype='text/plain')
    if self.view == 'alternates':
        # call the base class' render alternates view method
        return self._render_alternates_view()
    elif self.view == 'pdm':
        # render the view with the token 'pdm' as text/html
        if self.format == 'text/html':
            # you need to define your own self.export_html()
            return self.export_html(model_view=self.view)
        else:
            # you need to define your own self.export_rdf()
            return Response(self.export_rdf(self.view, self.format), mimetype=self.format)
    elif self.view == 'nemsr':
        # you need to define your own self.export_nemsr_geojson()
        return self.export_nemsr_geojson()

The example code determines the response based on the set view and format of the object.

See also

See Custom Renderer for implementation details for Renderer.