source: OpenWorkouts-current/ow/views/error.py @ 5ec3a0b

currentfeature/docs
Last change on this file since 5ec3a0b was 5ec3a0b, checked in by borja <borja@…>, 5 years ago

Imported sources from the old python2-only repository:

  • Modified the code so it is python 3.6 compatible
  • Fixed deprecation warnings, pyramid 1.10.x supported now
  • Fixed deprecation warnings about some libraries, like pyramid-simpleform
  • Added pytest-pycodestyle and pytest-flakes for automatic checks on the source code files when running tests.
  • Added default pytest.ini setup to enforce some default parameters when running tests.
  • Cleaned up the code a bit, catched up with tests coverage.
  • Property mode set to 100644
File size: 1.5 KB
Line 
1import logging
2
3from pyramid.view import (
4    view_config,
5    notfound_view_config,
6    forbidden_view_config
7)
8from pyramid.httpexceptions import HTTPFound
9
10log = logging.getLogger(__name__)
11
12
13@notfound_view_config(renderer='ow:templates/404.pt')
14def 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')
25def 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__')
45def 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    }
Note: See TracBrowser for help on using the repository browser.