https://github.com/aio-libs/aiohttp-flashbag
The library provides flashbag for aiohttp
https://github.com/aio-libs/aiohttp-flashbag
Last synced: 6 months ago
JSON representation
The library provides flashbag for aiohttp
- Host: GitHub
- URL: https://github.com/aio-libs/aiohttp-flashbag
- Owner: aio-libs
- License: mit
- Created: 2017-11-15T15:22:50.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-11-25T01:47:53.000Z (about 2 years ago)
- Last Synced: 2025-06-09T07:08:35.723Z (7 months ago)
- Language: Python
- Homepage:
- Size: 19.5 KB
- Stars: 10
- Watchers: 10
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
aiohttp_flashbag
================
The library provides flashbag for `aiohttp.web`__.
.. _aiohttp_web: https://docs.aiohttp.org/en/stable/
__ aiohttp_web_
.. image:: https://img.shields.io/travis/aio-libs/aiohttp-flashbag.svg
:target: https://travis-ci.org/aio-libs/aiohttp-flashbag
.. image:: https://codecov.io/github/aio-libs/aiohttp-flashbag/coverage.svg
:target: https://codecov.io/github/aio-libs/aiohttp-flashbag
Usage
-----
The library allows us to share some data between requests inside session.
Basic usage example:
.. code-block:: python
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 = '{validation_error}'.format(
validation_error=validation_error,
)
body = '''
aiohttp_flashbag demo
{error_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.