Mercurial > public > src > rhodecode
annotate pylons_app/controllers/error.py @ 215:70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Mon, 24 May 2010 22:18:15 +0200 |
parents | b68b2246e5a6 |
children | 6ada8c223374 |
rev | line source |
---|---|
0 | 1 import logging |
125
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
2 import cgi |
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
3 import os |
14 | 4 import paste.fileapp |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
5 from pylons import tmpl_context as c, app_globals as g, request, config |
0 | 6 from pylons.controllers.util import forward |
7 from pylons.i18n.translation import _ | |
8 from pylons_app.lib.base import BaseController, render | |
125
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
9 from pylons.middleware import media_path |
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
10 from pylons_app.lib.utils import check_repo |
215
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
11 from pylons_app.lib.filters import clean_repo |
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
12 log = logging.getLogger(__name__) |
0 | 13 |
14 class ErrorController(BaseController): | |
15 """ | |
16 Generates error documents as and when they are required. | |
17 | |
18 The ErrorDocuments middleware forwards to ErrorController when error | |
19 related status codes are returned from the application. | |
20 | |
21 This behaviour can be altered by changing the parameters to the | |
22 ErrorDocuments middleware in your config/middleware.py file. | |
23 """ | |
215
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
24 # def __before__(self): |
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
25 # super(ErrorController, self).__before__() |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
26 |
0 | 27 def document(self): |
28 resp = request.environ.get('pylons.original_response') | |
215
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
29 |
14 | 30 log.debug(resp.status) |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
31 |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
32 e = request.environ |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
33 c.serv_p = r'%(protocol)s://%(host)s/' % { |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
34 'protocol': e.get('wsgi.url_scheme'), |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
35 'host':e.get('HTTP_HOST'), |
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
36 } |
125
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
37 |
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
38 |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
39 if resp.status_int == 404: |
215
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
40 org_e = request.environ.get('pylons.original_request').environ |
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
41 c.repo_name = repo_name = org_e['PATH_INFO'].split('/')[1] |
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
42 c.repo_name_cleaned = clean_repo(c.repo_name) |
70f645fa97cc
Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents:
191
diff
changeset
|
43 if check_repo(repo_name, g.base_path): |
125
2811259dc12d
Moved check_repo function to utils, error controller check for first name in url, for this repo and only prints 404 make repo template if repo does not exists, moded check repo from admin
Marcin Kuzminski <marcin@python-works.com>
parents:
101
diff
changeset
|
44 return render('/errors/error_404.html') |
87
9f6300b96380
Updated error handling, from mercurial to pylons. + added tempalte for 404
Marcin Kuzminski <marcin@python-blog.com>
parents:
14
diff
changeset
|
45 |
0 | 46 c.error_message = cgi.escape(request.GET.get('code', str(resp.status))) |
47 c.error_explanation = self.get_error_explanation(resp.status_int) | |
48 | |
49 #redirect to when error with given seconds | |
14 | 50 c.redirect_time = 0 |
0 | 51 c.redirect_module = _('Home page')# name to what your going to be redirected |
52 c.url_redirect = "/" | |
53 | |
54 return render('/errors/error_document.html') | |
55 | |
14 | 56 |
57 def img(self, id): | |
58 """Serve Pylons' stock images""" | |
59 return self._serve_file(os.path.join(media_path, 'img', id)) | |
60 | |
61 def style(self, id): | |
62 """Serve Pylons' stock stylesheets""" | |
63 return self._serve_file(os.path.join(media_path, 'style', id)) | |
64 | |
0 | 65 def _serve_file(self, path): |
66 """Call Paste's FileApp (a WSGI application) to serve the file | |
67 at the specified path | |
68 """ | |
14 | 69 fapp = paste.fileapp.FileApp(path) |
70 return fapp(request.environ, self.start_response) | |
0 | 71 |
72 def get_error_explanation(self, code): | |
73 ''' get the error explanations of int codes | |
74 [400, 401, 403, 404, 500]''' | |
75 try: | |
76 code = int(code) | |
77 except: | |
78 code = 500 | |
79 | |
80 if code == 400: | |
81 return _('The request could not be understood by the server due to malformed syntax.') | |
82 if code == 401: | |
83 return _('Unathorized access to resource') | |
84 if code == 403: | |
85 return _("You don't have permission to view this page") | |
86 if code == 404: | |
87 return _('The resource could not be found') | |
88 if code == 500: | |
89 return _('The server encountered an unexpected condition which prevented it from fulfilling the request.') | |
90 | |
91 |