From 68b2dc01c260f0b68d6273a4a85cde1796e9879c Mon Sep 17 00:00:00 2001 From: Coen van der Kamp Date: Sat, 9 May 2020 20:35:09 +0200 Subject: [PATCH] Render user bar on non page views * resolves #5565 --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/2.10.rst | 1 + docs/topics/writing_templates.rst | 2 + wagtail/admin/templatetags/wagtailuserbar.py | 48 ++++++++++---------- wagtail/admin/tests/test_userbar.py | 8 ++++ 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 38b0f1879f..d0bfdbd51a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -23,6 +23,7 @@ Changelog * Remove sticky footer on small devices, so that content is not blocked and more easily editable (Saeed Tahmasebi) * Add ``alt`` property to ``ImageRenditionField`` api representation (Liam Mullens) * Add ``purge_revisions`` management command to purge old page revisions (Jacob Topp-Mugglestone, Tom Dyson) + * Render the Wagtail User Bar on non ``Page`` views (Caitlin White, Coen van der Kamp) * Fix: Support IPv6 domain (Alex Gleason, Coen van der Kamp) * Fix: Ensure link to add a new user works when no users are visible in the users list (LB (Ben Johnston)) * Fix: `AbstractEmailForm` saved submission fields are now aligned with the email content fields, `form.cleaned_data` will be used instead of `form.fields` (Haydn Greatnews) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 802f0ef582..049468b68b 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -459,6 +459,7 @@ Contributors * Luke Hardwick * Saeed Tahmasebi * Liam Mullens +* Caitlin White Translators =========== diff --git a/docs/releases/2.10.rst b/docs/releases/2.10.rst index 3bd52bedce..9d90a94379 100644 --- a/docs/releases/2.10.rst +++ b/docs/releases/2.10.rst @@ -32,6 +32,7 @@ Other features * Add ability to replace the default Wagtail logo in the userbar, via ``branding_logo`` block (Meteor0id) * Add `alt` property to `ImageRenditionField` api representation (Liam Mullens) * Add `purge_revisions` management command to purge old page revisions (Jacob Topp-Mugglestone, Tom Dyson) + * Render the Wagtail User Bar on non `Page` views (Caitlin White, Coen van der Kamp) Bug fixes diff --git a/docs/topics/writing_templates.rst b/docs/topics/writing_templates.rst index dad3d79324..2934985439 100644 --- a/docs/topics/writing_templates.rst +++ b/docs/topics/writing_templates.rst @@ -242,6 +242,8 @@ Wagtail User Bar This tag provides a contextual flyout menu for logged-in users. The menu gives editors the ability to edit the current page or add a child page, besides the options to show the page in the Wagtail page explorer or jump to the Wagtail admin dashboard. Moderators are also given the ability to accept or reject a page being previewed as part of content moderation. +This tag may be used on regular Django views, without page object. The user bar will contain one item pointing to the admin. + .. code-block:: html+django {% load wagtailuserbar %} diff --git a/wagtail/admin/templatetags/wagtailuserbar.py b/wagtail/admin/templatetags/wagtailuserbar.py index 6c89b72167..2b5d457bc9 100644 --- a/wagtail/admin/templatetags/wagtailuserbar.py +++ b/wagtail/admin/templatetags/wagtailuserbar.py @@ -7,8 +7,6 @@ from wagtail.admin.userbar import ( from wagtail.core import hooks from wagtail.core.models import PAGE_TEMPLATE_VAR, Page, PageRevision -# from django.contrib.auth.decorators import permission_required - register = template.Library() @@ -49,36 +47,36 @@ def wagtailuserbar(context, position='bottom-right'): if getattr(request, 'is_preview', False): return '' - # Only render if the context contains a variable referencing a saved page page = get_page_instance(context) - if page is None: - return '' - - # Dont render anything if the page has not been saved - i.e. a preview - if page.pk is None: - return '' - try: revision_id = request.revision_id except AttributeError: revision_id = None - if revision_id is None: - items = [ - AdminItem(), - ExplorePageItem(Page.objects.get(id=page.id)), - EditPageItem(Page.objects.get(id=page.id)), - AddPageItem(Page.objects.get(id=page.id)), - ] + if page and page.id: + # Saved page. + if revision_id is None: + items = [ + AdminItem(), + ExplorePageItem(Page.objects.get(id=page.id)), + EditPageItem(Page.objects.get(id=page.id)), + AddPageItem(Page.objects.get(id=page.id)), + ] + else: + items = [ + AdminItem(), + ExplorePageItem(PageRevision.objects.get(id=revision_id).page), + EditPageItem(PageRevision.objects.get(id=revision_id).page), + AddPageItem(PageRevision.objects.get(id=revision_id).page), + ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)), + RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)), + ] + elif page: + # A page without an id is a preview. + return "" else: - items = [ - AdminItem(), - ExplorePageItem(PageRevision.objects.get(id=revision_id).page), - EditPageItem(PageRevision.objects.get(id=revision_id).page), - AddPageItem(PageRevision.objects.get(id=revision_id).page), - ApproveModerationEditPageItem(PageRevision.objects.get(id=revision_id)), - RejectModerationEditPageItem(PageRevision.objects.get(id=revision_id)), - ] + # Not a page. + items = [AdminItem()] for fn in hooks.get_hooks('construct_wagtail_userbar'): fn(request, items) diff --git a/wagtail/admin/tests/test_userbar.py b/wagtail/admin/tests/test_userbar.py index b18b171e45..41e7e97f65 100644 --- a/wagtail/admin/tests/test_userbar.py +++ b/wagtail/admin/tests/test_userbar.py @@ -62,6 +62,14 @@ class TestUserbarTag(TestCase): # Make sure nothing was rendered self.assertEqual(content, '') + def test_userbar_tag_no_page(self): + template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}") + content = template.render(Context({ + 'request': self.dummy_request(self.user), + })) + + self.assertIn("", content) + class TestUserbarFrontend(TestCase, WagtailTestUtils): def setUp(self):