/kai/controllers/error.py

https://bitbucket.org/bbangert/kai/ · Python · 46 lines · 30 code · 4 blank · 12 comment · 2 complexity · 2522960e97a1cf771f3549f672ad8734 MD5 · raw file

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