Changes between Version 82 and Version 83 of S3/S3REST/s3_rest_controller


Ignore:
Timestamp:
06/04/13 21:47:24 (11 years ago)
Author:
hardik juneja
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/S3REST/s3_rest_controller

    v82 v83  
    478478=== Pre-Process ===
    479479
    480   - ''coming soon...''
     480
     481A prep hook would allow you to change a handler configuration in certain situations, e.g. testing a URL variable:
     482
     483{{{
     484def 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
     502This 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
     504While 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
     506A very important structure during prep and postp is the S3Request object (usually instantiated as "r"). This object contains all necessary
     507information 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.
    481508
    482509==== Passing information between main controller & pre-processor ====