Url redirection using pyramid#
This module allow to define redirection rules used by pyramid before route selection.
Each rule has to be added with add_rewriting_rule method in a pyramid includeme function to be used.
Example of usage :
def includeme(config): config.add_redirection_rule(r'<a regex>', callback_method, keep_query_components)
The callback_method takes four arguments:
a pyramid request ;
the url that has to be matched by the rule ;
a dictionnary containing all the named subgroups of the regex match ;
a boolean to decide if we want to keep parameters of the query after the redirection (True by default).
It must return a string corresponding to the new url.
Add add_redirection_rule pyramid directive and url_redirection_tween pyramid tween handler to the pyramid configuration.
- cubicweb.pyramid.url_redirection.add_redirection_rule(config, rule, callback_method, keep_query_component=True)#
Declare a pyramid directive allowing to add a redirection rule. Each rewriting rule can then be added in the includeme at the end of the file by using config.add_redirection_rule(rule, callback_method. :param config: pyramid configuration, used by pyramid. :param rule: a regex used to match uri. :param callback_method: a method which return the redirected uri. :param keep_query_component: if we want to keep the query component of the URL (the part after the first “?”) after redirection.
- cubicweb.pyramid.url_redirection.url_redirection_tween_factory(handler, registry)#
A pyramid tween handler that browse each redirection_rules added with add_redirection_rule directive to find if the current path matches a rule.