mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-24 00:24:48 +00:00
Allow pageurl / slugurl tags to function when request.site is not available
Fixes #3352 Conflicts: CONTRIBUTORS.rst
This commit is contained in:
parent
8f25cd9b92
commit
14f991df14
5 changed files with 44 additions and 4 deletions
|
|
@ -35,6 +35,7 @@ Changelog
|
|||
* Fix: "X minutes ago" timestamps are now marked for translation (Janneke Janssen, Matt Westcott)
|
||||
* Fix: Avoid indexing unsaved field content on `save(update_fields=[...])` operations (Matt Westcott)
|
||||
* Fix: Corrected ordering of arguments passed to ModelAdmin `get_extra_class_names_for_field_col` / `get_extra_attrs_for_field_col` methods (Andy Babic)
|
||||
* `pageurl` / `slugurl` tags now function when request.site is not available (Tobias McNulty, Matt Westcott)
|
||||
|
||||
|
||||
1.8.1 (26.01.2017)
|
||||
|
|
|
|||
|
|
@ -210,6 +210,7 @@ Contributors
|
|||
* Martey Dodoo
|
||||
* David Ray
|
||||
* Alasdair Nicol
|
||||
* Tobias McNulty
|
||||
* Vorlif
|
||||
* Kjartan Sverrisson
|
||||
* LB (Ben Johnston)
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ Bug fixes
|
|||
* "X minutes ago" timestamps are now marked for translation (Janneke Janssen, Matt Westcott)
|
||||
* Avoid indexing unsaved field content on `save(update_fields=[...])` operations (Matt Westcott)
|
||||
* Corrected ordering of arguments passed to ModelAdmin ``get_extra_class_names_for_field_col`` / ``get_extra_attrs_for_field_col`` methods (Andy Babic)
|
||||
* ``pageurl`` / ``slugurl`` tags now function when request.site is not available (Tobias McNulty, Matt Westcott)
|
||||
|
||||
|
||||
Upgrade considerations
|
||||
|
|
|
|||
|
|
@ -18,7 +18,13 @@ def pageurl(context, page):
|
|||
Outputs a page's URL as relative (/foo/bar/) if it's within the same site as the
|
||||
current page, or absolute (http://example.com/foo/bar/) if not.
|
||||
"""
|
||||
return page.relative_url(context['request'].site)
|
||||
try:
|
||||
current_site = context['request'].site
|
||||
except (KeyError, AttributeError):
|
||||
# request.site not available in the current context; fall back on page.url
|
||||
return page.url
|
||||
|
||||
return page.relative_url(current_site)
|
||||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
|
|
@ -26,11 +32,17 @@ def slugurl(context, slug):
|
|||
"""Returns the URL for the page that has the given slug."""
|
||||
page = Page.objects.filter(slug=slug).first()
|
||||
|
||||
if page:
|
||||
return page.relative_url(context['request'].site)
|
||||
else:
|
||||
if not page:
|
||||
return None
|
||||
|
||||
try:
|
||||
current_site = context['request'].site
|
||||
except (KeyError, AttributeError):
|
||||
# request.site not available in the current context; fall back on page.url
|
||||
return page.url
|
||||
|
||||
return page.relative_url(current_site)
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def wagtail_version():
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django import template
|
||||
from django.core.cache import cache
|
||||
from django.http import HttpRequest
|
||||
from django.test import TestCase
|
||||
from django.utils.safestring import SafeText
|
||||
|
||||
|
|
@ -25,6 +27,29 @@ class TestPageUrlTags(TestCase):
|
|||
self.assertContains(response,
|
||||
'<a href="/events/">Back to events index</a>')
|
||||
|
||||
def test_pageurl_without_request_in_context(self):
|
||||
page = Page.objects.get(url_path='/home/events/')
|
||||
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% pageurl page %}">{{ page.title }}</a>''')
|
||||
|
||||
# no 'request' object in context
|
||||
result = tpl.render(template.Context({'page': page}))
|
||||
self.assertIn('<a href="/events/">Events</a>', result)
|
||||
|
||||
# 'request' object in context, but no 'site' attribute
|
||||
result = tpl.render(template.Context({'page': page, 'request': HttpRequest()}))
|
||||
self.assertIn('<a href="/events/">Events</a>', result)
|
||||
|
||||
def test_slugurl_without_request_in_context(self):
|
||||
tpl = template.Template('''{% load wagtailcore_tags %}<a href="{% slugurl 'events' %}">Events</a>''')
|
||||
|
||||
# no 'request' object in context
|
||||
result = tpl.render(template.Context({}))
|
||||
self.assertIn('<a href="/events/">Events</a>', result)
|
||||
|
||||
# 'request' object in context, but no 'site' attribute
|
||||
result = tpl.render(template.Context({'request': HttpRequest()}))
|
||||
self.assertIn('<a href="/events/">Events</a>', result)
|
||||
|
||||
|
||||
class TestSiteRootPathsCache(TestCase):
|
||||
fixtures = ['test.json']
|
||||
|
|
|
|||
Loading…
Reference in a new issue