Changeset 5727

Show
Ignore:
Timestamp:
11/19/08 16:39:52 (2 months ago)
Author:
faide
Message:

Applied patch from #1996 Thanks to Peter Russell to have reported this nice implementation from Rick Copeland. See http://blog.pythonisito.com/2008/07/restfulness-in-turbogears.html for more details...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/controllers.py

    r5725 r5727  
    505505 
    506506 
     507class ExposedDescriptor(object): 
     508    """Descriptor used by RESTMethod to tell if it is exposed 
     509    """ 
     510    def __get__(self, obj, cls=None): 
     511        """Return True if the object has a method for the HTTP method of the current request 
     512        """ 
     513        if cls is None: cls = obj 
     514        cp_methodname = cherrypy.request.method 
     515        methodname = cp_methodname.lower() 
     516        method = getattr(cls, methodname, None) 
     517        if callable(method) and getattr(method, 'exposed', False): 
     518            return True 
     519        raise cherrypy.HTTPError(405, '%s not allowed on %s' % ( 
     520            cp_methodname, cherrypy.request.browser_url)) 
     521 
     522 
     523class RESTMethod(object): 
     524    """Allows REST style dispatch based on different HTTP methods 
     525 
     526    For an example usage see turbogears.tests.test_restmethod 
     527    """ 
     528    exposed = ExposedDescriptor() 
     529 
     530    def __init__(self, *l, **kw): 
     531        methodname = cherrypy.request.method.lower() 
     532        self.result = getattr(self, methodname)(*l, **kw) 
     533 
     534    def __iter__(self): 
     535        return iter(self.result) 
     536 
     537 
    507538def url(tgpath, tgparams=None, **kw): 
    508539    """Computes URLs. 
     
    511542    with a "/"), the server.webpath, SCRIPT_NAME and the approot of the 
    512543    application are prepended to the path. In order for the approot to 
     544 
    513545    be detected properly, the root object should extend 
     546 
    514547    controllers.RootController. 
    515548