PageRenderTime 147ms CodeModel.GetById 81ms app.highlight 17ms RepoModel.GetById 43ms app.codeStats 0ms

/kai/controllers/error.py

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