Commit d47d90f2 authored by Martijn Pieters's avatar Martijn Pieters

Extend the requestmethod decorator factory to allow multiple request methods

parent 5db07830
...@@ -27,12 +27,17 @@ def _buildFacade(spec, docstring): ...@@ -27,12 +27,17 @@ def _buildFacade(spec, docstring):
return 'def _facade%s:\n """%s"""\n return _curried%s' % ( return 'def _facade%s:\n """%s"""\n return _curried%s' % (
args, docstring, callargs) args, docstring, callargs)
def requestmethod(method): def requestmethod(*methods):
"""Create a request method specific decorator""" """Create a request method specific decorator"""
method = method.upper() methods = map(lambda m: m.upper(), methods)
if len(methods) > 1:
methodsstr = ', '.join(methods[:-1])
methodsstr += ' or ' + methods[-1]
else:
methodsstr = methods[0]
def _methodtest(callable): def _methodtest(callable):
"""Only allow callable when request method is %s.""" % method """Only allow callable when request method is %s.""" % methodsstr
spec = inspect.getargspec(callable) spec = inspect.getargspec(callable)
args, defaults = spec[0], spec[3] args, defaults = spec[0], spec[3]
try: try:
...@@ -51,8 +56,8 @@ def requestmethod(method): ...@@ -51,8 +56,8 @@ def requestmethod(method):
request = args[r_index] request = args[r_index]
if IBrowserRequest.providedBy(request): if IBrowserRequest.providedBy(request):
if request.method != method: if request.method not in methods:
raise Forbidden('Request must be %s' % method) raise Forbidden('Request must be %s' % methodsstr)
# Reconstruct keyword arguments # Reconstruct keyword arguments
if defaults is not None: if defaults is not None:
......
...@@ -74,3 +74,15 @@ pass in the desired request method:: ...@@ -74,3 +74,15 @@ pass in the desired request method::
Traceback (most recent call last): Traceback (most recent call last):
... ...
Forbidden: Request must be PUT Forbidden: Request must be PUT
You can pass in multiple request methods allow access by any of them::
>>> @requestmethod('GET', 'HEAD', 'PROPFIND')
... def foo(bar, REQUEST=None):
... return bar
>>> foo('spam', GET)
'spam'
>>> foo('spam', POST)
Traceback (most recent call last):
...
Forbidden: Request must be GET, HEAD or PROPFIND
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment