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`
remove-default-role
Eric Holscher 2018-04-20 00:44:15 +08:00 committed by Anthony
parent 0efc5e89a0
commit 6664f352ee
2 changed files with 17 additions and 2 deletions

View File

@ -143,8 +143,12 @@ class Redirect(models.Model):
version_slug=version_slug) version_slug=version_slug)
return to return to
def redirect_exact(self, path, **__): def redirect_exact(self, path, language=None, version_slug=None):
if path == self.from_url: 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) log.debug('Redirecting %s', self)
return self.to_url return self.to_url
# Handle full sub-level redirects # Handle full sub-level redirects

View File

@ -147,6 +147,17 @@ class RedirectAppTests(TestCase):
self.assertEqual( self.assertEqual(
r['Location'], 'http://pip.readthedocs.org/en/latest/tutorial/install.html') 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) @override_settings(USE_SUBDOMAIN=True)
def test_redirect_keeps_version_number(self): def test_redirect_keeps_version_number(self):
Redirect.objects.create( Redirect.objects.create(