Skip to content

Fix exact redirects. #3965

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

Merged
merged 3 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions readthedocs/redirects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,15 @@ def redirect_page(self, path, language=None, version_slug=None):
version_slug=version_slug)
return to

def redirect_exact(self, path, **__):
if path == self.from_url:
def redirect_exact(self, path, language=None, version_slug=None):
if language and version_slug:
# reconstruct the full path for an exact redirect
full_path = '/{language}/{version_slug}/{path}'.format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better here to use self.get_full_path which uses our resolve_path and handle more cases?

language=language,
version_slug=version_slug,
path=path.lstrip('/'),
)
if full_path == self.from_url:
log.debug('Redirecting %s', self)
return self.to_url
# Handle full sub-level redirects
Expand Down
11 changes: 11 additions & 0 deletions readthedocs/rtd_tests/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def test_redirect_page(self):
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html')

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_exact(self):
Redirect.objects.create(
project=self.pip, redirect_type='exact',
from_url='/en/latest/install.html', to_url='/en/latest/tutorial/install.html'
)
r = self.client.get('/en/latest/install.html', HTTP_HOST='pip.readthedocs.org')
self.assertEqual(r.status_code, 302)
self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html')

@override_settings(USE_SUBDOMAIN=True)
def test_redirect_keeps_version_number(self):
Redirect.objects.create(
Expand Down