From 6f6f9b8e469881b21bea22bd68ddb4ab587083e9 Mon Sep 17 00:00:00 2001 From: Arthur Holzner Date: Tue, 12 Jun 2018 14:03:52 +0200 Subject: [PATCH] Added fallback to pageurl (#4603) --- CHANGELOG.txt | 1 + docs/releases/2.5.rst | 1 + docs/topics/writing_templates.rst | 18 +++++++++++++++++- wagtail/core/templatetags/wagtailcore_tags.py | 7 ++++++- wagtail/core/tests/tests.py | 11 +++++++++++ wagtail/tests/urls.py | 3 +++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 7a645a06b5..d5ed22b345 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -18,6 +18,7 @@ Changelog * Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott) * Add a Django setting `TAG_LIMIT` to limit number of tags that can be added to any taggit model (Mani) * Added instructions on how to generate urls for `ModelAdmin` to documentation (LB (Ben Johnston), Andy Babic) + * Added option to specify a fallback URL on `{% pageurl %}` (Arthur Holzner) * Fix: Set `SERVER_PORT` to 443 in `Page.dummy_request()` for HTTPS sites (Sergey Fedoseev) * Fix: Include port number in `Host` header of `Page.dummy_request()` (Sergey Fedoseev) * Fix: Validation error messages in `InlinePanel` no longer count towards `max_num` when disabling the 'add' button (Todd Dembrey, Thibaud Colas) diff --git a/docs/releases/2.5.rst b/docs/releases/2.5.rst index d4eede6504..e8a87d9bdd 100644 --- a/docs/releases/2.5.rst +++ b/docs/releases/2.5.rst @@ -28,6 +28,7 @@ Other features * Add Learn Wagtail to third-party tutorials in documentation (Matt Westcott) * Add a Django setting ``TAG_LIMIT`` to limit number of tags that can be added to any taggit model (Mani) * Added instructions on how to generate urls for ``ModelAdmin`` to documentation (LB (Ben Johnston), Andy Babic) + * Added option to specify a fallback URL on ``{% pageurl %}`` (Arthur Holzner) Bug fixes diff --git a/docs/topics/writing_templates.rst b/docs/topics/writing_templates.rst index df7edb91f3..1170c46a60 100644 --- a/docs/topics/writing_templates.rst +++ b/docs/topics/writing_templates.rst @@ -160,7 +160,23 @@ Takes a Page object and returns a relative URL (``/foo/bar/``) if within the sam {% load wagtailcore_tags %} ... - + Back to index + + +A ``fallback`` keyword argument can be provided - this should be a URL route name that takes no parameters, and will be used as a substitute URL when the passed page is ``None``. + +.. code-block:: html+django + + {% load wagtailcore_tags %} + + {% for publication in page.related_publications.all %} +
  • + + {{ publication.title }} + +
  • + {% endfor %} + .. _slugurl_tag: diff --git a/wagtail/core/templatetags/wagtailcore_tags.py b/wagtail/core/templatetags/wagtailcore_tags.py index dda1cf62d8..a50cf0668a 100644 --- a/wagtail/core/templatetags/wagtailcore_tags.py +++ b/wagtail/core/templatetags/wagtailcore_tags.py @@ -1,4 +1,5 @@ from django import template +from django.shortcuts import reverse from django.template.defaulttags import token_kwargs from django.utils.encoding import force_text from django.utils.safestring import mark_safe @@ -12,11 +13,15 @@ register = template.Library() @register.simple_tag(takes_context=True) -def pageurl(context, page): +def pageurl(context, page, fallback=None): """ 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. + If kwargs contains a fallback view name and page is None, the fallback view url will be returned. """ + if page is None and fallback: + return reverse(fallback) + if not hasattr(page, 'relative_url'): raise ValueError("pageurl tag expected a Page object, got %r" % page) diff --git a/wagtail/core/tests/tests.py b/wagtail/core/tests/tests.py index 61507fe895..20001ca12c 100644 --- a/wagtail/core/tests/tests.py +++ b/wagtail/core/tests/tests.py @@ -2,6 +2,7 @@ from django import template from django.core.cache import cache from django.http import HttpRequest from django.test import TestCase +from django.urls.exceptions import NoReverseMatch from django.utils.safestring import SafeText from wagtail.core.models import Page, Site @@ -19,6 +20,16 @@ class TestPageUrlTags(TestCase): self.assertContains(response, 'Christmas') + def test_pageurl_fallback(self): + tpl = template.Template('''{% load wagtailcore_tags %}Fallback''') + result = tpl.render(template.Context({'page': None})) + self.assertIn('Fallback', result) + + def test_pageurl_fallback_without_valid_fallback(self): + tpl = template.Template('''{% load wagtailcore_tags %}Fallback''') + with self.assertRaises(NoReverseMatch): + tpl.render(template.Context({'page': None})) + def test_slugurl_tag(self): response = self.client.get('/events/christmas/') self.assertEqual(response.status_code, 200) diff --git a/wagtail/tests/urls.py b/wagtail/tests/urls.py index 878f47e679..23c3d45633 100644 --- a/wagtail/tests/urls.py +++ b/wagtail/tests/urls.py @@ -1,4 +1,5 @@ from django.conf.urls import include, url +from django.http import HttpResponse from wagtail.admin import urls as wagtailadmin_urls from wagtail.api.v2.endpoints import PagesAPIEndpoint @@ -37,6 +38,8 @@ urlpatterns = [ url(r'^testapp/', include(testapp_urls)), + url(r'^fallback/', lambda: HttpResponse('ok'), name='fallback'), + # For anything not caught by a more specific rule above, hand over to # Wagtail's serving mechanism url(r'', include(wagtail_urls)),