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.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9df0082f3daf9b34e56c8e49064a900897848ec4759ac3ff3a8f5247e54a43e |
|
MD5 | e52c83a77eccaafaee2749ebdcf9884c |
|
BLAKE2b-256 | 5dcc1467e79b1d68e8ed3ddbbea1d5a1d4cbde5d9c56783315b1bec024ec4961 |
Hashes for pyramid_services_viewmapper-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c5ef1a6a882c4f1dc11de371478f90893acd563534c88944664ed93b5e276bcd |
|
MD5 | fc254b3d3d3a40044dabe9b3303e20dd |
|
BLAKE2b-256 | 9c6fce80536b484d0ff5520a7cd49e74f04186759061d34b69ec04df8e0e5691 |