/usr/lib/python2.7/dist-packages/pylons/decorators/rest.py is in python-pylons 1.0.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | """REST decorators"""
import logging
from decorator import decorator
from pylons.controllers.util import abort
from pylons.decorators.util import get_pylons
__all__ = ['dispatch_on', 'restrict']
log = logging.getLogger(__name__)
def restrict(*methods):
"""Restricts access to the function depending on HTTP method
Example:
.. code-block:: python
from pylons.decorators import rest
class SomeController(BaseController):
@rest.restrict('GET')
def comment(self, id):
"""
def check_methods(func, *args, **kwargs):
"""Wrapper for restrict"""
if get_pylons(args).request.method not in methods:
log.debug("Method not allowed by restrict")
abort(405, headers=[('Allow', ','.join(methods))])
return func(*args, **kwargs)
return decorator(check_methods)
def dispatch_on(**method_map):
"""Dispatches to alternate controller methods based on HTTP method
Multiple keyword arguments should be passed, with the keyword
corresponding to the HTTP method to dispatch on (DELETE, POST, GET,
etc.) and the value being the function to call. The value should be
a string indicating the name of the function to dispatch to.
Example:
.. code-block:: python
from pylons.decorators import rest
class SomeController(BaseController):
@rest.dispatch_on(POST='create_comment')
def comment(self):
# Do something with the comment
def create_comment(self, id):
# Do something if its a post to comment
"""
def dispatcher(func, self, *args, **kwargs):
"""Wrapper for dispatch_on"""
alt_method = method_map.get(get_pylons(args).request.method)
if alt_method:
alt_method = getattr(self, alt_method)
log.debug("Dispatching to %s instead", alt_method)
return self._inspect_call(alt_method, **kwargs)
return func(self, *args, **kwargs)
return decorator(dispatcher)
|