]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/controllers/error.py
Create Pylons app
[bluechips.git] / bluechips / controllers / error.py
1 import cgi
2 import os.path
3
4 from paste.urlparser import StaticURLParser
5 from pylons import request
6 from pylons.controllers.util import forward
7 from pylons.middleware import error_document_template, media_path
8
9 from bluechips.lib.base import BaseController
10
11 class ErrorController(BaseController):
12     """Generates error documents as and when they are required.
13
14     The ErrorDocuments middleware forwards to ErrorController when error
15     related status codes are returned from the application.
16
17     This behaviour can be altered by changing the parameters to the
18     ErrorDocuments middleware in your config/middleware.py file.
19     
20     """
21     def document(self):
22         """Render the error document"""
23         resp = request.environ.get('pylons.original_response')
24         page = error_document_template % \
25             dict(prefix=request.environ.get('SCRIPT_NAME', ''),
26                  code=cgi.escape(request.GET.get('code', resp.status)),
27                  message=cgi.escape(request.GET.get('message', resp.body)))
28         return page
29
30     def img(self, id):
31         """Serve Pylons' stock images"""
32         return self._serve_file(os.path.join(media_path, 'img'), id)
33
34     def style(self, id):
35         """Serve Pylons' stock stylesheets"""
36         return self._serve_file(os.path.join(media_path, 'style'), id)
37
38     def _serve_file(self, root, path):
39         """Call Paste's FileApp (a WSGI application) to serve the file
40         at the specified path
41         """
42         static = StaticURLParser(root)
43         request.environ['PATH_INFO'] = '/%s' % path
44         return static(request.environ, self.start_response)