From 716ca8d40ab825e2a325fab9c0b743109b44ab6b Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 17 Apr 2010 12:05:29 +0200 Subject: [PATCH] adapt error controller to new Pylons --- ddportfolioservice/controllers/error.py | 29 +++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/ddportfolioservice/controllers/error.py b/ddportfolioservice/controllers/error.py index 114f52a..c20d41e 100644 --- a/ddportfolioservice/controllers/error.py +++ b/ddportfolioservice/controllers/error.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # # DDPortfolio service ErrorController -# Copyright (c) 2009 Jan Dittberner +# Copyright © 2009, 2010 Jan Dittberner # # This file is part of DDPortfolio service. # @@ -21,14 +21,17 @@ # . # import cgi -import os.path -from paste.urlparser import StaticURLParser -from pylons.middleware import error_document_template, media_path +from paste.urlparser import PkgResourcesParser +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): + """Generates error documents as and when they are required. The ErrorDocuments middleware forwards to ErrorController when error @@ -38,26 +41,28 @@ class ErrorController(BaseController): ErrorDocuments middleware in your config/middleware.py file. """ + def document(self): """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 % \ dict(prefix=request.environ.get('SCRIPT_NAME', ''), - code=cgi.escape(request.params.get('code', '')), - message=cgi.escape(request.params.get('message', ''))) + code=cgi.escape(request.GET.get('code', str(resp.status_int))), + message=content) return page def img(self, id): """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): """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 at the specified path """ - static = StaticURLParser(root) request.environ['PATH_INFO'] = '/%s' % path - return static(request.environ, self.start_response) + return forward(PkgResourcesParser('pylons', 'pylons'))