adapt error controller to new Pylons

This commit is contained in:
Jan Dittberner 2010-04-17 12:05:29 +02:00
parent 794bed92fd
commit 716ca8d40a

View file

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# DDPortfolio service ErrorController # DDPortfolio service ErrorController
# Copyright (c) 2009 Jan Dittberner <jan@dittberner.info> # Copyright © 2009, 2010 Jan Dittberner <jan@dittberner.info>
# #
# This file is part of DDPortfolio service. # This file is part of DDPortfolio service.
# #
@ -21,14 +21,17 @@
# <http://www.gnu.org/licenses/>. # <http://www.gnu.org/licenses/>.
# #
import cgi import cgi
import os.path
from paste.urlparser import StaticURLParser from paste.urlparser import PkgResourcesParser
from pylons.middleware import error_document_template, media_path from pylons import request
from pylons.controllers.util import forward
from pylons.middleware import error_document_template
from webhelpers.html.builder import literal
from ddportfolioservice.lib.base import * from ddportfolioservice.lib.base import BaseController
class ErrorController(BaseController): class ErrorController(BaseController):
"""Generates error documents as and when they are required. """Generates error documents as and when they are required.
The ErrorDocuments middleware forwards to ErrorController when error The ErrorDocuments middleware forwards to ErrorController when error
@ -38,26 +41,28 @@ class ErrorController(BaseController):
ErrorDocuments middleware in your config/middleware.py file. ErrorDocuments middleware in your config/middleware.py file.
""" """
def document(self): def document(self):
"""Render the error document""" """Render the error document"""
resp = request.environ.get('pylons.original_response')
content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
page = error_document_template % \ page = error_document_template % \
dict(prefix=request.environ.get('SCRIPT_NAME', ''), dict(prefix=request.environ.get('SCRIPT_NAME', ''),
code=cgi.escape(request.params.get('code', '')), code=cgi.escape(request.GET.get('code', str(resp.status_int))),
message=cgi.escape(request.params.get('message', ''))) message=content)
return page return page
def img(self, id): def img(self, id):
"""Serve Pylons' stock images""" """Serve Pylons' stock images"""
return self._serve_file(os.path.join(media_path, 'img'), id) return self._serve_file('/'.join(['media/img', id]))
def style(self, id): def style(self, id):
"""Serve Pylons' stock stylesheets""" """Serve Pylons' stock stylesheets"""
return self._serve_file(os.path.join(media_path, 'style'), id) return self._serve_file('/'.join(['media/style', id]))
def _serve_file(self, root, path): def _serve_file(self, path):
"""Call Paste's FileApp (a WSGI application) to serve the file """Call Paste's FileApp (a WSGI application) to serve the file
at the specified path at the specified path
""" """
static = StaticURLParser(root)
request.environ['PATH_INFO'] = '/%s' % path request.environ['PATH_INFO'] = '/%s' % path
return static(request.environ, self.start_response) return forward(PkgResourcesParser('pylons', 'pylons'))