source: cows/trunk/cows/pylons/project_templates/cows_server/+package+/controllers/error.py_tmpl @ 4917

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/project_templates/cows_server/+package+/controllers/error.py_tmpl@4917
Revision 4917, 1.3 KB checked in by spascoe, 11 years ago (diff)

Created a Paste project template based on Dom's example server. This
was constructed by following the tutorial at
 http://pylonsbook.com/alpha1/simplesite_part_3 "Making SimpleSite? into a
Paste Project Template"

Line 
1import os.path
2
3import paste.fileapp
4from pylons.middleware import error_document_template, media_path
5
6from {{package}}.lib.base import *
7
8class ErrorController(BaseController):
9    """Generates error documents as and when they are required.
10
11    The ErrorDocuments middleware forwards to ErrorController when error
12    related status codes are returned from the application.
13
14    This behaviour can be altered by changing the parameters to the
15    ErrorDocuments middleware in your config/middleware.py file.
16    """
17
18    def document(self):
19        """Render the error document"""
20        page = error_document_template % \
21            dict(prefix=request.environ.get('SCRIPT_NAME', ''),
22                 code=request.params.get('code', ''),
23                 message=request.params.get('message', ''))
24        return page
25
26    def img(self, id):
27        """Serve Pylons' stock images"""
28        return self._serve_file(os.path.join(media_path, 'img', id))
29
30    def style(self, id):
31        """Serve Pylons' stock stylesheets"""
32        return self._serve_file(os.path.join(media_path, 'style', id))
33
34    def _serve_file(self, path):
35        """Call Paste's FileApp (a WSGI application) to serve the file
36        at the specified path
37        """
38        fapp = paste.fileapp.FileApp(path)
39        return fapp(request.environ, self.start_response)
Note: See TracBrowser for help on using the repository browser.