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