websauna.system.core.route module

Route related helpers.

websauna.system.core.route.add_template_only_view(config, pattern, name, template, view_args=None, route_args=None)[source]

Adds a view which do not have a specific view function assgined.

The view will render a template with the default template context.

Parameters
  • pattern (str) – A path where the view is, e.g. /features

  • name (str) – View name for route_url()

  • tempalte – A template to render

  • view_args (Optional[dict]) – kwargs passed to pyramid.config.Configurator.add_view()

  • route_args (Optional[dict]) – kwargs passed to pyramid.config.Configurator.add_view()

websauna.system.core.route.get_config_route(request, config_key)[source]

Route to a given URL from settings file.

Return type

str

class websauna.system.core.route.simple_route(path, *args, **kwargs)[source]

Bases: object

A set of simple defaults for Pyramid routing.

Pyramid’s URL dispatch has separate concepts for routes and views. This gives additional flexibility in that you can one route map to multiple views, using different predicates (e.g.: predicates depending on Accept header, whether request is XHR or not, etc.). In many applications, this flexibility is not needed and having both routes and views adds a bit of complexity and duplication, and reduces DRYness. This module implements some easy-to-use mechanisms that create a route and a view in one step, resulting in simpler, easier to understand code. This kind of makes Pyramid’s routing look a bit more like Flask, albeit without Flask’s controversial thread locals.

Note

At the moment simple_route doesn’t support viewdefaults.

Examples:

# Pyramid
from pyramid.response import Response

# Websauna
from websauna.system.core.route import simple_route


# ``simple_route`` decorator can be used to associate view configuration
# information with a function, method, or class that acts as a Pyramid view
# callable.
@simple_route('/path/to/view')
def view_callable(request):
    return Response('Hello')


# You can set route name or any of view configuration arguments explicitly.
@simple_route(
    '/path/to/view/{arg}',
    route_name='foobar',
    request_method='POST',
    renderer='json')
def my_view(request):
    arg = request.matchdict["arg"]
    return {'message': 'Hello {}'.format(arg)}


# More than one decorator can be stacked on top of any number of others. Each
# decorator creates a separate route and view registration. It's better to
# provide ``route_name`` explicitly in this case.
@simple_route('/change', route_name='change')
@simple_route('/edit', route_name='edit')
def edit(request):
    return Response('edited!')


# ``simple_route` can be used as a class decorator.
@simple_route('/class-as-a-view')
class ClassAsAView(object):
    def __init__(self, request):
        self.request = request

    def __call__(self):
        return Response('class-as-a-view')


# The decorator can also be used against a method of a class.
class MyViewClass(object):

    # Class constructor must accept an argument list in one of two forms:
    #   * either a single argument, request, or
    #   * two arguments, context, request.
    def __init__(self, request):
        self.request = request

    # The method which is decorated must return a response.
    @simple_route('/amethod')
    def amethod(self):
        return Response('amethod')


# Using the decorator against a particular method of a class is equivalent to
# using the attr parameter in a decorator attached to the class itself.
@simple_route('/class-attr', attr='amethod')
class MyViewClassAttr(object):

    def __init__(self, request):
        self.request = request

    def amethod(self):
        return Response('class-attr')

Respectively URLs for registered views are: