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

06/04/13 21:47:24 (8 years ago)
hardik juneja



  • S3/S3REST/s3_rest_controller

    v82 v83  
    478478=== Pre-Process ===
    480   - ''coming soon...''
     481A prep hook would allow you to change a handler configuration in certain situations, e.g. testing a URL variable:
     484def myresource():
     486   """ RESTful CRUD controller """
     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!
     495   # Hook pre-processor into REST controller:
     496   s3db.prep = prep
     498   output = s3_rest_controller(modulename, resourcename)
     499   return output
     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.
     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).
     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.
    482509==== Passing information between main controller & pre-processor ====