diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 1c96a84df1..b6c9233382 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -9,7 +9,8 @@ Changelog * The current live version of a page is now tracked on the revision listing view (Matheus Bratfisch) * Each block created in a `StreamField` is now assigned a globally unique identifier (Matt Westcott) * Mixcloud oEmbed pattern has been updated (Alice Rose) - * Added ``last_published_at`` field to the Page model (Matt Westcott) + * Added `last_published_at` field to the Page model (Matt Westcott) + * Added `show_in_menus_default` flag on page models, to allow "show in menus" to be checked by default (LB (Ben Johnston)) * Fix: Unauthenticated AJAX requests to admin views now return 403 rather than redirecting to the login page (Karl Hobley) * Fix: `TableBlock` options `afterChange`, `afterCreateCol`, `afterCreateRow`, `afterRemoveCol`, `afterRemoveRow` and `contextMenu` can now be overridden (Loic Teixeira) * Fix: The lastmod field returned by wagtailsitemaps now shows the last published date rather than the date of the last draft edit (Matt Westcott) diff --git a/docs/reference/pages/model_reference.rst b/docs/reference/pages/model_reference.rst index 9c56077a4b..f3fb363f65 100644 --- a/docs/reference/pages/model_reference.rst +++ b/docs/reference/pages/model_reference.rst @@ -86,6 +86,8 @@ Database fields Toggles whether the page should be included in site-wide menus. + Defaults to ``False`` and can be overridden on the model with ``show_in_menus_default = True``. + This is used by the :meth:`~wagtail.wagtailcore.query.PageQuerySet.in_menu` QuerySet filter. Methods and properties diff --git a/docs/releases/1.11.rst b/docs/releases/1.11.rst index 3448f83f85..fd580b1e87 100644 --- a/docs/releases/1.11.rst +++ b/docs/releases/1.11.rst @@ -49,6 +49,7 @@ Other features * Each block created in a ``StreamField`` is now assigned a globally unique identifier (Matt Westcott) * Mixcloud oEmbed pattern has been updated (Alice Rose) * Added ``last_published_at`` field to the Page model (Matt Westcott) + * Added ``show_in_menus_default`` flag on page models, to allow "show in menus" to be checked by default (LB (Ben Johnston)) Bug fixes diff --git a/wagtail/tests/testapp/migrations/0017_alwaysshowinmenuspage.py b/wagtail/tests/testapp/migrations/0017_alwaysshowinmenuspage.py new file mode 100644 index 0000000000..5253df4b56 --- /dev/null +++ b/wagtail/tests/testapp/migrations/0017_alwaysshowinmenuspage.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-05-26 13:38 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailcore', '0032_add_bulk_delete_page_permission'), + ('tests', '0016_auto_20170303_2340'), + ] + + operations = [ + migrations.CreateModel( + name='AlwaysShowInMenusPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + ] diff --git a/wagtail/tests/testapp/models.py b/wagtail/tests/testapp/models.py index aebf62d95f..339296225f 100644 --- a/wagtail/tests/testapp/models.py +++ b/wagtail/tests/testapp/models.py @@ -961,3 +961,7 @@ class TabbedSettings(TestSetting): FieldPanel('email') ], heading='Second tab'), ]) + + +class AlwaysShowInMenusPage(Page): + show_in_menus_default = True diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 18d2bac103..44a5a211ff 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -295,6 +295,8 @@ class Page(six.with_metaclass(PageBase, AbstractPage, index.Indexed, Clusterable blank=True, help_text=_("Optional. 'Search Engine Friendly' title. This will appear at the top of the browser window.") ) + + show_in_menus_default = False show_in_menus = models.BooleanField( verbose_name=_('show in menus'), default=False, @@ -367,11 +369,16 @@ class Page(six.with_metaclass(PageBase, AbstractPage, index.Indexed, Clusterable def __init__(self, *args, **kwargs): super(Page, self).__init__(*args, **kwargs) - if not self.id and not self.content_type_id: - # this model is being newly created rather than retrieved from the db; - # set content type to correctly represent the model class that this was - # created as - self.content_type = ContentType.objects.get_for_model(self) + if not self.id: + # this model is being newly created + # rather than retrieved from the db; + if not self.content_type_id: + # set content type to correctly represent the model class + # that this was created as + self.content_type = ContentType.objects.get_for_model(self) + if 'show_in_menus' not in kwargs: + # if the value is not set on submit refer to the model setting + self.show_in_menus = self.show_in_menus_default def __str__(self): return self.title diff --git a/wagtail/wagtailcore/tests/test_page_model.py b/wagtail/wagtailcore/tests/test_page_model.py index 8f25225d52..21c9eb09fc 100644 --- a/wagtail/wagtailcore/tests/test_page_model.py +++ b/wagtail/wagtailcore/tests/test_page_model.py @@ -16,10 +16,11 @@ from django.test.utils import override_settings from freezegun import freeze_time from wagtail.tests.testapp.models import ( - AbstractPage, Advert, BlogCategory, BlogCategoryBlogPage, BusinessChild, BusinessIndex, - BusinessNowherePage, BusinessSubIndex, CustomManager, CustomManagerPage, EventIndex, EventPage, - GenericSnippetPage, ManyToManyBlogPage, MTIBasePage, MTIChildPage, MyCustomPage, OneToOnePage, - SimplePage, SingleEventPage, SingletonPage, StandardIndex, TaggedPage) + AbstractPage, Advert, AlwaysShowInMenusPage, BlogCategory, BlogCategoryBlogPage, BusinessChild, + BusinessIndex, BusinessNowherePage, BusinessSubIndex, CustomManager, CustomManagerPage, + EventIndex, EventPage, GenericSnippetPage, ManyToManyBlogPage, MTIBasePage, MTIChildPage, + MyCustomPage, OneToOnePage, SimplePage, SingleEventPage, SingletonPage, StandardIndex, + TaggedPage) from wagtail.tests.utils import WagtailTestUtils from wagtail.wagtailcore.models import Page, PageManager, Site, get_page_models @@ -1349,3 +1350,26 @@ class TestDummyRequest(TestCase): # '*' is not a valid hostname, so ensure that we replace it with something sensible self.assertNotEqual(request.META['HTTP_HOST'], '*') + + +class TestShowInMenusDefaultOption(TestCase): + """ + This tests that a page model can define the default for 'show_in_menus' + """ + fixtures = ['test.json'] + + def test_show_in_menus_default(self): + # Create a page that does not have the default init + page = Page( + title='My Awesome Page', slug='my-awesome-page') + + # Check that the page instance creates with show_in_menu as False + self.assertFalse(page.show_in_menus) + + def test_show_in_menus_default_override(self): + # Create a page that does have the default init + page = AlwaysShowInMenusPage( + title='My Awesome Page', slug='my-awesome-page') + + # Check that the page instance creates with show_in_menu as True + self.assertTrue(page.show_in_menus)