Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxito redirects: pass full_path instead of re-creating it. #9557

Merged
merged 9 commits into from
Aug 29, 2022
24 changes: 24 additions & 0 deletions readthedocs/proxito/tests/test_old_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,30 @@ def test_redirect_exact(self):
'http://project.dev.readthedocs.io/en/latest/tutorial/install.html',
)

def test_redirect_exact_looks_like_version(self):
self._storage_exists(
[
"/media/html/project/latest/guides/install.html",
]
)

fixture.get(
Redirect,
project=self.project,
redirect_type="exact",
from_url="/en/versions.json",
to_url="/en/latest/versions.json",
)
r = self.client.get(
"/en/versions.json",
HTTP_HOST="project.dev.readthedocs.io",
)
self.assertEqual(r.status_code, 302)
self.assertEqual(
r["Location"],
"http://project.dev.readthedocs.io/en/latest/versions.json",
)

def test_redirect_exact_with_rest(self):
"""
Exact redirects can have a ``$rest`` in the ``from_url``.
Expand Down
26 changes: 13 additions & 13 deletions readthedocs/redirects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,18 @@ def get_full_path(self, filename, language=None, version_slug=None, allow_crossd
filename=filename,
)

def get_redirect_path(self, path, language=None, version_slug=None):
def get_redirect_path(self, path, full_path=None, language=None, version_slug=None):
method = getattr(
self,
'redirect_{type}'.format(
type=self.redirect_type,
),
)
return method(path, language=language, version_slug=version_slug)
return method(
path, full_path=full_path, language=language, version_slug=version_slug
)

def redirect_prefix(self, path, language=None, version_slug=None):
def redirect_prefix(self, path, full_path, language=None, version_slug=None):
if path.startswith(self.from_url):
log.debug("Redirecting...", redirect=self)
# pep8 and blank don't agree on having a space before :.
Expand All @@ -189,7 +191,7 @@ def redirect_prefix(self, path, language=None, version_slug=None):
)
return to

def redirect_page(self, path, language=None, version_slug=None):
def redirect_page(self, path, full_path, language=None, version_slug=None):
if path == self.from_url:
log.debug('Redirecting...', redirect=self)
to = self.get_full_path(
Expand All @@ -200,11 +202,7 @@ def redirect_page(self, path, language=None, version_slug=None):
)
return to

def redirect_exact(self, path, language=None, version_slug=None):
full_path = path
if language and version_slug:
# reconstruct the full path for an exact redirect
full_path = self.get_full_path(path, language, version_slug, allow_crossdomain=False)
def redirect_exact(self, path, full_path, language=None, version_slug=None):
if full_path == self.from_url:
log.debug('Redirecting...', redirect=self)
return self.to_url
Expand All @@ -215,7 +213,7 @@ def redirect_exact(self, path, language=None, version_slug=None):
cut_path = full_path.replace(match, self.to_url, 1)
return cut_path

def redirect_sphinx_html(self, path, language=None, version_slug=None):
def redirect_sphinx_html(self, path, full_path, language=None, version_slug=None):
for ending in ['/', '/index.html']:
if path.endswith(ending):
log.debug('Redirecting...', redirect=self)
Expand All @@ -228,9 +226,11 @@ def redirect_sphinx_html(self, path, language=None, version_slug=None):
allow_crossdomain=False,
)

def redirect_sphinx_htmldir(self, path, language=None, version_slug=None):
if path.endswith('.html'):
log.debug('Redirecting...', redirect=self)
def redirect_sphinx_htmldir(
self, path, full_path, language=None, version_slug=None
):
if path.endswith(".html"):
log.debug("Redirecting...", redirect=self)
path = path[1:] # Strip leading slash.
to = re.sub('.html$', '/', path)
return self.get_full_path(
Expand Down
1 change: 1 addition & 0 deletions readthedocs/redirects/querysets.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def get_redirect_path_with_status(
for redirect in queryset.select_related('project'):
new_path = redirect.get_redirect_path(
path=normalized_path,
full_path=normalized_full_path,
language=language,
version_slug=version_slug,
)
Expand Down