[Checkins] SVN: Zope/trunk/lib/python/AccessControl/requestmethod. Extend the requestmethod decorator factory to allow multiple request methods

Martijn Pieters mj at zopatista.com
Tue Apr 3 11:49:16 EDT 2007


Log message for revision 73992:
  Extend the requestmethod decorator factory to allow multiple request methods

Changed:
  U   Zope/trunk/lib/python/AccessControl/requestmethod.py
  U   Zope/trunk/lib/python/AccessControl/requestmethod.txt

-=-
Modified: Zope/trunk/lib/python/AccessControl/requestmethod.py
===================================================================
--- Zope/trunk/lib/python/AccessControl/requestmethod.py	2007-04-03 15:38:16 UTC (rev 73991)
+++ Zope/trunk/lib/python/AccessControl/requestmethod.py	2007-04-03 15:49:16 UTC (rev 73992)
@@ -27,12 +27,17 @@
     return 'def _facade%s:\n    """%s"""\n    return _curried%s' % (
         args, docstring, callargs)
 
-def requestmethod(method):
+def requestmethod(*methods):
     """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):
-        """Only allow callable when request method is %s.""" % method
+        """Only allow callable when request method is %s.""" % methodsstr
         spec = inspect.getargspec(callable)
         args, defaults = spec[0], spec[3]
         try:
@@ -51,8 +56,8 @@
                 request = args[r_index]
                 
             if IBrowserRequest.providedBy(request):
-                if request.method != method:
-                    raise Forbidden('Request must be %s' % method)
+                if request.method not in methods:
+                    raise Forbidden('Request must be %s' % methodsstr)
     
             # Reconstruct keyword arguments
             if defaults is not None:

Modified: Zope/trunk/lib/python/AccessControl/requestmethod.txt
===================================================================
--- Zope/trunk/lib/python/AccessControl/requestmethod.txt	2007-04-03 15:38:16 UTC (rev 73991)
+++ Zope/trunk/lib/python/AccessControl/requestmethod.txt	2007-04-03 15:49:16 UTC (rev 73992)
@@ -74,3 +74,15 @@
   Traceback (most recent call last):
   ...
   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



More information about the Checkins mailing list