Async Django User
Using django user with async frameworks like aiohttp, starlette etc.
pip install async-django-session async-django-user
tl;dr
Take a look at registration / authorization examples for aiohttp + databases or starlette + asyncpg.
API
Backends
There's two ways of communicating to database available:
- through databases - which is compatible with most of major RDBMS:
database = databases.Database(DB_URI) await database.connect() backend = async_django_user.databases.Backend(database, SECRET_KEY)
- or directly through asyncpg (PostgreSQL only):
pool = await asyncpg.create_pool(DB_URI) backend = async_django_user.asyncpg.Backend(pool, SECRET_KEY)
User
To fetch an user from db by its id stored in [django session] there's
backend.get_user_from_session method:
user = backend.get_user_from_session(session)
It's lazy so the user data won't be actually fetched until you call its
load method. It caches the result, so it's inexpensive to call it multiple
times:
await user.load()
User provides dict interface to it's data (eg user["username"]) and a few
methods:
await user.authenticate(username, password)- checks credentials and populates the user from database if they're validuser.login()- sets session variables logging the user inuser.logout()- clears the session dataawait user.set_password(password)- sets a new password for the userawait user.save([fields])- saves the whole user or a particular set of its fieldsawait register()- saves a new user into db
Frameworks integration
There's built-in middlewares for a few async frameworks to automatically load user of the current request. Take a look at examples folder for:
Running examples
Running the examples you can see different frameworks using the same session and user data.
Install the requirements:
cd examples
pip install -r requirements.txt
Create database and tables:
createdb async_django_session
python django_app.py migrate
Create a user:
python django_app.py createsuperuser
Run aiohttp example which uses databases backend:
python aiohttp_app.py
Run starlette example which uses asyncpg backend:
python starlette_app.py
Run django example:
python django_app.py runserver
Metadata
Release files for async-django-user 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| async_django_user-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Release files / async_django_user-0.1.1-py3-none-any.whl
| Download URL | async_django_user-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bd7c9b63095c1814bfb1eab85ea0a29419676746e8dad971822160e59cd752e6
|
|
BLAKE2b-256 checksum How to use checksums |
6c2e1aee7baf820646446921004674c1d835d24062c31ac2d37c583c94e877a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Security for async-django-user 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.