From 6664f352eef903a2744b9c81962619e01a33b4b5 Mon Sep 17 00:00:00 2001 From: Eric Holscher Date: Fri, 20 Apr 2018 00:44:15 +0800 Subject: [PATCH] Fix exact redirects. (#3965) * Fix exact redirects. Exact redirects depend on the full path being set against, so that you can do things like redirect versions. This was broken in a previous refactor. * Use `self.get_full_path` instead of recreating the path * Avoid undefined `full_path` --- readthedocs/redirects/models.py | 8 ++++++-- readthedocs/rtd_tests/tests/test_redirects.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/readthedocs/redirects/models.py b/readthedocs/redirects/models.py index f0b3558f8..935a1b250 100644 --- a/readthedocs/redirects/models.py +++ b/readthedocs/redirects/models.py @@ -143,8 +143,12 @@ class Redirect(models.Model): 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): + 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) + if full_path == self.from_url: log.debug('Redirecting %s', self) return self.to_url # Handle full sub-level redirects diff --git a/readthedocs/rtd_tests/tests/test_redirects.py b/readthedocs/rtd_tests/tests/test_redirects.py index 480e6ca62..fed232526 100644 --- a/readthedocs/rtd_tests/tests/test_redirects.py +++ b/readthedocs/rtd_tests/tests/test_redirects.py @@ -147,6 +147,17 @@ class RedirectAppTests(TestCase): 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(