From f17553257ce2e769f9b5292fc20c973875145a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 11 Jan 2020 14:27:07 +0100 Subject: [PATCH] misc: prefer latest match when looking for skeleton (#38899) --- combo/public/views.py | 5 +++-- tests/test_public.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/combo/public/views.py b/combo/public/views.py index db6a0280..a2303488 100644 --- a/combo/public/views.py +++ b/combo/public/views.py @@ -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: diff --git a/tests/test_public.py b/tests/test_public.py index f7375a51..14d9a4f7 100644 --- a/tests/test_public.py +++ b/tests/test_public.py @@ -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()