Skip to content

Commit

Permalink
Fix pageurl and slugurl handling of situations where request.site is …
Browse files Browse the repository at this point in the history
…null (#5501)
  • Loading branch information
solarissmoke authored and gasman committed Aug 14, 2019
1 parent 7c3418f commit 0ee07dd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Changelog
* Fix: Middleware responses during page preview are now properly returned to the user (Matt Westcott)
* Fix: Default text of page links in rich text uses the public page title rather than the admin display title (Andy Chosak)
* Fix: Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
* Fix: `pageurl` and `slugurl` tags no longer fail when `request.site` is `None` (Samir Shah)


2.6.1 (05.08.2019)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Bug fixes
* Middleware responses during page preview are now properly returned to the user (Matt Westcott)
* Default text of page links in rich text uses the public page title rather than the admin display title (Andy Chosak)
* Specific page permission checks are now enforced when viewing a page revision (Andy Chosak)
* ``pageurl`` and ``slugurl`` tags no longer fail when ``request.site`` is ``None`` (Samir Shah)


Upgrade considerations
Expand Down
10 changes: 8 additions & 2 deletions wagtail/core/templatetags/wagtailcore_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def pageurl(context, page, fallback=None):
# request.site not available in the current context; fall back on page.url
return page.url

if current_site is None:
# request.site is set to None; fall back on page.url
return page.url

# Pass page.relative_url the request object, which may contain a cached copy of
# Site.get_site_root_paths()
# This avoids page.relative_url having to make a database/cache fetch for this list
Expand All @@ -48,13 +52,15 @@ def slugurl(context, slug):
that matches the slug on any site.
"""

page = None
try:
current_site = context['request'].site
except (KeyError, AttributeError):
# No site object found - allow the fallback below to take place.
page = None
pass
else:
page = Page.objects.in_site(current_site).filter(slug=slug).first()
if current_site is not None:
page = Page.objects.in_site(current_site).filter(slug=slug).first()

# If no page is found, fall back to searching the whole tree.
if page is None:
Expand Down
17 changes: 17 additions & 0 deletions wagtail/core/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def test_pageurl_without_request_in_context(self):
result = tpl.render(template.Context({'page': page, 'request': HttpRequest()}))
self.assertIn('<a href="/events/">Events</a>', result)

def test_pageurl_with_null_site_in_request(self):
page = Page.objects.get(url_path='/home/events/')
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page %}">{{ page.title }}</a>''')

# 'request' object in context, but site is None
request = HttpRequest()
request.site = None
result = tpl.render(template.Context({'page': page, 'request': request}))
self.assertIn('<a href="/events/">Events</a>', result)

def test_bad_pageurl(self):
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page %}">{{ page.title }}</a>''')

Expand Down Expand Up @@ -96,6 +106,13 @@ def test_slugurl_without_request_in_context(self):
result = slugurl(template.Context({'request': HttpRequest()}), 'events')
self.assertEqual(result, '/events/')

def test_slugurl_with_null_site_in_request(self):
# 'request' object in context, but site is None
request = HttpRequest()
request.site = None
result = slugurl(template.Context({'request': request}), 'events')
self.assertEqual(result, '/events/')


class TestSiteRootPathsCache(TestCase):
fixtures = ['test.json']
Expand Down

0 comments on commit 0ee07dd

Please sign in to comment.