3
3
import itertools
4
4
import logging
5
5
import os
6
- from urllib .parse import urlparse , urlunparse
6
+ from urllib .parse import urlparse
7
7
8
8
from django .conf import settings
9
9
from django .core .files .storage import get_storage_class
10
- from django .http import Http404 , HttpResponse , HttpResponseRedirect , HttpResponsePermanentRedirect
10
+ from django .http import Http404 , HttpResponse , HttpResponseRedirect
11
11
from django .shortcuts import render
12
12
from django .urls import resolve as url_resolve
13
13
from django .utils .decorators import method_decorator
20
20
from readthedocs .projects import constants
21
21
from readthedocs .projects .templatetags .projects_tags import sort_version_aware
22
22
23
- from .mixins import ServeDocsMixin
23
+ from .mixins import ServeDocsMixin , ServeRedirectMixin
24
24
25
25
from .decorators import map_project_slug
26
26
from .redirects import redirect_project_slug
30
30
log = logging .getLogger (__name__ ) # noqa
31
31
32
32
33
- class ServeDocsBase (ServeDocsMixin , View ):
33
+ class ServeDocsBase (ServeRedirectMixin , ServeDocsMixin , View ):
34
34
35
35
def get (self ,
36
36
request ,
@@ -77,29 +77,16 @@ def get(self,
77
77
)
78
78
raise Http404 ('Invalid URL for project with versions' )
79
79
80
- full_path = request .path
81
- redirect_path , http_status = final_project .redirects .get_redirect_path_with_status (
82
- language = lang_slug , version_slug = version_slug , path = filename , full_path = full_path ,
83
- )
84
- if redirect_path is not None :
85
- schema , netloc , path , params , query , fragments = urlparse (full_path )
86
- new_path = urlunparse ((schema , netloc , redirect_path , params , query , fragments ))
87
-
88
- # Re-use the domain and protocol used in the current request.
89
- # Redirects shouldn't change the domain, version or language.
90
- # However, if the new_path is already an absolute URI, just use it
91
- new_path = request .build_absolute_uri (new_path )
92
- log .info (
93
- 'Redirecting: from=%s to=%s http_status=%s' ,
94
- request .build_absolute_uri (),
95
- new_path ,
96
- http_status ,
97
- )
98
-
99
- if http_status and http_status == 301 :
100
- return HttpResponsePermanentRedirect (new_path )
101
-
102
- return HttpResponseRedirect (new_path )
80
+ # TODO: un-comment when ready to perform redirect here
81
+ # redirect_path, http_status = self.get_redirect(
82
+ # final_project,
83
+ # lang_slug,
84
+ # version_slug,
85
+ # filename,
86
+ # request.path,
87
+ # )
88
+ # if redirect_path and http_status:
89
+ # return self.get_redirect_response(request, redirect_path, http_status)
103
90
104
91
# Check user permissions and return an unauthed response if needed
105
92
if not self .allowed_user (request , final_project , version_slug ):
@@ -125,7 +112,7 @@ class ServeDocs(SettingsOverrideObject):
125
112
_default_class = ServeDocsBase
126
113
127
114
128
- class ServeError404Base (View ):
115
+ class ServeError404Base (ServeRedirectMixin , View ):
129
116
130
117
def get (self , request , proxito_path , template_name = '404.html' ):
131
118
"""
@@ -156,6 +143,17 @@ def get(self, request, proxito_path, template_name='404.html'):
156
143
filename = kwargs .get ('filename' , '' ),
157
144
)
158
145
146
+ # Check and perform redirects on 404 handler
147
+ redirect_path , http_status = self .get_redirect (
148
+ final_project ,
149
+ lang_slug ,
150
+ version_slug ,
151
+ filename ,
152
+ request .path ,
153
+ )
154
+ if redirect_path and http_status :
155
+ return self .get_redirect_response (request , redirect_path , http_status )
156
+
159
157
storage_root_path = final_project .get_storage_path (
160
158
type_ = 'html' ,
161
159
version_slug = version_slug ,
0 commit comments