| | 1 | = S3Method = |
| | 2 | |
| | 3 | '''S3Method''' is an interface to implement method handlers for the [wiki:S3XRC/RESTfulAPI RESTful API]. Method handlers are used to perform requested actions on S3Resources, and produce the output for the view. |
| | 4 | |
| | 5 | == Implementing Methods == |
| | 6 | |
| | 7 | Method handlers must be callable objects, i.e. they can be instances of classes which implement the __call__ method, lambdas or normal functions. |
| | 8 | |
| | 9 | In any case, they must take the {{{S3Request}}} instance (representing the HTTP request) and a variable list of named arguments as parameters: |
| | 10 | |
| | 11 | Function: |
| | 12 | {{{ |
| | 13 | def my_method_handler(r, **attr): |
| | 14 | return dict() |
| | 15 | }}} |
| | 16 | |
| | 17 | Lambda: |
| | 18 | {{{ |
| | 19 | my_method_handler = lambda r, **attr: dict() |
| | 20 | }}} |
| | 21 | |
| | 22 | Class and instance: |
| | 23 | {{{ |
| | 24 | class MyMethodHandler: |
| | 25 | def __call__(r, **attr): |
| | 26 | return dict() |
| | 27 | |
| | 28 | my_method_handler = MyMethodHandler() |
| | 29 | }}} |
| | 30 | |
| | 31 | The returned {{{dict}}} will be passed to the view, so it would usually contain the output data. |
| | 32 | |
| | 33 | == S3Method Interface == |
| | 34 | |
| | 35 | The {{{S3Method}}} class implements the {{{__call__(r, **attr)}}} method, and adds some attributes and methods which are commonly needed in most method handlers: |
| | 36 | |
| | 37 | '''Attributes:''' |
| | 38 | - '''manager''': the S3ResourceController instance |
| | 39 | - '''session''': the session store |
| | 40 | - '''request''': the web2py Request instance |
| | 41 | - '''response''': the web2py Response instance |
| | 42 | - '''T''': the global translator object |
| | 43 | - '''db''': the global DAL instance (database) |
| | 44 | - '''download_url''': the default download-URL |
| | 45 | - '''method''': string containing the requested method |
| | 46 | |
| | 47 | '''Resource:''' |
| | 48 | - '''resource''': the S3Resource instance for the requested resource |
| | 49 | - '''prefix''': the application prefix ("Module" prefix) |
| | 50 | - '''name''': the name of the requested resource (without prefix) |
| | 51 | - '''table''': the corresponding database table |
| | 52 | - '''tablename''': the database table name |
| | 53 | - '''record''': the record ID specified in the URL |
| | 54 | |
| | 55 | Note that, other than in {{{S3Request}}} instances, the resource attributes here refer to the target resource of the request, meaning, this would be the ''component'' if a component has been requested. To get at the parent resource, use {{{self.resource.parent}}}. |
| | 56 | |
| | 57 | '''Methods:''' |
| | 58 | - '''permit''': check authorization (shortcut for auth.shn_has_permission) |
| | 59 | - '''_config''': gets a CRUD setting of the target table |
| | 60 | - '''_view''': finds the pathname of a custom view template |
| | 61 | |
| | 62 | '''Hooks:''' |
| | 63 | - '''next''': URL to redirect to after the current request has been completed, typically set by the method handler (Note: this redirection does never happen after GET) |
| | 64 | |
| | 65 | To implement a method handler as {{{S3Method}}}, you would typically subclass {{{S3Method}}} and implement the {{{apply_method}}} method: |
| | 66 | |
| | 67 | {{{ |
| | 68 | class MyMethodHandler(S3Method): |
| | 69 | |
| | 70 | def apply_method(r, **attr): |
| | 71 | return dict() |
| | 72 | }}} |
| | 73 | |
| | 74 | For examples see modules/s3/s3crud.py or modules/s3/s3search.py. |