added convenience method in_site and tests

This commit is contained in:
Chris Rogers 2016-10-30 11:04:25 +00:00 committed by Matt Westcott
parent cb268c233a
commit 89e985d02a
5 changed files with 52 additions and 2 deletions

View file

@ -23,6 +23,7 @@ Changelog
* `ChoiceBlock` now accepts a callable as the choices list (Mikalai Radchuk)
* Redundant action buttons are now omitted from the root page in the explorer (Nick Smith)
* Locked pages are now disabled from editing at the browser level (Edd Baldry)
* Added `in_site` method for filtering page querysets to pages within the specified site (Chris Rogers)
* Fix: `AbstractForm` now respects custom `get_template` methods on the page model (Gagaro)
* Fix: Use specific page model for the parent page in the explore index (Gagaro)
* Fix: Remove responsive styles in embed when there is no ratio available (Gagaro)

View file

@ -71,6 +71,15 @@ Reference
.. automethod:: not_in_menu
.. automethod:: in_site
Example:
.. code-block:: python
# Get all the EventPages in the current site
site_events = EventPage.objects.in_site(request.site)
.. automethod:: page
Example:

View file

@ -54,6 +54,7 @@ Minor features
* ``ChoiceBlock`` now accepts a callable as the choices list (Mikalai Radchuk)
* Redundant action buttons are now omitted from the root page in the explorer (Nick Smith)
* Locked pages are now disabled from editing at the browser level (Edd Baldry)
* Added :meth:`wagtail.wagtailcore.query.PageQuerySet.in_site` method for filtering page querysets to pages within the specified site (Chris Rogers)
Bug fixes

View file

@ -350,6 +350,12 @@ class PageQuerySet(SearchableQuerySetMixin, TreeQuerySet):
else:
return self._clone(klass=SpecificQuerySet)
def in_site(self, site):
"""
This filters the QuerySet to only contain pages within the specified site.
"""
return self.descendant_of(site.root_page, inclusive=True)
def specific_iterator(qs):
"""

View file

@ -2,8 +2,8 @@ from __future__ import absolute_import, unicode_literals
from django.test import TestCase
from wagtail.tests.testapp.models import EventPage, SingleEventPage
from wagtail.wagtailcore.models import Page, PageViewRestriction
from wagtail.tests.testapp.models import EventPage, SimplePage, SingleEventPage
from wagtail.wagtailcore.models import Page, PageViewRestriction, Site
from wagtail.wagtailcore.signals import page_unpublished
@ -398,6 +398,39 @@ class TestPageQuerySet(TestCase):
self.assertTrue(pages.filter(id=event.id).exists())
class TestPageQueryInSite(TestCase):
fixtures = ['test.json']
def setUp(self):
self.site_2_page = SimplePage(
title="Site 2 page",
slug="site_2_page",
content="Hello",
)
Page.get_first_root_node().add_child(instance=self.site_2_page)
self.site_2_subpage = SimplePage(
title="Site 2 subpage",
slug="site_2_subpage",
content="Hello again",
)
self.site_2_page.add_child(instance=self.site_2_subpage)
self.site_2 = Site.objects.create(
hostname='example.com',
port=8080,
root_page=Page.objects.get(pk=self.site_2_page.pk),
is_default_site=False
)
self.about_us_page = SimplePage.objects.get(url_path='/home/about-us/')
def test_in_site(self):
site_2_pages = SimplePage.objects.in_site(self.site_2)
self.assertIn(self.site_2_page, site_2_pages)
self.assertIn(self.site_2_subpage, site_2_pages)
self.assertNotIn(self.about_us_page, site_2_pages)
class TestPageQuerySetSearch(TestCase):
fixtures = ['test.json']