kerno.peto module¶
Convenience types to store incoming state and remove boilerplate.
A Peto
is typically instantiated in the controller layer and then
passed around (as the main context object) to actions, event handlers,
schemas, helpers etc. This removes boilerplate, helps remove
global variables and makes unit testing easier.
A previous version had an Optional user
variable in Peto,
but that led to assert peto.user
in most actions. We avoid that
by providing also a UserlessPeto
class. Now it’s explicit in the type!
In your application you should subclass them like this:
from kerno.peto import AbstractPeto, AbsUserlessPeto
UserlessPeto = AbsUserlessPeto[Repo] # using the concrete Repo type
Peto = AbstractPeto[Repo, User] # with the concrete User type
- class kerno.peto.AbsUserlessPeto(kerno: kerno.kerno.Kerno, repo: kerno.peto.TRepo, raw: Dict[str, Any])[source]¶
Bases:
Generic
[kerno.peto.TRepo
]Convenience type for actions NOT done by a logged user.
- classmethod from_pyramid(request, json=False) kerno.peto.AbsUserlessPeto [source]¶
Integration with the pyramid web framework.
Typical usage is, inside your pyramid view, you do:
upeto = UserlessPeto.from_pyramid(request, json=True) # Call your action/service layer, which returns a Rezulto instance: rezulto = create_user(upeto) return rezulto
When invoked with
json=True
,upeto.raw
contains a dict with the JSON payload from the pyramid request.
- kerno: kerno.kerno.Kerno¶
- raw: Dict[str, Any]¶
- repo: kerno.peto.TRepo¶
- class kerno.peto.AbstractPeto(kerno: kerno.kerno.Kerno, repo: kerno.peto.TRepo, raw: Dict[str, Any], user: kerno.peto.TUser)[source]¶
Bases:
kerno.peto.AbsUserlessPeto
[kerno.peto.TRepo
],Generic
[kerno.peto.TRepo
,kerno.peto.TUser
]Convenience type for actions done by a logged user.
- classmethod from_pyramid(request, json=False) kerno.peto.APeto [source]¶
Integration with the pyramid web framework.
Typical usage is, inside your pyramid view, you do:
peto = Peto.from_pyramid(request, json=True) # Call your action/service layer, which returns a Rezulto instance: rezulto = create_user(peto) return rezulto
When invoked with
json=True
,peto.raw
contains a dict with the JSON payload from the pyramid request.Another example, very brief:
@kerno_view def get_audit_data(request): "\""Return audit data to be displayed to a superuser."\"" return get_audits( peto=Peto.from_pyramid(request), **request.json_body)
- classmethod from_userless(upeto: kerno.peto.AbsUserlessPeto, user: kerno.peto.TUser) kerno.peto.APeto [source]¶
Get a Peto instance from a UserlessPeto and a user object.
- user: kerno.peto.TUser¶