View mapper for use with pyramid_services.
Project description
pyramid_services_viewmapper
An extension for the Pyramid web framework.
pyramid_services_viewmapper
provides a view mapper
for use in conjunction with pyramid_services.
It extends the functionality of pyramid_services
to provide dependency
injection: you can add custom arguments to your view functions, and
the view mapper will automatically populate those arguments with matching
services.
Setup
Register the view mapper in your Configurator
:
from pyramid_services_viewmapper import ServiceViewMapper
config.set_view_mapper(ServiceViewMapper)
Usage
Services registered by interface alone are injected by a simple type annotation. For example this...
@view_config(route_name="home", renderer="json")
def home(request, login_svc: ILoginService):
token = login_svc.create_token_for_login(name)
return {"access_token": token}
...is equivalent to this:
@view_config(route_name="home", renderer="json")
def home(request):
login_svc = request.find_service(ILoginService)
token = login_svc.create_token_for_login(name)
return {"access_token": token}
Services registered by name can be injected using a ServiceInjector
object.
For example this...
from pyramid_services_viewmapper import ServiceInjector as S
@view_config(route_name="users", renderer="json")
def users(request, db: S(name="db")):
users = db.query(User)
return {"users": users}
...is equivalent to this:
@view_config(route_name="users", renderer="json")
def users(request):
db = request.find_service(name="db")
users = db.query(User)
return {"users": users}
The ServiceInjector
class can be used as a shortcut for this:
from pyramid_services_viewmapper import ServiceInjector as S
@view_config(route_name="users", renderer="json")
def users(request, db: S):
users = db.query(User)
return {"users": users}
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
Hashes for pyramid_services_viewmapper-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d2f7ba55f555393ce05e9780f9791b3a2e038c95acf80fcc7b0462677635e2d |
|
MD5 | 116516879044bb0beba6a121ebcee8d8 |
|
BLAKE2b-256 | c831a7246dc8654fbbaf34263140b72f18bd4553e223c3d62ae6d0439fd93535 |
Hashes for pyramid_services_viewmapper-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 407438fa0488fb45d9cbec5fb6076d3285b748f9c3129143f82b712510d80d94 |
|
MD5 | 59f298334a1c3fe8b2650b5c39679401 |
|
BLAKE2b-256 | abb0a1817df0e41038d3ce8313f6d6c5b9e789965a94f236dd070f484ae3b8ee |