From 44564ea76aef23f4c9719f54bee8fc22559c70bb Mon Sep 17 00:00:00 2001 From: Tim Heap Date: Fri, 30 Oct 2015 10:32:09 +1100 Subject: [PATCH] Link to homepage from dashboard page link if single site If the Wagtail install has only one site, link to the homepage of that site from the dashboard page summary instead of the root page of the hierarchy. Hopefully this will prevent some of the confusion causing people to create pages under the root page, where they are inaccessible. See #1612 for more information around this issue. Wagtail installs with multiple (or zero) sites retain the old behaviour of linking to the root page. --- CHANGELOG.txt | 1 + docs/releases/1.3.rst | 1 + wagtail/wagtailadmin/site_summary.py | 14 +++++- .../wagtailadmin/home/site_summary_pages.html | 2 +- wagtail/wagtailadmin/tests/tests.py | 44 ++++++++++++++++--- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 943acd5db3..631c5e4473 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -25,6 +25,7 @@ Changelog * Added success message after updating image from the image upload view (Christian Peters) * Added a `request.is_preview` variable for templates to distinguish between previewing and live (Denis Voskvitsov) * New translations for Arabic and Latvian + * 'Pages' link on site stats dashboard now links to the site homepage when only one site exists, rather than the root level * Fix: Images and page revisions created by a user are no longer deleted when the user is deleted (Rich Atkinson) * Fix: HTTP cache purge now works again on Python 2 (Mitchel Cabuloy) * Fix: Locked pages can no longer be unpublished (Alex Bridge) diff --git a/docs/releases/1.3.rst b/docs/releases/1.3.rst index ee19dd7bd2..6179110b5f 100644 --- a/docs/releases/1.3.rst +++ b/docs/releases/1.3.rst @@ -69,6 +69,7 @@ Minor features * Added an optional human-friendly ``site_name`` field to sites (Timo Rieber) * Added success message after updating image from the image upload view (Christian Peters) * Added a ``request.is_preview`` variable for templates to distinguish between previewing and live (Denis Voskvitsov) + * 'Pages' link on site stats dashboard now links to the site homepage when only one site exists, rather than the root level * New translations for Arabic and Latvian diff --git a/wagtail/wagtailadmin/site_summary.py b/wagtail/wagtailadmin/site_summary.py index 1509f3487f..8edde692bb 100644 --- a/wagtail/wagtailadmin/site_summary.py +++ b/wagtail/wagtailadmin/site_summary.py @@ -1,5 +1,5 @@ from wagtail.wagtailcore import hooks -from wagtail.wagtailcore.models import Page +from wagtail.wagtailcore.models import Page, Site from wagtail.utils.compat import render_to_string @@ -21,7 +21,19 @@ class PagesSummaryItem(SummaryItem): template = 'wagtailadmin/home/site_summary_pages.html' def get_context(self): + # If there is a single site, link to the homepage of that site + # Otherwise, if there are multiple sites, link to the root page + try: + site = Site.objects.get() + root = site.root_page + single_site = True + except (Site.DoesNotExist, Site.MultipleObjectsReturned): + root = None + single_site = False + return { + 'single_site': single_site, + 'root_page': root, 'total_pages': Page.objects.count() - 1, # subtract 1 because the root node is not a real page } diff --git a/wagtail/wagtailadmin/templates/wagtailadmin/home/site_summary_pages.html b/wagtail/wagtailadmin/templates/wagtailadmin/home/site_summary_pages.html index f1c70fe515..51d552d68a 100644 --- a/wagtail/wagtailadmin/templates/wagtailadmin/home/site_summary_pages.html +++ b/wagtail/wagtailadmin/templates/wagtailadmin/home/site_summary_pages.html @@ -1,7 +1,7 @@ {% load i18n wagtailadmin_tags %}
  • - + {% blocktrans count counter=total_pages with total_pages|intcomma as total %} {{ total }} Page {% plural %} diff --git a/wagtail/wagtailadmin/tests/tests.py b/wagtail/wagtailadmin/tests/tests.py index 0ef9211182..7526e2b9d1 100644 --- a/wagtail/wagtailadmin/tests/tests.py +++ b/wagtail/wagtailadmin/tests/tests.py @@ -4,16 +4,16 @@ from __future__ import unicode_literals import json -from django.test import TestCase, override_settings -from django.core.urlresolvers import reverse -from django.core import mail from django.contrib.auth import get_user_model - +from django.core import mail +from django.core.urlresolvers import reverse +from django.test import TestCase, override_settings from taggit.models import Tag from wagtail.tests.utils import WagtailTestUtils -from wagtail.wagtailcore.models import Page +from wagtail.wagtailadmin.site_summary import PagesSummaryItem from wagtail.wagtailadmin.utils import send_mail +from wagtail.wagtailcore.models import Page, Site from django.core.urlresolvers import reverse_lazy from wagtail.wagtailadmin.menu import MenuItem @@ -69,6 +69,40 @@ class TestHome(TestCase, WagtailTestUtils): self.assertEqual(response.status_code, 200) +class TestPagesSummary(TestCase, WagtailTestUtils): + def setUp(self): + self.login() + + def get_request(self): + """ + Get a Django WSGI request that has been passed through middleware etc. + """ + return self.client.get('/admin/').wsgi_request + + def test_page_summary_single_site(self): + request = self.get_request() + root_page = request.site.root_page + link = ''.format(reverse('wagtailadmin_explore', args=[root_page.pk])) + page_summary = PagesSummaryItem(request) + self.assertIn(link, page_summary.render()) + + def test_page_summary_multiple_sites(self): + Site.objects.create( + hostname='example.com', + root_page=Page.objects.get(pk=1)) + request = self.get_request() + link = ''.format(reverse('wagtailadmin_explore_root')) + page_summary = PagesSummaryItem(request) + self.assertIn(link, page_summary.render()) + + def test_page_summary_zero_sites(self): + Site.objects.all().delete() + request = self.get_request() + link = ''.format(reverse('wagtailadmin_explore_root')) + page_summary = PagesSummaryItem(request) + self.assertIn(link, page_summary.render()) + + class TestEditorHooks(TestCase, WagtailTestUtils): def setUp(self): self.homepage = Page.objects.get(id=2)