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

Fix #7884 Redirects: 'target page route' with non-RoutablePageMixin pages #7896

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion wagtail/contrib/redirects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def clean(self):

class Meta:
model = Redirect
fields = ('old_path', 'site', 'is_permanent', 'redirect_page', 'redirect_page_route_path', 'redirect_link')
fields = ('old_path', 'site', 'is_permanent', 'redirect_page', 'redirect_link')


class ImportForm(forms.Form):
Expand Down
2 changes: 1 addition & 1 deletion wagtail/contrib/redirects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def link(self):
return base_url
try:
page.resolve_subpage(self.redirect_page_route_path)
except Resolver404:
except (AttributeError, Resolver404):
return base_url
return base_url.rstrip("/") + self.redirect_page_route_path
elif self.redirect_link:
Expand Down
8 changes: 8 additions & 0 deletions wagtail/contrib/redirects/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def test_redirect_to_specific_page_route(self):
title="Routable Page",
live=True,
))
contact_page = Page.objects.get(url_path='/home/contact-us/')

# test redirect with a VALID route path
models.Redirect.add_redirect(old_path='/old-path-one', redirect_to=routable_page, page_route_path='/render-method-test-custom-template/')
Expand All @@ -179,8 +180,15 @@ def test_redirect_to_specific_page_route(self):
# test redirect with an INVALID route path
models.Redirect.add_redirect(old_path='/old-path-two', redirect_to=routable_page, page_route_path='/invalid-route/')
response = self.client.get('/old-path-two/', HTTP_HOST='test.example.com')
# we should still make it to the correct page
self.assertRedirects(response, '/routable-page/', status_code=301, fetch_redirect_response=False)

# test redirect with route path for a non-routable page
models.Redirect.add_redirect(old_path="/old-path-three", redirect_to=contact_page, page_route_path="/route-to-nowhere/")
response = self.client.get('/old-path-three/', HTTP_HOST='test.example.com')
# we should still make it to the correct page
self.assertRedirects(response, "/contact-us/", status_code=301, fetch_redirect_response=False)

def test_redirect_from_any_site(self):
contact_page = Page.objects.get(url_path='/home/contact-us/')
Site.objects.create(hostname='other.example.com', port=80, root_page=contact_page)
Expand Down