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

Changeset 727

Show
Ignore:
Timestamp:
10/08/05 13:44:09
Author:
fumanchu
Message:

Tutorial fixes, plus a bug in _cputil.getErrorPage.

Files:

Legend:

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

    r709 r727  
    211211    if errorPageFile: 
    212212        try: 
    213             template = file(errorPageFile, 'rb') 
     213            template = file(errorPageFile, 'rb').read() 
    214214        except: 
    215215            m = kwargs['message'] 
  • trunk/cherrypy/test/test_tutorials.py

    r723 r727  
    172172        self.assertHeader("Content-Disposition", "attachment; filename=pdf_file.pdf") 
    173173        self.assertEqual(len(self.body), 85698) 
     174     
     175    def test10HTTPErrors(self): 
     176        self.load_tut_module("tut10_http_errors") 
     177         
     178        self.getPage("/") 
     179        self.assertInBody("""<a href="toggleTracebacks">""") 
     180        self.assertInBody("""<a href="/doesNotExist">""") 
     181        self.assertInBody("""<a href="/error?code=403">""") 
     182        self.assertInBody("""<a href="/error?code=500">""") 
     183        self.assertInBody("""<a href="/messageArg">""") 
     184         
     185        tracebacks = cherrypy.config.get('server.showTracebacks') 
     186        self.getPage("/toggleTracebacks") 
     187        self.assertEqual(cherrypy.config.get('server.showTracebacks'), not tracebacks) 
     188        self.assertStatus("302 Found") 
     189         
     190        self.getPage("/error?code=500") 
     191        self.assertStatus("500 Internal error") 
     192        self.assertInBody("Server got itself in trouble") 
     193         
     194        self.getPage("/error?code=403") 
     195        self.assertStatus("403 Forbidden") 
     196        self.assertInBody("<h2>You can't do that!</h2>") 
     197         
     198        self.getPage("/messageArg") 
     199        self.assertStatus("500 Internal error") 
     200        self.assertInBody("If you construct an HTTPError with a 'message'") 
     201 
    174202 
    175203if __name__ == "__main__": 
  • trunk/cherrypy/tutorial/custom_error.html

    r650 r727  
    77</head> 
    88    <body> 
    9  
    109        <h2>You can't do that!</h2> 
     10        <p>%(message)s</p> 
    1111        <p>This is a custom error page that is read from a file.<p> 
     12        <p>%(traceback)s</p> 
    1213    </body> 
    13  
    1414</html> 
    15  
    16  
    17 <?xml version="1.0" encoding="UTF-8"?> 
    18 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    19   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    20 <html> 
    21 <head> 
    22     <title>403 Unauthorized</title> 
    23 </head> 
    24     <body> 
    25  
    26         <h2>You can't do that!</h2> 
    27         <p>This is a custom error page that is read from a file.<p> 
    28     </body> 
    29  
    30 </html> 
    31  
    32  
  • trunk/cherrypy/tutorial/tut01_helloworld.py

    r382 r727  
    11""" 
    2 Tutorial 01 - Hello World 
     2Tutorial - Hello World 
    33 
    44The most basic (working) CherryPy application possible. 
  • trunk/cherrypy/tutorial/tut02_expose_methods.py

    r382 r727  
    11""" 
    2 Tutorial 02 - Multiple methods 
     2Tutorial - Multiple methods 
    33 
    44This tutorial shows you how to link to other methods of your request 
  • trunk/cherrypy/tutorial/tut03_get_and_post.py

    r382 r727  
    11""" 
    2 Tutorial 03 - Passing variables 
     2Tutorial - Passing variables 
    33 
    44This tutorial shows you how to pass GET/POST variables to methods. 
  • trunk/cherrypy/tutorial/tut04_complex_site.py

    r382 r727  
    11""" 
    2 Tutorial 04 - Multiple objects 
     2Tutorial - Multiple objects 
    33 
    44This tutorial shows you how to create a site structure through multiple 
  • trunk/cherrypy/tutorial/tut05_derived_objects.py

    r382 r727  
    11""" 
    2 Tutorial 05 - Object inheritance 
     2Tutorial - Object inheritance 
    33 
    44You are free to derive your request handler classes from any base 
  • trunk/cherrypy/tutorial/tut06_default_method.py

    r382 r727  
    11""" 
    2 Tutorial 07 - The default method 
     2Tutorial - The default method 
    33 
    44Request handler objects can implement a method called "default" that 
  • trunk/cherrypy/tutorial/tut07_sessions.py

    r522 r727  
    11""" 
    2 Tutorial 08 - Sessions 
     2Tutorial - Sessions 
    33 
    44Storing session data in CherryPy applications is very easy: cherrypy.request 
  • trunk/cherrypy/tutorial/tut09_files.py

    r723 r727  
    1 """Tutorial: File upload""" 
     1""" 
     2 
     3Tutorial: File upload and download 
     4 
     5Uploads 
     6------- 
     7 
     8When a client uploads a file to a CherryPy application, it's placed 
     9on disk immediately. CherryPy will pass it to your exposed method 
     10as an argument (see "myFile" below); that arg will have a "file" 
     11attribute, which is a handle to the temporary uploaded file. 
     12If you wish to permanently save the file, you need to read() 
     13from myFile.file and write() somewhere else. 
     14 
     15Note the use of 'enctype="multipart/form-data"' and 'input type="file"' 
     16in the HTML which the client uses to upload the file. 
     17 
     18 
     19Downloads 
     20--------- 
     21 
     22If you wish to send a file to the client, you have two options: 
     23First, you can simply return a file-like object from your page handler. 
     24CherryPy will read the file and serve it as the content (HTTP body) 
     25of the response. However, that doesn't tell the client that 
     26the response is a file to be saved, rather than displayed. 
     27Use cherrypy.lib.cptools.serveFile for that; it takes four 
     28arguments: 
     29 
     30serveFile(path, contentType=None, disposition=None, name=None) 
     31 
     32Set "name" to the filename that you expect clients to use when they save 
     33your file. Note that the "name" argument is ignored if you don't also 
     34provide a "disposition" ("application/x-download" works in most cases). 
     35 
     36""" 
    237 
    338import os 
  • trunk/cherrypy/tutorial/tut10_http_errors.py

    r650 r727  
    1 """Tutorial: http errors """ 
     1""" 
     2 
     3Tutorial: HTTP errors 
     4 
     5HTTPError is used to return an error response to the client. 
     6CherryPy has lots of options regarding how such errors are 
     7logged, displayed, and formatted. 
     8 
     9""" 
    210 
    311import cherrypy 
    412 
    5 # we want to customize 403 errors 
    6 customErrors = { 
    7                  'errorPage.403' : "custom_error.html" 
    8                } 
    9  
    10 cherrypy.config.update({'/' : customErrors}) 
    1113 
    1214class HTTPErrorDemo(object): 
     
    2224        return """ 
    2325        <html><body> 
    24             <h2><a href="toggleTracebacks">Toggle tracebacks %s</a><br/><br/></h2> 
    25             <a href="/doesNotExist">Click me i'm a broken link!</a
    26             <br/><br/
    27             <a href="/error?code=403">Use a custom an error page from a file.</a
    28             <br/><br/
    29             These errors are explicitly raised by the application. 
    30             <a href="/error?code=400">400</a
    31             <a href="/error?code=401">401</a
    32             <a href="/error?code=402">402</a
    33             <a href="/error?code=500">500</a
    34             <br/><br/> 
    35             <a href="/bodyArg">You can also set the response body when you raise an error</a
     26            <h2><a href="toggleTracebacks">Toggle tracebacks %s</a></h2> 
     27            <p><a href="/doesNotExist">Click me; I'm a broken link!</a></p
     28            <p><a href="/error?code=403">Use a custom an error page from a file.</a></p
     29            <p>These errors are explicitly raised by the application:</p
     30            <ul
     31                <li><a href="/error?code=400">400</a></li> 
     32                <li><a href="/error?code=401">401</a></li
     33                <li><a href="/error?code=402">402</a></li
     34                <li><a href="/error?code=500">500</a></li
     35            </ul
     36            <p><a href="/messageArg">You can also set the response body 
     37            when you raise an error.</a></p
    3638        </body></html> 
    3739        """ % trace 
    3840    index.exposed = True 
    39  
     41     
    4042    def toggleTracebacks(self): 
    4143        # simple function to toggle tracebacks on and off  
     
    4446         
    4547        # redirect back to the index 
    46         raise cherrypy._cperror.HTTPRedirect('/') 
    47     toggleTracebacks.exposed=True 
     48        raise cherrypy.HTTPRedirect('/') 
     49    toggleTracebacks.exposed = True 
    4850     
    4951    def error(self, code): 
    5052        # raise an error based on the get query 
    51         code = int(code) 
    5253        raise cherrypy.HTTPError(status = code) 
    5354    error.exposed = True 
     55     
     56    def messageArg(self): 
     57        message = ("If you construct an HTTPError with a 'message' " 
     58                   "argument, it wil be placed on the error page " 
     59                   "(underneath the status line by default).") 
     60        raise cherrypy.HTTPError(500, message=message) 
     61    messageArg.exposed = True 
    5462 
    55     def bodyArg(self): 
    56         message = """ If you construct a HTTPError wiht body argument, the body argument 
    57                       will overide any default or custom error page. 
    58                   """ 
    59         raise cherrypy.HTTPError(403, body = message) 
    60     bodyArg.exposed = True 
    6163 
    6264cherrypy.root = HTTPErrorDemo() 
     65 
     66# Set a custom response for 403 errors. 
     67import os 
     68localDir = os.path.dirname(__file__) 
     69curpath = os.path.normpath(os.path.join(os.getcwd(), localDir)) 
     70cherrypy.config.update({'errorPage.403' : os.path.join(curpath, "custom_error.html")}) 
     71 
    6372 
    6473if __name__ == '__main__': 

Hosted by WebFaction

Log in as guest/cpguest to create tickets