Fix queryset ordering and add a test to confirm ordering matches expectation in all setups

This commit is contained in:
Andy Babic 2018-06-17 22:51:26 +01:00 committed by Matt Westcott
parent be534ee535
commit 34579c3776
5 changed files with 51 additions and 1 deletions

View file

@ -1,6 +1,12 @@
Changelog
=========
2.1.1 (xx.xx.xxxx) - IN DEVELOPMENT
~~~~~~~~~~~~~~~~~~
* Fix: Site.get_site_root_paths() preferring other sites over the default when some sites share the same root_page (Andy Babic)
2.1 (22.05.2018)
~~~~~~~~~~~~~~~~

16
docs/releases/2.1.1.rst Normal file
View file

@ -0,0 +1,16 @@
============================================
Wagtail 2.1.1 release notes - IN DEVELOPMENT
============================================
.. contents::
:local:
:depth: 1
What's new
==========
Bug fixes
~~~~~~~~~
* Fix ``Site.get_site_root_paths()`` preferring other sites over the default when some sites share the same root_page (Andy Babic)

View file

@ -5,6 +5,7 @@ Release notes
:maxdepth: 1
upgrading
2.1.1
2.1
2.0.1
2.0

View file

@ -161,7 +161,7 @@ class Site(models.Model):
result = [
(site.id, site.root_page.url_path, site.root_url)
for site in Site.objects.select_related('root_page').order_by(
'-root_page__url_path', 'is_default_site', 'hostname')
'-root_page__url_path', '-is_default_site', 'hostname')
]
cache.set('wagtail_site_root_paths', result, 3600)

View file

@ -122,3 +122,30 @@ class TestDefaultSite(TestCase):
with self.assertRaises(Site.MultipleObjectsReturned):
# If there already are multiple default sites, you're in trouble
site.clean_fields()
class TestGetSiteRootPaths(TestCase):
def setUp(self):
self.default_site = Site.objects.get()
self.abc_site = Site.objects.create(
hostname='abc.com', root_page=self.default_site.root_page
)
self.def_site = Site.objects.create(
hostname='def.com', root_page=self.default_site.root_page
)
# Changing the hostname to show that being the default site takes
# promotes a site over the alphabetical ordering of hostname
self.default_site.hostname = 'xyz.com'
self.default_site.save()
def test_result_order_when_multiple_sites_share_the_same_root_page(self):
result = Site.get_site_root_paths()
# An entry for the default site should come first
self.assertEqual(result[0][0], self.default_site.id)
# Followed by entries for others in 'host' alphabetical order
self.assertEqual(result[1][0], self.abc_site.id)
self.assertEqual(result[2][0], self.def_site.id)