kerno.web.pyramid package

Module contents

Integration between Kerno and the awesome Pyramid web framework.

kerno.web.pyramid.includeme(config) None[source]

Integrate kerno with Pyramid.

  • Make request.kerno available.

  • Make request.repo available.

  • Also register an IKerno interface so one can retrieve the kerno instance from the Pyramid registry with kerno = registry.queryUtility(IKerno, default=None).

  • Add our views to a Pyramid app so it will display our exceptions.

Usage example:

from kerno.web.pyramid import IKerno, Kerno

def init_kerno(config_path: str) -> Kerno:
    \"\"\"Return initialized kerno, separate from web frameworks.\"\"\"
    from kerno.start import Eko
    eko = Eko.from_ini(config_path)
    # ...more kerno initialization code here, then...
    return eko.kerno

def main(global_config, **settings):
    \"\"\"Return a Pyramid WSGI application.\"\"\"
    from pyramid.config import Configurator
    with Configurator(settings=settings) as config:
        kerno = init_kerno(global_config['__file__'])
        config.registry.registerUtility(kerno, IKerno)
        config.include('kerno.web.pyramid')
        # ...more Pyramid configuration code here, then at the end...
    return config.make_wsgi_app()

For the HTML version, if you’d like to change our template to your own, just override the Pyramid view configuration. Example:

config.add_view(
    context=MalbonaRezulto, accept='text/html', view=malbona_view,
    renderer='yourapp:templates/malbona.jinja2')
kerno.web.pyramid.kerno_view(fn: Callable) Callable[source]

Decorate Pyramid views that call Kerno actions or operations.

The view can return a Rezulto or RAISE a MalbonaRezulto. Then this decorator sets the status_int of the response (to 200 or 201) and converts it to a dictionary.

kerno.web.pyramid.malbona_view(context, request) Dict[str, Any][source]

Pyramid view handler that returns a MalbonaRezulto as a dictionary.

kerno.web.pyramid.raise_if_not_authenticated(request: kerno.web.pyramid.typing.KRequest) None[source]

Return 418 if the client is not authenticated.

The code 401 is for missing HTTP Basic Authentication and comes with related expectations, therefore we abuse “418 I’m a teapot” which was spent in a joke and therefore is never used.

This is better than Pyramid’s is_authenticated=True view predicate, which returns 404.

The response includes a UIMessage and a command to show the login form.