Skip to main content

Flashbag (flash messages) support for aiohttp.web

Project description

The library provides flashbag for aiohttp.web.

https://img.shields.io/travis/wikibusiness/aiohttp-flashbag.svg https://codecov.io/github/wikibusiness/aiohttp-flashbag/coverage.svg

Usage

The library allows us to share some data between requests inside session.

Basic usage example:

import aiohttp_flashbag
from aiohttp import web
from aiohttp_session import setup as setup_session
from aiohttp_session import SimpleCookieStorage


async def handler_get(request):
    validation_error = aiohttp_flashbag.flashbag_get(request, 'error')

    error_html = ''

    if validation_error is not None:
        error_html = '<span>{validation_error}</span>'.format(
            validation_error=validation_error,
        )

    body = '''
        <html>
            <head><title>aiohttp_flashbag demo</title></head>
            <body>
                <form method="POST" action="/">
                    <input type="text" name="name" />
                    {error_html}
                    <input type="submit" value="Say hello">
                </form>
            </body>
        </html>
    '''
    body = body.format(error_html=error_html)

    return web.Response(body=body.encode('utf-8'), content_type='text/html')


async def handler_post(request):
    post = await request.post()

    if len(post['name']) == 0:
        aiohttp_flashbag.flashbag_set(request, 'error', 'Name is required')

        return web.HTTPSeeOther('/')

    body = 'Hello, {name}'.format(name=post['name'])

    return web.Response(body=body.encode('utf-8'), content_type='text/html')


def make_app():
    session_storage = SimpleCookieStorage()

    app = web.Application()

    setup_session(app, session_storage)

    app.middlewares.append(aiohttp_flashbag.flashbag_middleware)

    app.router.add_route(
        'GET',
        '/',
        handler_get,
    )

    app.router.add_route(
        'POST',
        '/',
        handler_post,
    )

    return app


web.run_app(make_app())

First of all, you have to register aiohttp_flashbag.flashbag_middleware in aiohttp.web.Application.

You can get some data from the previous request with aiohttp_flashbag.flashbag_get method. Parameters:

  • request. Instance of aiohttp.web_request.Request.

  • key. Name of “variable” that you want to get

  • default. The default value that should be returned, if the key doesn’t exist in session flashbag.

To set one “variable” in flashbag you should use aiohttp_flashbag.flashbag_set. Parameters:

  • request. Instance of aiohttp.web_request.Request.

  • key. Name of “variable” that you want to specify.

  • value. Data that you want to specify.

If you need to replace all “variables” in flashbag you should use aiohttp_flashbag.flashbag_replace_all. Parameters:

  • request. Instance of aiohttp.web_request.Request.

  • value. Dict with values that you want to add into flashbag.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-flashbag-0.0.2.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

aiohttp_flashbag-0.0.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-flashbag-0.0.2.tar.gz.

File metadata

File hashes

Hashes for aiohttp-flashbag-0.0.2.tar.gz
Algorithm Hash digest
SHA256 602c38592bcf0f3ff1b3ee8ba5d08055e664fde7e81de9f83ae7c8259af55d3b
MD5 d2653d4450b147f13c9c285c822db1c9
BLAKE2b-256 d0245bf67112717501f4b2afe86c32782d28facd593eee922585e52551c7f227

See more details on using hashes here.

File details

Details for the file aiohttp_flashbag-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_flashbag-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2d977c4fdf08bc8cd663fc9f739f70ad7fff7d880c75d8ef8f47a3962464c2eb
MD5 1ae724ecfcc469a3aa109ce1bc28fb2f
BLAKE2b-256 96b5280a4030724b0a0e8586a1a78670537dccf6c7c901e1dbdfaad5fad7a20c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page