Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1229

Show
Ignore:
Timestamp:
08/08/06 01:35:07
Author:
fumanchu
Message:

Moved request.handler from a function (with vpath, and request.params included in cell refs) to an instance of a callable PageHandler? class (with vpath, params included as .args, .kwargs attributes). Instances of HTTPRedirect and HTTPError (including NotFound?) can now also be set directly as request.handler (they raise self when called).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cperror.py

    r1224 r1229  
    129129        else: 
    130130            raise ValueError("The %s status code is unknown." % status) 
     131     
     132    def __call__(self): 
     133        # Allow the exception to be used as a request.handler. 
     134        raise self 
    131135 
    132136 
     
    183187         
    184188        _be_ie_unfriendly(self.status) 
     189     
     190    def __call__(self): 
     191        # Allow the exception to be used as a request.handler. 
     192        raise self 
    185193 
    186194 
  • trunk/cherrypy/_cprequest.py

    r1225 r1229  
    390390 
    391391 
     392class PageHandler(object): 
     393    """Callable which sets response.body.""" 
     394     
     395    def __init__(self, callable, *args, **kwargs): 
     396        self.callable = callable 
     397        self.args = args 
     398        self.kwargs = kwargs 
     399     
     400    def __call__(self): 
     401        cherrypy.response.body = self.callable(*self.args, **self.kwargs) 
     402 
     403 
     404class LateParamPageHandler(PageHandler): 
     405     
     406    def __getattribute__(self, name): 
     407        attr = object.__getattribute__(self, name) 
     408        if name == "kwargs": 
     409            if attr: 
     410                kwargs = cherrypy.request.params.copy() 
     411                kwargs.update(attr) 
     412            else: 
     413                kwargs = cherrypy.request.params 
     414            return kwargs 
     415        else: 
     416            return attr 
     417 
     418 
    392419class Dispatcher(object): 
    393420     
     
    401428         
    402429        if func: 
    403             def handler(): 
    404                 cherrypy.response.body = func(*vpath, **request.params) 
    405             request.handler = handler 
    406         else: 
    407             def notfound(): 
    408                 raise cherrypy.NotFound() 
    409             request.handler = notfound 
     430            request.handler = LateParamPageHandler(func, *vpath) 
     431        else: 
     432            request.handler = cherrypy.NotFound() 
    410433     
    411434    def find_handler(self, path): 
     
    545568                func = getattr(resource, "GET", None) 
    546569            if func: 
    547                 def handler(): 
    548                     cherrypy.response.body = func(*vpath, **request.params) 
    549                 request.handler = handler 
    550                 return 
     570                request.handler = LateParamPageHandler(func, *vpath) 
    551571            else: 
    552                 def notallowed(): 
    553                     raise cherrypy.HTTPError(405) 
    554                 request.handler = notallowed 
    555         else: 
    556             def notfound(): 
    557                 raise cherrypy.NotFound() 
    558             request.handler = notfound 
     572                request.handler = cherrypy.HTTPError(405) 
     573        else: 
     574            request.handler = cherrypy.NotFound() 
    559575 
    560576 

Hosted by WebFaction

Log in as guest/cpguest to create tickets