misc: prefer latest match when looking for skeleton (#38899)

This commit is contained in:
Frédéric Péters 2020-01-11 14:27:07 +01:00
parent dea9a0484a
commit f17553257c
2 changed files with 19 additions and 2 deletions

View File

@ -246,7 +246,8 @@ def skeleton(request):
selected_page = None
same_domain_pages = []
# look in redirect pages after the best match for the source
# look in redirect pages after the best match for the source, in case of
# several exact matches take the latest.
redirect_pages = Page.objects.exclude(redirect_url__isnull=True).exclude(redirect_url='')
for page in redirect_pages:
try:
@ -256,7 +257,7 @@ def skeleton(request):
if not redirect_url:
continue
if source.startswith(redirect_url):
if selected_page is None or len(redirect_url) > len(selected_page.get_redirect_url()):
if selected_page is None or len(redirect_url) >= len(selected_page.get_redirect_url()):
selected_page = page
if urlparse.urlparse(redirect_url).netloc == netloc:

View File

@ -339,6 +339,22 @@ def test_page_skeleton(app):
resp = app.get('/__skeleton__/?source=%s' % quote('http://127.0.0.1:8999/'))
assert resp.headers['x-combo-page-id'] == '__root'
# prefer last match
Page.objects.all().delete()
page = Page(title='Elsewhere', slug='elsewhere', template_name='standard',
redirect_url='http://example.net/foo/')
page.save()
cell = TextCell(page=page, placeholder='footer', text='Foobar1', order=0)
cell.save()
page = Page(title='Elsewhere2', slug='elsewhere2', template_name='standard',
redirect_url='http://example.net/foo/', order=2)
page.save()
cell = TextCell(page=page, placeholder='footer', text='Foobar2', order=0)
cell.save()
resp = app.get('/__skeleton__/?source=%s' % quote('http://example.net/foo/bar'))
assert "Foobar2" in resp.text
def test_subpage_location(app):
Page.objects.all().delete()