diff options
author | Ryan Petrello <lists@ryanpetrello.com> | 2015-01-16 13:30:45 -0500 |
---|---|---|
committer | Ryan Petrello <lists@ryanpetrello.com> | 2015-05-21 12:58:48 -0700 |
commit | baf80afa5eda4928eea2349c5cc494b09783ddd2 (patch) | |
tree | bfc2345902e1ac8817c1e675d2b3bdbc344903f9 /pecan/decorators.py | |
parent | 26d1d59aec72d3123cbff8dc929f200a198457fb (diff) | |
download | pecan-baf80afa5eda4928eea2349c5cc494b09783ddd2.tar.gz |
Allow users to specify custom path segments for routing.
Change-Id: I3f998d62c2f188818a747f1aa91beb9439ed77a2
Diffstat (limited to 'pecan/decorators.py')
-rw-r--r-- | pecan/decorators.py | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/pecan/decorators.py b/pecan/decorators.py index 36d33fa..68195a9 100644 --- a/pecan/decorators.py +++ b/pecan/decorators.py @@ -13,10 +13,10 @@ __all__ = [ def when_for(controller): def when(method=None, **kw): def decorate(f): - expose(**kw)(f) _cfg(f)['generic_handler'] = True controller._pecan['generic_handlers'][method.upper()] = f controller._pecan['allowed_methods'].append(method.upper()) + expose(**kw)(f) return f return decorate return when @@ -24,7 +24,8 @@ def when_for(controller): def expose(template=None, content_type='text/html', - generic=False): + generic=False, + route=None): ''' Decorator used to flag controller methods as being "exposed" for @@ -38,6 +39,11 @@ def expose(template=None, ``functools.singledispatch`` generic functions. Allows you to split a single controller into multiple paths based upon HTTP method. + :param route: The name of the path segment to match (excluding + separator characters, like `/`). Defaults to the name of + the function itself, but this can be used to resolve paths + which are not valid Python function names, e.g., if you + wanted to route a function to `some-special-path'. ''' if template == 'json': @@ -47,8 +53,19 @@ def expose(template=None, # flag the method as exposed f.exposed = True - # set a "pecan" attribute, where we will store details cfg = _cfg(f) + + if route: + # This import is here to avoid a circular import issue + from pecan import routing + if cfg.get('generic_handler'): + raise ValueError( + 'Path segments cannot be overridden for generic ' + 'controllers.' + ) + routing.route(route, f) + + # set a "pecan" attribute, where we will store details cfg['content_type'] = content_type cfg.setdefault('template', []).append(template) cfg.setdefault('content_types', {})[content_type] = template |