480 | | - ''coming soon...'' |
| 480 | |
| 481 | A prep hook would allow you to change a handler configuration in certain situations, e.g. testing a URL variable: |
| 482 | |
| 483 | {{{ |
| 484 | def myresource(): |
| 485 | |
| 486 | """ RESTful CRUD controller """ |
| 487 | |
| 488 | # Define pre-processor as local function: |
| 489 | def prep(r): |
| 490 | mylist = request.vars.get("mylist") |
| 491 | if mylist: |
| 492 | r.set_handler("list", my_list_controller) |
| 493 | return True # do not forget to return True! |
| 494 | |
| 495 | # Hook pre-processor into REST controller: |
| 496 | s3db.prep = prep |
| 497 | |
| 498 | output = s3_rest_controller(modulename, resourcename) |
| 499 | return output |
| 500 | }}} |
| 501 | |
| 502 | This example would switch to my_list_controller instead of shn_list in case there is a ?mylist= in the URL. In all other cases, the default handlers are executed as usual, you still have a RESTful API for your resources. |
| 503 | |
| 504 | While you can define prep's and postp's as local functions (as in the example above) or even lambdas, it is also very well possible to create more generic, reusable prep and postp functions (e.g. to implement different method handler configurations, or to catch certain situations to bypass the CRUD, or to manipulate the output dict in a certain way). |
| 505 | |
| 506 | A very important structure during prep and postp is the S3Request object (usually instantiated as "r"). This object contains all necessary |
| 507 | information to process the current REST request, and it is passed to both prep and postp. See [wiki:S3XRC#RESTfulAPI] for a list of attributes and methods. |