comparison pylons_app/controllers/error.py @ 14:923f0e6ab010

change error controller, added handling 404 to mercurial,
author Marcin Kuzminski
date Sat, 20 Feb 2010 22:45:59 +0100
parents 564e40829f80
children 9f6300b96380
comparison
equal deleted inserted replaced
13:b6b3533ad63c 14:923f0e6ab010
1 import logging 1 import logging
2 from paste.urlparser import PkgResourcesParser 2 from paste.urlparser import PkgResourcesParser
3 import paste.fileapp
3 from pylons import request, tmpl_context as c 4 from pylons import request, tmpl_context as c
4 from pylons.controllers.util import forward 5 from pylons.controllers.util import forward
5 from pylons.i18n.translation import _ 6 from pylons.i18n.translation import _
6 from pylons_app.lib.base import BaseController, render 7 from pylons_app.lib.base import BaseController, render
8 from pylons.middleware import error_document_template, media_path
7 import cgi 9 import cgi
10 import os
8 11
9 log = logging.getLogger(__name__) 12 log = logging.getLogger(__name__)
10 class ErrorController(BaseController): 13 class ErrorController(BaseController):
11 """ 14 """
12 Generates error documents as and when they are required. 15 Generates error documents as and when they are required.
15 related status codes are returned from the application. 18 related status codes are returned from the application.
16 19
17 This behaviour can be altered by changing the parameters to the 20 This behaviour can be altered by changing the parameters to the
18 ErrorDocuments middleware in your config/middleware.py file. 21 ErrorDocuments middleware in your config/middleware.py file.
19 """ 22 """
23 #
24 def __before__(self):
25 pass
20 26
21 def document(self): 27 def document(self):
22 28
23 resp = request.environ.get('pylons.original_response') 29 resp = request.environ.get('pylons.original_response')
30 log.debug(resp.status)
24 c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) 31 c.error_message = cgi.escape(request.GET.get('code', str(resp.status)))
25 c.error_explanation = self.get_error_explanation(resp.status_int) 32 c.error_explanation = self.get_error_explanation(resp.status_int)
26 33
27 c.serv_p = ''.join(['http://', request.environ.get('HTTP_HOST', '')]) 34 c.serv_p = ''.join(['http://', request.environ.get('HTTP_HOST', '')])
28 35
29 #redirect to when error with given seconds 36 #redirect to when error with given seconds
30 c.redirect_time = 5 37 c.redirect_time = 0
31 c.redirect_module = _('Home page')# name to what your going to be redirected 38 c.redirect_module = _('Home page')# name to what your going to be redirected
32 c.url_redirect = "/" 39 c.url_redirect = "/"
33 40
34 return render('/errors/error_document.html') 41 return render('/errors/error_document.html')
35 42
43
44 def img(self, id):
45 """Serve Pylons' stock images"""
46 return self._serve_file(os.path.join(media_path, 'img', id))
47
48 def style(self, id):
49 """Serve Pylons' stock stylesheets"""
50 return self._serve_file(os.path.join(media_path, 'style', id))
51
36 def _serve_file(self, path): 52 def _serve_file(self, path):
37 """Call Paste's FileApp (a WSGI application) to serve the file 53 """Call Paste's FileApp (a WSGI application) to serve the file
38 at the specified path 54 at the specified path
39 """ 55 """
40 request.environ['PATH_INFO'] = '/%s' % path 56 fapp = paste.fileapp.FileApp(path)
41 return forward(PkgResourcesParser('pylons', 'pylons')) 57 return fapp(request.environ, self.start_response)
42 58
43 def get_error_explanation(self, code): 59 def get_error_explanation(self, code):
44 ''' get the error explanations of int codes 60 ''' get the error explanations of int codes
45 [400, 401, 403, 404, 500]''' 61 [400, 401, 403, 404, 500]'''
46 try: 62 try: