From 1840ecb672e25e466f593bfe661f88822d4119a8 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 18 Jun 2014 11:09:14 +0100 Subject: [PATCH 1/2] Added in_menu and not_in_menu methods to PageQuerySet --- CHANGELOG.txt | 1 + wagtail/wagtailcore/models.py | 6 ++++++ wagtail/wagtailcore/query.py | 9 ++++++++ .../wagtailcore/tests/test_page_queryset.py | 21 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index ef9c25707b..35e6ab7c33 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -17,6 +17,7 @@ Changelog * 'image' tag now accepts extra keyword arguments to be output as attributes on the img tag * Added an 'attrs' property to image rendition objects to output src, width, height and alt attributes all in one go * Added 'construct_whitelister_element_rules' hook for customising the HTML whitelist used when saving rich text fields + * Added 'in_menu' and 'not_in_menu' methods to PageQuerySet * Fix: Animated GIFs are now coalesced before resizing * Fix: Wand backend clones images before modifying them * Fix: Admin breadcrumb now positioned correctly on mobile diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 40a10a9c54..e6aee2fa84 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -135,6 +135,12 @@ class PageManager(models.Manager): def not_live(self): return self.get_queryset().not_live() + def in_menu(self): + return self.get_query_set().in_menu() + + def not_in_menu(self): + return self.get_query_set().not_in_menu() + def page(self, other): return self.get_queryset().page(other) diff --git a/wagtail/wagtailcore/query.py b/wagtail/wagtailcore/query.py index 9404e25b4d..57e8ffff33 100644 --- a/wagtail/wagtailcore/query.py +++ b/wagtail/wagtailcore/query.py @@ -16,6 +16,15 @@ class PageQuerySet(MP_NodeQuerySet): def not_live(self): return self.exclude(self.live_q()) + def in_menu_q(self): + return Q(show_in_menus=True) + + def in_menu(self): + return self.filter(self.in_menu_q()) + + def not_in_menu(self): + return self.exclude(self.in_menu_q()) + def page_q(self, other): return Q(id=other.id) diff --git a/wagtail/wagtailcore/tests/test_page_queryset.py b/wagtail/wagtailcore/tests/test_page_queryset.py index 06f2c3e21c..57599173ce 100644 --- a/wagtail/wagtailcore/tests/test_page_queryset.py +++ b/wagtail/wagtailcore/tests/test_page_queryset.py @@ -34,6 +34,27 @@ class TestPageQuerySet(TestCase): event = Page.objects.get(url_path='/home/events/someone-elses-event/') self.assertTrue(pages.filter(id=event.id).exists()) + def test_in_menu(self): + pages = Page.objects.in_menu() + + # All pages must be be in the menus + for page in pages: + self.assertTrue(page.show_in_menus) + + # Check that the events index is in the results + events_index = Page.objects.get(url_path='/home/events/') + self.assertTrue(pages.filter(id=events_index.id).exists()) + + def test_not_in_menu(self): + pages = Page.objects.not_in_menu() + + # All pages must not be in menus + for page in pages: + self.assertFalse(page.show_in_menus) + + # Check that the root page is in the results + self.assertTrue(pages.filter(id=1).exists()) + def test_page(self): homepage = Page.objects.get(url_path='/home/') pages = Page.objects.page(homepage) From fe45b4e424deb36bf0aea41dd011240ef098889a Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 18 Jun 2014 11:41:54 +0100 Subject: [PATCH 2/2] Replaced get_query_set with get_queryset for in_menu methods --- wagtail/wagtailcore/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index e6aee2fa84..e98abbb640 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -136,10 +136,10 @@ class PageManager(models.Manager): return self.get_queryset().not_live() def in_menu(self): - return self.get_query_set().in_menu() + return self.get_queryset().in_menu() def not_in_menu(self): - return self.get_query_set().not_in_menu() + return self.get_queryset().not_in_menu() def page(self, other): return self.get_queryset().page(other)