typecasting
¶
Fork of aspen.typecasting to clean some stuff up. Upstream it, ya?
-
gratipay.typecasting.
cast
(website, request, state)[source]¶ Implement typecasting (differently from stock Aspen).
When matching paths, Aspen looks for
/%foo/
and then foo is a variable with the value in the URL path, so/bar/
would end up withfoo='bar'
.There’s a dictionary at
website.typecasters
that maps variable names to functions, dependency-injectable as withwebsite.algorithm
(state-chain) functions. If an entry exists intypecasters
for a given path variable, then the value ofpath[part]
is replaced with the result of calling the function.Before calling your cast function, we add an additional value to the state dict at
path_part
: the URL path part that matched, as a string. That is user input, so handle it carefully. It’s your job to raiseResponse(40x)
if it’s bad input.