1 | import logging |
---|
2 | |
---|
3 | from pyramid.view import ( |
---|
4 | view_config, |
---|
5 | notfound_view_config, |
---|
6 | forbidden_view_config |
---|
7 | ) |
---|
8 | from pyramid.httpexceptions import HTTPFound |
---|
9 | |
---|
10 | log = logging.getLogger(__name__) |
---|
11 | |
---|
12 | |
---|
13 | @notfound_view_config(renderer='ow:templates/404.pt') |
---|
14 | def not_found(request): |
---|
15 | """ |
---|
16 | This is the view that handles 404 (not found) http responses |
---|
17 | """ |
---|
18 | request.response.status_int = 404 |
---|
19 | return { |
---|
20 | 'url': request.resource_url(request.root) |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | @forbidden_view_config(renderer='ow:templates/403.pt') |
---|
25 | def forbidden(request): |
---|
26 | """ |
---|
27 | This is the actual view called for 403 (forbidden) http requests |
---|
28 | If the user is already logged in, show the 403 page, if not, send her |
---|
29 | to the login page |
---|
30 | """ |
---|
31 | if request.authenticated_userid: |
---|
32 | request.response.status_int = 403 |
---|
33 | return { |
---|
34 | 'url': request.resource_url(request.root) |
---|
35 | } |
---|
36 | login_url = request.resource_url( |
---|
37 | request.root, 'login', query=dict(return_to=request.url)) |
---|
38 | return HTTPFound(location=login_url) |
---|
39 | |
---|
40 | |
---|
41 | @view_config( |
---|
42 | context=Exception, |
---|
43 | renderer='ow:templates/500.pt', |
---|
44 | permission='__no_permission_required__') |
---|
45 | def exceptions(context, request): |
---|
46 | """ |
---|
47 | This is the view that handles 500 (Internal Server Error) http responses |
---|
48 | The exceptions are logged to the log file before showing the error page |
---|
49 | """ |
---|
50 | log.error("The error was: %s" % context, exc_info=(context)) |
---|
51 | request.response.status_int = 500 |
---|
52 | return { |
---|
53 | 'url': request.resource_url(request.root) |
---|
54 | } |
---|