forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
101 lines (75 loc) · 2.92 KB
/
__init__.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
"""
Core views.
Including the main homepage, documentation and header rendering,
and server errors.
"""
import structlog
from django.conf import settings
from django.http import JsonResponse
from django.shortcuts import render
from django.views.generic import TemplateView, View
from readthedocs.core.mixins import PrivateViewMixin
from readthedocs.projects.models import Project
log = structlog.get_logger(__name__)
class NoProjectException(Exception):
pass
class HealthCheckView(View):
def get(self, request, *args, **kwargs):
return JsonResponse({'status': 200}, status=200)
class HomepageView(TemplateView):
template_name = 'homepage.html'
def get_context_data(self, **kwargs):
"""Add latest builds and featured projects."""
context = super().get_context_data(**kwargs)
context['featured_list'] = Project.objects.filter(featured=True)
return context
class SupportView(PrivateViewMixin, TemplateView):
template_name = 'support/index.html'
def get_context_data(self, **kwargs):
"""Pass along endpoint for support form."""
context = super().get_context_data(**kwargs)
context['SUPPORT_FORM_ENDPOINT'] = settings.SUPPORT_FORM_ENDPOINT
return context
def server_error_404(
request, template_name="errors/404/community.html", exception=None
):
"""A simple 404 handler."""
# This property is set by ProxitoHttp404. We could also have a look at the
# subproject_slug
project_slug = getattr(exception, "project_slug", None)
log.debug(exception)
project = getattr(exception, "project", None)
subproject_slug = getattr(exception, "subproject_slug", None)
if subproject_slug:
template_name = "errors/404/no_subproject.html"
elif project:
template_name = "errors/404/no_project_page.html"
elif project_slug:
template_name = "errors/404/no_project.html"
r = render(
request,
template_name,
context={"project": project, "project_slug": project_slug},
)
r.status_code = 404
return r
def server_error_500(request, template_name='500.html'):
"""A simple 500 handler so we get media."""
r = render(request, template_name)
r.status_code = 500
return r
def do_not_track(request):
dnt_header = request.headers.get("Dnt")
# https://w3c.github.io/dnt/drafts/tracking-dnt.html#status-representation
return JsonResponse( # pylint: disable=redundant-content-type-for-json-response
{
'policy': 'https://docs.readthedocs.io/en/latest/privacy-policy.html',
'same-party': [
'readthedocs.org',
'readthedocs.com',
'readthedocs.io', # .org Documentation Sites
'readthedocs-hosted.com', # .com Documentation Sites
],
'tracking': 'N' if dnt_header == '1' else 'T',
}, content_type='application/tracking-status+json',
)