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
Built Distribution
Close
Hashes for pyramid_autowire-0.1.0.dev3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bfbadc3ed2345c28e2de4b60367fede1454d4342c25b3dbedeb96c15b9c7aff |
|
MD5 | ad012bdcbba66e83627a82c8d413dc72 |
|
BLAKE2b-256 | 82684e33ee62107483c72d0830ffea7d08b6fe00e9b43b0cdde4b6910c296af7 |
Close
Hashes for pyramid_autowire-0.1.0.dev3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f70a904748785cc6246ba41cfbbac8cc963ade156d3e8354b884c8a8bbac67e |
|
MD5 | 7d560297b2a9d22a560aec817209b954 |
|
BLAKE2b-256 | 613930f94f70fe325c659b7335eaceede91d8ea9f18f7ecd855d0be18bee38f3 |