42
42
43
43
44
44
class ServePageRedirect (CDNCacheControlMixin , ServeRedirectMixin , ServeDocsMixin , View ):
45
+
46
+ """
47
+ Page redirect view.
48
+
49
+ This allows users to redirec to the default version of a project.
50
+ For example:
51
+
52
+ - /page/api/index.html -> /en/latest/api/index.html
53
+ - /projects/subproject/page/index.html -> /projects/subproject/en/latest/api/index.html
54
+ """
55
+
45
56
def get (self , request , subproject_slug = None , filename = "" ):
57
+ """Handle all page redirects."""
46
58
47
59
unresolved_domain = request .unresolved_domain
48
60
project = unresolved_domain .project
@@ -73,6 +85,14 @@ def get(self, request, subproject_slug=None, filename=""):
73
85
74
86
75
87
class ServeDocsBase (CDNCacheControlMixin , ServeRedirectMixin , ServeDocsMixin , View ):
88
+
89
+ """
90
+ Serve docs view.
91
+
92
+ This view serves all the documentation pages,
93
+ and handles canonical redirects.
94
+ """
95
+
76
96
def get (
77
97
self ,
78
98
request ,
@@ -89,6 +109,7 @@ def get(
89
109
``subproject_slash`` is used to determine if the subproject URL has a slash,
90
110
so that we can decide if we need to serve docs or add a /.
91
111
"""
112
+ # pylint: disable=too-many-locals
92
113
unresolved_domain = request .unresolved_domain
93
114
# Handle requests that need canonicalizing first,
94
115
# e.g. HTTP -> HTTPS, redirect to canonical domain, etc.
@@ -282,6 +303,7 @@ def _get_canonical_redirect_type(self, request):
282
303
.exists ()
283
304
)
284
305
# For .com we need to check if the project supports custom domains.
306
+ # pylint: disable=protected-access
285
307
if canonical_domain and resolver ._use_cname (project ):
286
308
log .debug (
287
309
"Proxito Public Domain -> Canonical Domain Redirect." ,
@@ -442,9 +464,15 @@ class ServeDocs(SettingsOverrideObject):
442
464
_default_class = ServeDocsBase
443
465
444
466
445
- class ServeError404Base (ServeRedirectMixin , ServeDocsMixin , View ):
467
+ class ServeError404Base (CDNCacheControlMixin , ServeRedirectMixin , ServeDocsMixin , View ):
468
+
469
+ """
470
+ Proxito handler for 404 pages.
446
471
447
- def get (self , request , proxito_path , template_name = '404.html' ):
472
+ This view is called by an internal nginx redirect when there is a 404.
473
+ """
474
+
475
+ def get (self , request , proxito_path ):
448
476
"""
449
477
Handler for 404 pages on subdomains.
450
478
@@ -460,6 +488,7 @@ def get(self, request, proxito_path, template_name='404.html'):
460
488
with the default version and finally, if none of them are found, the Read
461
489
the Docs default page (Maze Found) is rendered by Django and served.
462
490
"""
491
+ # pylint: disable=too-many-locals
463
492
log .bind (proxito_path = proxito_path )
464
493
log .debug ('Executing 404 handler.' )
465
494
@@ -798,10 +827,14 @@ class ServeError404(SettingsOverrideObject):
798
827
_default_class = ServeError404Base
799
828
800
829
801
- class ServeRobotsTXTBase (ServeDocsMixin , View ):
830
+ class ServeRobotsTXTBase (CDNCacheControlMixin , CDNCacheTagsMixin , ServeDocsMixin , View ):
831
+
832
+ """Serve robots.txt from the domain's root."""
802
833
803
834
# Always cache this view, since it's the same for all users.
804
835
cache_response = True
836
+ # Extra cache tag to invalidate only this view if needed.
837
+ project_cache_tag = "robots.txt"
805
838
806
839
def get (self , request ):
807
840
"""
@@ -822,7 +855,7 @@ def get(self, request):
822
855
)
823
856
824
857
# Verify if the project is marked as spam and return a custom robots.txt
825
- elif "readthedocsext.spamfighting" in settings .INSTALLED_APPS :
858
+ if "readthedocsext.spamfighting" in settings .INSTALLED_APPS :
826
859
from readthedocsext .spamfighting .utils import is_robotstxt_denied # noqa
827
860
if is_robotstxt_denied (project ):
828
861
return render (
@@ -894,15 +927,30 @@ def _get_hidden_paths(self, project):
894
927
]
895
928
return hidden_paths
896
929
930
+ def _get_project (self ):
931
+ # Method used by the CDNCacheTagsMixin class.
932
+ return self .request .unresolved_domain .project
933
+
934
+ def _get_version (self ):
935
+ # Method used by the CDNCacheTagsMixin class.
936
+ # This view isn't explicitly mapped to a version,
937
+ # but it can be when we serve a custom robots.txt file.
938
+ # TODO: refactor how we set cache tags to avoid this.
939
+ return None
940
+
897
941
898
942
class ServeRobotsTXT (SettingsOverrideObject ):
899
943
_default_class = ServeRobotsTXTBase
900
944
901
945
902
- class ServeSitemapXMLBase (View ):
946
+ class ServeSitemapXMLBase (CDNCacheControlMixin , CDNCacheTagsMixin , View ):
947
+
948
+ """Serve sitemap.xml from the domain's root."""
903
949
904
950
# Always cache this view, since it's the same for all users.
905
951
cache_response = True
952
+ # Extra cache tag to invalidate only this view if needed.
953
+ project_cache_tag = "sitemap.xml"
906
954
907
955
def get (self , request ):
908
956
"""
@@ -1034,6 +1082,16 @@ def changefreqs_generator():
1034
1082
content_type = 'application/xml' ,
1035
1083
)
1036
1084
1085
+ def _get_project (self ):
1086
+ # Method used by the CDNCacheTagsMixin class.
1087
+ return self .request .unresolved_domain .project
1088
+
1089
+ def _get_version (self ):
1090
+ # Method used by the CDNCacheTagsMixin class.
1091
+ # This view isn't explicitly mapped to a version,
1092
+ # TODO: refactor how we set cache tags to avoid this.
1093
+ return None
1094
+
1037
1095
1038
1096
class ServeSitemapXML (SettingsOverrideObject ):
1039
1097
_default_class = ServeSitemapXMLBase
0 commit comments