forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexceptions.py
176 lines (127 loc) · 5.25 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from django.http import Http404
from django.utils.translation import pgettext_lazy
_not_found_subject_translation_context = (
"Names a subject that was not found in a 404 error message. Used like "
"'The {{ not_found_subject }} you are looking for at <code>{{ path_not_found }}</code> "
"was not found.'"
)
class ContextualizedHttp404(Http404):
"""
Base class for contextualized HTTP 404 handling.
Subclasses may define their own template name,
HTTP status and object that was not found.
The contextualized exception is handled by proxito's 404 handler
"""
template_name = "errors/404/base.html"
not_found_subject = pgettext_lazy(_not_found_subject_translation_context, "page")
def __init__(self, http_status=404, path_not_found=None, **kwargs):
"""
Constructor that all subclasses should call.
:param kwargs: all kwargs are added as page context for rendering the 404 template
:param http_status: 404 view should respect this and set the HTTP status.
:param path_not_found: Inform the template and 404 view about a different path from
request.path
"""
self.http_status = http_status
self.path_not_found = path_not_found
self.kwargs = kwargs
def get_context(self):
c = {
"not_found_subject": self.not_found_subject,
"path_not_found": self.path_not_found,
}
c.update(self.kwargs)
return c
class DomainDNSHttp404(ContextualizedHttp404):
"""Raised if a DNS record points to us and we don't know the domain."""
template_name = "errors/404/dns.html"
not_found_subject = pgettext_lazy(
_not_found_subject_translation_context, "matching DNS record"
)
def __init__(self, domain, **kwargs):
"""
Raised when DNS for a custom domain is bad.
:param domain: The domain for which DNS is misconfigured.
:param kwargs:
"""
kwargs["domain"] = domain
super().__init__(**kwargs)
class ProjectHttp404(ContextualizedHttp404):
"""
Raised if a domain did not resolve to a project.
This is currently used very broadly.
It indicates a number of reasons for the user.
"""
template_name = "errors/404/no_project.html"
not_found_subject = pgettext_lazy(_not_found_subject_translation_context, "project")
def __init__(self, domain, **kwargs):
"""
Raised when a project wasn't found for a given domain.
:param domain: The domain (custom and hosted) that was not found.
:param kwargs:
"""
kwargs["domain"] = domain
super().__init__(**kwargs)
class SubprojectHttp404(ContextualizedHttp404):
"""Raised if a subproject was not found."""
template_name = "errors/404/no_subproject.html"
not_found_subject = pgettext_lazy(
"Names an object not found in a 404 error", "subproject"
)
def __init__(self, project, **kwargs):
"""
Raised if a subproject was not found.
:param project: The project in which the subproject could not be found
:param kwargs: Context dictionary of the rendered template
"""
kwargs["project"] = project
super().__init__(**kwargs)
class ProjectFilenameHttp404(ContextualizedHttp404):
"""Raised if a page inside an existing project was not found."""
template_name = "errors/404/no_project_page.html"
not_found_subject = pgettext_lazy(
_not_found_subject_translation_context, "documentation page"
)
def __init__(self, project, **kwargs):
"""
Raised if a page inside an existing project was not found.
:param project: The project in which the file could not be found
:param kwargs: Context dictionary of the rendered template
"""
kwargs["project"] = project
super().__init__(**kwargs)
class ProjectTranslationHttp404(ContextualizedHttp404):
"""
Raised if a translation of a project was not found.
This means that the project does not exist for requested language.
If a page isn't found, raise a ProjectPageHttp404.
"""
template_name = "errors/404/no_language.html"
not_found_subject = pgettext_lazy(
"Names an object not found in a 404 error", "translation"
)
def __init__(self, project, **kwargs):
"""
Raised if a translation of a project was not found.
:param project: The project in which the translation could not be found
:param kwargs: Context dictionary of the rendered template
"""
kwargs["project"] = project
super().__init__(**kwargs)
class ProjectVersionHttp404(ContextualizedHttp404):
"""
Raised if a version was not found.
Note: The containing project can be a subproject.
"""
template_name = "errors/404/no_version.html"
not_found_subject = pgettext_lazy(
_not_found_subject_translation_context, "documentation version"
)
def __init__(self, project, **kwargs):
"""
Raised if a version was not found.
:param project: The project in which the version could not be found
:param kwargs: Context dictionary of the rendered template
"""
kwargs["project"] = project
super().__init__(**kwargs)