morepath.publish – Web publisher

Functionality to turn a morepath.Request into a morepath.Response using Morepath configuration. It looks up a model instance for the request path and parameters, then looks up a view for that model object to create the response.

The publish module:

  • resolves the request into a model object.
  • resolves the model object and the request into a view.
  • the view then generates a response.

It all starts at publish().

morepath.publish.consume(app, request)

Consume path segments from request to find model obj.

Removes the successfully consumed path segments from morepath.Request.unconsumed.

Uses morepath.traject.Traject.consume() to consume path segments according to path configuration.

Extracts URL parameters from the path.

Gets a factory function and uses matched path variables and URL parameters to construct the model instance (or morepath.App instance).

Parameters:
  • app – the morepath.App instance that contains the path registry to use.
  • requestmorepath.Request instance that contains the path segments to consume.
Returns:

The new model object, or a mounted morepath.App instance, or None if no new instance could be found.

morepath.publish.get_view_name(stack)

Determine view name from leftover stack of path segments

Parameters:stack – a list of path segments left over after consuming the path.
Returns:view name string or None if no view name can be determined.
morepath.publish.publish(request)

Handle request and return response.

It uses resolve_model() to use the information in request (path, request method, etc) to resolve to a model object. resolve_response() then creates a view for the request and the object.

Parameters:
morepath.publish.resolve_model(request)

Resolve request to a model object.

This takes the path information as a stack of path segments in morepath.Request.unconsumed and consumes it step by step using consume() to find the model object as declared by morepath.App.path() directive. It can traverse through mounted applications as indicated by the morepath.App.mount() directive.

The implicit Reg lookup used to look up generic dispatch functions is set to the app and is updated to the mounted application when traversing into it.

Param:morepath.Request instance.
Returns:model object or None if not found.
morepath.publish.resolve_response(obj, request)

Given model object and request, create response.

This uses get_view_name() to set up the view name on the request object.

If no view name exist it raises webob.exc.HTTPNotFound.

It then uses morepath.generic.view() to resolve the view for the model object and the request by doing dynamic dispatch.

Parameters:
  • obj – model object to get response for.
  • requestmorepath.Request instance.
Returns:

morepath.Response instance