Skip to main content

Django user for async frameworks

Project description

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 valid
  • user.login() - sets session variables logging the user in
  • user.logout() - clears the session data
  • await user.set_password(password) - sets a new password for the user
  • await user.save([fields]) - saves the whole user or a particular set of its fields
  • await 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

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

async_django_user-0.2.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file async_django_user-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: async_django_user-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for async_django_user-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 149f1f189883044f4f0ed66ab2406d9f5515cc939faef9b9d84420b212561b72
MD5 8a21bd53c68151d339bcdda9df205326
BLAKE2b-256 b4299c598e3283e9c08e87852f471d9ca39a08a79093dcb500c9ba7220f4310c

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