Show More
@@ -1,92 +1,96 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 | import cgi |
|
2 | import cgi | |
3 | import os |
|
3 | import os | |
4 | import paste.fileapp |
|
4 | import paste.fileapp | |
5 | from pylons import tmpl_context as c, app_globals as g, request, config |
|
5 | from pylons import tmpl_context as c, app_globals as g, request, config | |
6 | from pylons.controllers.util import forward |
|
6 | from pylons.controllers.util import forward | |
7 | from pylons.i18n.translation import _ |
|
7 | from pylons.i18n.translation import _ | |
8 | from pylons_app.lib.base import BaseController, render |
|
8 | from pylons_app.lib.base import BaseController, render | |
9 | from pylons.middleware import media_path |
|
9 | from pylons.middleware import media_path | |
10 | from pylons_app.lib.utils import check_repo |
|
10 | from pylons_app.lib.utils import check_repo | |
11 | import pylons_app.lib.helpers as h |
|
11 | import pylons_app.lib.helpers as h | |
12 | from pylons_app import __version__ |
|
12 | from pylons_app import __version__ | |
13 | log = logging.getLogger(__name__) |
|
13 | log = logging.getLogger(__name__) | |
14 |
|
14 | |||
15 | class ErrorController(BaseController): |
|
15 | class ErrorController(BaseController): | |
16 | """ |
|
16 | """ | |
17 | Generates error documents as and when they are required. |
|
17 | Generates error documents as and when they are required. | |
18 |
|
18 | |||
19 | The ErrorDocuments middleware forwards to ErrorController when error |
|
19 | The ErrorDocuments middleware forwards to ErrorController when error | |
20 | related status codes are returned from the application. |
|
20 | related status codes are returned from the application. | |
21 |
|
21 | |||
22 | This behaviour can be altered by changing the parameters to the |
|
22 | This behaviour can be altered by changing the parameters to the | |
23 | ErrorDocuments middleware in your config/middleware.py file. |
|
23 | ErrorDocuments middleware in your config/middleware.py file. | |
24 | """ |
|
24 | """ | |
25 | def __before__(self): |
|
25 | def __before__(self): | |
26 | pass#disable all base actions since we don't need them here |
|
26 | pass#disable all base actions since we don't need them here | |
27 |
|
27 | |||
28 | def document(self): |
|
28 | def document(self): | |
29 | resp = request.environ.get('pylons.original_response') |
|
29 | resp = request.environ.get('pylons.original_response') | |
30 |
|
30 | |||
31 | log.debug('### %s ###', resp.status) |
|
31 | log.debug('### %s ###', resp.status) | |
32 |
|
32 | |||
33 | e = request.environ |
|
33 | e = request.environ | |
34 | c.serv_p = r'%(protocol)s://%(host)s/' % { |
|
34 | c.serv_p = r'%(protocol)s://%(host)s/' % { | |
35 | 'protocol': e.get('wsgi.url_scheme'), |
|
35 | 'protocol': e.get('wsgi.url_scheme'), | |
36 | 'host':e.get('HTTP_HOST'), |
|
36 | 'host':e.get('HTTP_HOST'), | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | if resp.status_int == 404: |
|
39 | if resp.status_int == 404: | |
40 | org_e = request.environ.get('pylons.original_request').environ |
|
40 | org_e = request.environ.get('pylons.original_request').environ | |
41 | c.repo_name = repo_name = org_e['PATH_INFO'].split('/')[1] |
|
41 | try: | |
|
42 | c.repo_name = org_e['PATH_INFO'].split('/')[1] | |||
|
43 | except IndexError: | |||
|
44 | c.repo_name = '' | |||
|
45 | ||||
42 | c.hg_app_version = __version__ |
|
46 | c.hg_app_version = __version__ | |
43 | c.repo_name_cleaned = h.repo_name_slug(c.repo_name) |
|
47 | c.repo_name_cleaned = h.repo_name_slug(c.repo_name) | |
44 | if check_repo(repo_name, g.base_path): |
|
48 | if check_repo(c.repo_name, g.base_path): | |
45 | return render('/errors/error_404.html') |
|
49 | return render('/errors/error_404.html') | |
46 |
|
50 | |||
47 | c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) |
|
51 | c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) | |
48 | c.error_explanation = self.get_error_explanation(resp.status_int) |
|
52 | c.error_explanation = self.get_error_explanation(resp.status_int) | |
49 |
|
53 | |||
50 | #redirect to when error with given seconds |
|
54 | #redirect to when error with given seconds | |
51 | c.redirect_time = 0 |
|
55 | c.redirect_time = 0 | |
52 | c.redirect_module = _('Home page')# name to what your going to be redirected |
|
56 | c.redirect_module = _('Home page')# name to what your going to be redirected | |
53 | c.url_redirect = "/" |
|
57 | c.url_redirect = "/" | |
54 |
|
58 | |||
55 | return render('/errors/error_document.html') |
|
59 | return render('/errors/error_document.html') | |
56 |
|
60 | |||
57 |
|
61 | |||
58 | def img(self, id): |
|
62 | def img(self, id): | |
59 | """Serve Pylons' stock images""" |
|
63 | """Serve Pylons' stock images""" | |
60 | return self._serve_file(os.path.join(media_path, 'img', id)) |
|
64 | return self._serve_file(os.path.join(media_path, 'img', id)) | |
61 |
|
65 | |||
62 | def style(self, id): |
|
66 | def style(self, id): | |
63 | """Serve Pylons' stock stylesheets""" |
|
67 | """Serve Pylons' stock stylesheets""" | |
64 | return self._serve_file(os.path.join(media_path, 'style', id)) |
|
68 | return self._serve_file(os.path.join(media_path, 'style', id)) | |
65 |
|
69 | |||
66 | def _serve_file(self, path): |
|
70 | def _serve_file(self, path): | |
67 | """Call Paste's FileApp (a WSGI application) to serve the file |
|
71 | """Call Paste's FileApp (a WSGI application) to serve the file | |
68 | at the specified path |
|
72 | at the specified path | |
69 | """ |
|
73 | """ | |
70 | fapp = paste.fileapp.FileApp(path) |
|
74 | fapp = paste.fileapp.FileApp(path) | |
71 | return fapp(request.environ, self.start_response) |
|
75 | return fapp(request.environ, self.start_response) | |
72 |
|
76 | |||
73 | def get_error_explanation(self, code): |
|
77 | def get_error_explanation(self, code): | |
74 | ''' get the error explanations of int codes |
|
78 | ''' get the error explanations of int codes | |
75 | [400, 401, 403, 404, 500]''' |
|
79 | [400, 401, 403, 404, 500]''' | |
76 | try: |
|
80 | try: | |
77 | code = int(code) |
|
81 | code = int(code) | |
78 | except: |
|
82 | except: | |
79 | code = 500 |
|
83 | code = 500 | |
80 |
|
84 | |||
81 | if code == 400: |
|
85 | if code == 400: | |
82 | return _('The request could not be understood by the server due to malformed syntax.') |
|
86 | return _('The request could not be understood by the server due to malformed syntax.') | |
83 | if code == 401: |
|
87 | if code == 401: | |
84 | return _('Unathorized access to resource') |
|
88 | return _('Unathorized access to resource') | |
85 | if code == 403: |
|
89 | if code == 403: | |
86 | return _("You don't have permission to view this page") |
|
90 | return _("You don't have permission to view this page") | |
87 | if code == 404: |
|
91 | if code == 404: | |
88 | return _('The resource could not be found') |
|
92 | return _('The resource could not be found') | |
89 | if code == 500: |
|
93 | if code == 500: | |
90 | return _('The server encountered an unexpected condition which prevented it from fulfilling the request.') |
|
94 | return _('The server encountered an unexpected condition which prevented it from fulfilling the request.') | |
91 |
|
95 | |||
92 |
|
96 |
General Comments 0
You need to be logged in to leave comments.
Login now