PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/minger_plus/controllers/error.py

https://bitbucket.org/bwmcadams/mingerplus
Python | 46 lines | 31 code | 5 blank | 10 comment | 1 complexity | 80869457133653b03468cce77f9e6f1f MD5 | raw file
 1import cgi
 2
 3from paste.urlparser import PkgResourcesParser
 4from pylons.middleware import error_document_template
 5from webhelpers.html.builder import literal
 6
 7from minger_plus.lib.base import BaseController
 8
 9class ErrorController(BaseController):
10
11    """Generates error documents as and when they are required.
12
13    The ErrorDocuments middleware forwards to ErrorController when error
14    related status codes are returned from the application.
15
16    This behaviour can be altered by changing the parameters to the
17    ErrorDocuments middleware in your config/middleware.py file.
18
19    """
20
21    def document(self):
22        """Render the error document"""
23        request = self._py_object.request
24        resp = request.environ.get('pylons.original_response')
25        content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
26        page = error_document_template % \
27            dict(prefix=request.environ.get('SCRIPT_NAME', ''),
28                 code=cgi.escape(request.GET.get('code', str(resp.status_int))),
29                 message=content)
30        return page
31
32    def img(self, id):
33        """Serve Pylons' stock images"""
34        return self._serve_file('/'.join(['media/img', id]))
35
36    def style(self, id):
37        """Serve Pylons' stock stylesheets"""
38        return self._serve_file('/'.join(['media/style', id]))
39
40    def _serve_file(self, path):
41        """Call Paste's FileApp (a WSGI application) to serve the file
42        at the specified path
43        """
44        request = self._py_object.request
45        request.environ['PATH_INFO'] = '/%s' % path
46        return PkgResourcesParser('pylons', 'pylons')(request.environ, self.start_response)