Decorators use by the TurboGears controllers.
Not all of these decorators are traditional wrappers. They are much simplified from the TurboGears 1 decorators, because all they do is register attributes on the functions they wrap, and then the DecoratedController provides the hooks needed to support these decorators.
This decorator allows you to ensure that the URL ends in “/”.
The decorator accomplish this by redirecting to the correct URL.
Usage : |
---|
You use this decorator as follows:
class MyController(object):
@with_trailing_slash
@expose()
def sample(self, *args):
return "found sample"
In the above example http://localhost:8080/sample redirects to http://localhost:8080/sample/ In addition, the URL http://localhost:8080/sample/1 redirects to http://localhost:8080/sample/1/
Ensure that the decorated method is always called with https.
This decorator allows you to ensure that the URL does not end in “/”.
The decorator accomplish this by redirecting to the correct URL.
Usage : |
---|
You use this decorator as follows:
class MyController(object):
@without_trailing_slash
@expose()
def sample(self, *args):
return "found sample"
In the above example http://localhost:8080/sample/ redirects to http://localhost:8080/sample In addition, the URL http://localhost:8080/sample/1/ redirects to http://localhost:8080/sample/1
Simple class to support ‘simple registration’ type decorators
Return the template engine data.
Provides a convenience method to get the proper engine, content_type, template, and exclude_names for a particular tg_format (which is pulled off of the request headers).
Registers a custom engine on the controller.
Multiple engines can be registered, but only one engine per custom_format.
The engine is registered when @expose is used with the custom_format parameter and controllers render using this engine when the use_custom_format() function is called with the corresponding custom_format.
exclude_names keeps track of a list of keys which will be removed from the controller’s dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other ‘automatic’ engines which don’t require a template.
render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype.
Registers the specified function as a hook.
We now have four core hooks that can be applied by adding decorators: before_validate, before_call, before_render, and after_render. register_hook attaches the function to the hook which get’s called at the appropriate time in the request life cycle.)
Registers an engine on the controller.
Multiple engines can be registered, but only one engine per content_type. If no content type is specified the engine is registered at / which is the default, and will be used whenever no content type is specified.
exclude_names keeps track of a list of keys which will be removed from the controller’s dictionary before it is loaded into the template. This allows you to exclude some information from JSONification, and other ‘automatic’ engines which don’t require a template.
render_params registers extra parameters which will be sent to the rendering method. This allows you to influence things like the rendering method or the injected doctype.
A list of callables to be run after the template is rendered.
Will be run before it is returned returned up the WSGI stack.
A list of callables to be run before the controller method is called.
A list of callables to be run before the template is rendered.
A list of callables to be run before validation is performed.
Register attributes on the decorated function.
Parameters : |
|
---|
The expose decorator registers a number of attributes on the decorated function, but does not actually wrap the function the way TurboGears 1.0 style expose decorators did.
This means that we don’t have to play any kind of special tricks to maintain the signature of the exposed function.
The exclude_names parameter is new, and it takes a list of keys that ought to be scrubbed from the dictionary before passing it on to the rendering engine. This is particularly useful for JSON.
The render_parameters is also new. It takes a dictionary of arguments that ought to be sent to the rendering engine, like this:
render_params={'method': 'xml', 'doctype': None}
Expose decorator can be stacked like this:
@expose('json', exclude_names='d')
@expose('kid:blogtutorial.templates.test_form',
content_type='text/html')
@expose('kid:blogtutorial.templates.test_form_xml',
content_type='text/xml', custom_format='special_xml')
def my_exposed_method(self):
return dict(a=1, b=2, d="username")
The expose(‘json’) syntax is a special case. json is a rendering engine, but unlike others it does not require a template, and expose assumes that it matches content_type=’application/json’
If you want to declare a desired content_type in a url, you can use the mime-type style dotted notation:
"/mypage.json" ==> for json
"/mypage.html" ==> for text/html
"/mypage.xml" ==> for xml.
If you’re doing an http post, you can also declare the desired content type in the accept headers, with standard content type strings.
By default expose assumes that the template is for html. All other content_types must be explicitly matched to a template and engine.
The last expose decorator example uses the custom_format parameter which takes an arbitrary value (in this case ‘special_xml’). You can then use the`use_custom_format` function within the method to decide which of the ‘custom_format’ registered expose decorators to use to render the template.
Override the template to be used.
Use override_template in a controller in order to change the template that will be used to render the response dictionary dynamically.
The template string passed in requires that you include the template engine name, even if you’re using the default.
So you have to pass in a template id string like:
"genshi:myproject.templates.index2"
future versions may make the genshi: optional if you want to use the default engine.
Paginate a given collection.
This decorator is mainly exposing the functionality of webhelpers.paginate().
Usage : |
---|
You use this decorator as follows:
class MyController(object):
@expose()
@paginate("collection")
def sample(self, *args):
collection = get_a_collection()
return dict(collection=collection)
To render the actual pager, use:
${tmpl_context.paginators.<name>.pager()}
It is possible to have several paginate()-decorators for one controller action to paginate several collections independently from each other. If this is desired, don’t forget to set the use_prefix-parameter to True.
Parameters : |
|
---|
TurboGears-specific action protector.
The default authorization denial handler of this protector will flash the message of the unmet predicate with warning or error as the flash status if the HTTP status code is 401 or 403, respectively.
See allow_only for controller-wide authorization.
Authorization denial handler for protectors.
Use use_custom_format in a controller in order to change the active @expose decorator when available.
Registers which validators ought to be applied.
If you want to validate the contents of your form, you can use the @validate() decorator to register the validators that ought to be called.
Parameters : |
|
---|
The first positional parameter can either be a dictonary of validators, a FormEncode schema validator, or a callable which acts like a FormEncode validator.
Decorator to force usage of a specific database engine in TurboGears SQLAlchemy BalancedSession.
Parameters: |
|
---|