Dependency injection for pyramid views
Project description
pyramid_autowire is a package that allows your pyramid views to use pyramids registry for dependency injection.
from pyramid_autowire import autowired
@view_defaults(route_name='test_route', mapper=autowired)
class Posts:
def __init__(
# Standard pyramid (context, request) and (request,) arguments
# are available through position-only args
self, request, /,
# the database and user objects will be injected from the registry
database: IDatabase, active_user: IUser
):
self.request = request
self.database = database
self.active_user = active_user
@view_config()
def view_post(
self, *,
# the matchdict is injected as keyword-only args
post_id
):
return self.database.query(Post, post_id)
def includeme(config):
registry = config.registry
# pretend for the example that the database session is global
registry.registerUtility(MyDatabase(), IDatabase)
def get_active_user(request):
# Fetch active user from the request data
...
registry.registerAdapter(get_active_user, (IRequest, ), IUser)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyramid_autowire-1.0.2.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for pyramid_autowire-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd0f21616eae0e5e8ad4e8d22fd5562cb3497113a25457298b1fcfc2026b5380 |
|
MD5 | e5793b1968ac3af4ee3edee5750271e2 |
|
BLAKE2b-256 | 3b1f03f83417d79e8c0d9425cdaf4501e43ef7ded12fe82fa38cc7197c42ad3f |