Middlewares

Dazzler middleware acts on all dazzler pages and routes. They are used to modify the aiohttp requests context.

Postgres

dazzler.contrib.postgresql.PostgresMiddleware

Insert aiopg pool in the request context.

Install

pip install dazzler[postgresql]

Configure

Set the connection details with configuration or with environ POSTGRES_DSN:

[postgres]
dsn = 'host=localhost port=5432 dbname=dbname user=user password=pw'

Options to change the keys used for the request and application:

[postgres.middleware]
# Default values
request_key = 'postgres'
app_key = 'postgres'

Note

A PostgresMiddleware is automatically added to the application if using the session or authentication system with PostgreSQL.

Usage

Use in context:

@page.bind('value@input')
async def on_input(ctx: BindingContext):
    pool = ctx.request['postgres']

    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute('...')

See also

aiopg documentation: https://aiopg.readthedocs.io/

Redis

dazzler.contrib.redis.Redis

Insert aioredis pool in the request context.

Install

pip install dazzler[redis]

Configure

Set REDIS_URL environ variable to change the url used.

Note

Automatically added when the session backend is set to Redis

[session]
backend = 'Redis'

Usage

Use in context:

@page.bind('value@input')
async def on_value(ctx: BindingContext):
    redis = ctx.request['redis']

Session

dazzler.system.session.SessionMiddleware

Insert a Session instance into the request context. Automatically scoped to the current visitor.

Configure

Set the backend to use from the following options: File, Redis, PostgreSQL.

[session]
backend = 'File'

Usage

Use with the context, directly available:

@page.bind('value@input')
async def on_input(ctx: BindingContext):
    session_value = await ctx.session.get('key')

Auth

dazzler.system.auth.AuthMiddleware

Adds the current user to the request.

Note

Automatically added when using the authentication system.

Custom

Create custom middlewares by implementing dazzler.system.Middleware