Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/django/views/defaults.py: 25%
57 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1from urllib.parse import quote
3from django.http import (
4 HttpResponseBadRequest,
5 HttpResponseForbidden,
6 HttpResponseNotFound,
7 HttpResponseServerError,
8)
9from django.template import Context, Engine, TemplateDoesNotExist, loader
10from django.views.decorators.csrf import requires_csrf_token
12ERROR_404_TEMPLATE_NAME = "404.html"
13ERROR_403_TEMPLATE_NAME = "403.html"
14ERROR_400_TEMPLATE_NAME = "400.html"
15ERROR_500_TEMPLATE_NAME = "500.html"
16ERROR_PAGE_TEMPLATE = """
17<!doctype html>
18<html lang="en">
19<head>
20 <title>%(title)s</title>
21</head>
22<body>
23 <h1>%(title)s</h1><p>%(details)s</p>
24</body>
25</html>
26"""
29# These views can be called when CsrfViewMiddleware.process_view() not run,
30# therefore need @requires_csrf_token in case the template needs
31# {% csrf_token %}.
34@requires_csrf_token
35def page_not_found(request, exception, template_name=ERROR_404_TEMPLATE_NAME):
36 """
37 Default 404 handler.
39 Templates: :template:`404.html`
40 Context:
41 request_path
42 The path of the requested URL (e.g., '/app/pages/bad_page/'). It's
43 quoted to prevent a content injection attack.
44 exception
45 The message from the exception which triggered the 404 (if one was
46 supplied), or the exception class name
47 """
48 exception_repr = exception.__class__.__name__
49 # Try to get an "interesting" exception message, if any (and not the ugly
50 # Resolver404 dictionary)
51 try:
52 message = exception.args[0]
53 except (AttributeError, IndexError):
54 pass
55 else:
56 if isinstance(message, str):
57 exception_repr = message
58 context = {
59 "request_path": quote(request.path),
60 "exception": exception_repr,
61 }
62 try:
63 template = loader.get_template(template_name)
64 body = template.render(context, request)
65 content_type = None # Django will use 'text/html'.
66 except TemplateDoesNotExist:
67 if template_name != ERROR_404_TEMPLATE_NAME:
68 # Reraise if it's a missing custom template.
69 raise
70 # Render template (even though there are no substitutions) to allow
71 # inspecting the context in tests.
72 template = Engine().from_string(
73 ERROR_PAGE_TEMPLATE
74 % {
75 "title": "Not Found",
76 "details": "The requested resource was not found on this server.",
77 },
78 )
79 body = template.render(Context(context))
80 content_type = "text/html"
81 return HttpResponseNotFound(body, content_type=content_type)
84@requires_csrf_token
85def server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
86 """
87 500 error handler.
89 Templates: :template:`500.html`
90 Context: None
91 """
92 try:
93 template = loader.get_template(template_name)
94 except TemplateDoesNotExist:
95 if template_name != ERROR_500_TEMPLATE_NAME:
96 # Reraise if it's a missing custom template.
97 raise
98 return HttpResponseServerError(
99 ERROR_PAGE_TEMPLATE % {"title": "Server Error (500)", "details": ""},
100 content_type="text/html",
101 )
102 return HttpResponseServerError(template.render())
105@requires_csrf_token
106def bad_request(request, exception, template_name=ERROR_400_TEMPLATE_NAME):
107 """
108 400 error handler.
110 Templates: :template:`400.html`
111 Context: None
112 """
113 try:
114 template = loader.get_template(template_name)
115 except TemplateDoesNotExist:
116 if template_name != ERROR_400_TEMPLATE_NAME:
117 # Reraise if it's a missing custom template.
118 raise
119 return HttpResponseBadRequest(
120 ERROR_PAGE_TEMPLATE % {"title": "Bad Request (400)", "details": ""},
121 content_type="text/html",
122 )
123 # No exception content is passed to the template, to not disclose any
124 # sensitive information.
125 return HttpResponseBadRequest(template.render())
128@requires_csrf_token
129def permission_denied(request, exception, template_name=ERROR_403_TEMPLATE_NAME):
130 """
131 Permission denied (403) handler.
133 Templates: :template:`403.html`
134 Context:
135 exception
136 The message from the exception which triggered the 403 (if one was
137 supplied).
139 If the template does not exist, an Http403 response containing the text
140 "403 Forbidden" (as per RFC 7231) will be returned.
141 """
142 try:
143 template = loader.get_template(template_name)
144 except TemplateDoesNotExist:
145 if template_name != ERROR_403_TEMPLATE_NAME:
146 # Reraise if it's a missing custom template.
147 raise
148 return HttpResponseForbidden(
149 ERROR_PAGE_TEMPLATE % {"title": "403 Forbidden", "details": ""},
150 content_type="text/html",
151 )
152 return HttpResponseForbidden(
153 template.render(request=request, context={"exception": str(exception)})
154 )