Use SiteManager for Sites, add tests

The SiteManager class was not used on the Site model, or anywhere else.

This was not caught by the tests, because nothing tested the relevant
attributes on the Site model, nor were they used in the code base. Tests
have been added now.
pull/1929/merge
Tim Heap 2015-11-17 16:36:00 +11:00 zatwierdzone przez Matt Westcott
rodzic 06cba807df
commit 1d4a276f49
4 zmienionych plików z 67 dodań i 0 usunięć

Wyświetl plik

@ -11,6 +11,7 @@ Changelog
* Added keyboard shortcuts for preview and save in the page editor
* Fix: HTTP cache purge now works again on Python 2 (Mitchel Cabuloy)
* Fix: Locked pages can no longer be unpublished (Alex Bridge)
* Fix: Site records now implement `get_by_natural_key`
1.2 (12.11.2015)
~~~~~~~~~~~~~~~~

Wyświetl plik

@ -26,6 +26,7 @@ Bug fixes
* HTTP cache purge now works again on Python 2 (Mitchel Cabuloy)
* Locked pages can no longer be unpublished (Alex Bridge)
* Site records now implement ``get_by_natural_key``
Upgrade considerations

Wyświetl plik

@ -62,6 +62,8 @@ class Site(models.Model):
root_page = models.ForeignKey('Page', verbose_name=_('Root page'), related_name='sites_rooted_here')
is_default_site = models.BooleanField(verbose_name=_('Is default site'), default=False, help_text=_("If true, this site will handle requests for all other hostnames that do not have a site entry of their own"))
objects = SiteManager()
class Meta:
unique_together = ('hostname', 'port')
verbose_name = _('site')
@ -147,6 +149,7 @@ class Site(models.Model):
return result
# Clear the wagtail_site_root_paths from the cache whenever Site records are updated
@receiver(post_save, sender=Site)
def clear_site_root_paths_on_save(sender, instance, **kwargs):

Wyświetl plik

@ -0,0 +1,62 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from wagtail.wagtailcore.models import Site, Page
class TestSiteNaturalKey(TestCase):
def test_natural_key(self):
site = Site(hostname='example.com', port=8080)
self.assertEqual(site.natural_key(), ('example.com', 8080))
def test_get_by_natural_key(self):
site = Site.objects.create(hostname='example.com', port=8080, root_page=Page.objects.get(pk=2))
self.assertEqual(Site.objects.get_by_natural_key('example.com', 8080),
site)
class TestSiteUrl(TestCase):
def test_root_url_http(self):
site = Site(hostname='example.com', port=80)
self.assertEqual(site.root_url, 'http://example.com')
def test_root_url_https(self):
site = Site(hostname='example.com', port=443)
self.assertEqual(site.root_url, 'https://example.com')
def test_root_url_custom_port(self):
site = Site(hostname='example.com', port=8000)
self.assertEqual(site.root_url, 'http://example.com:8000')
class TestDefaultSite(TestCase):
def test_create_default_site(self):
Site.objects.all().delete()
Site.objects.create(hostname='test.com', is_default_site=True,
root_page=Page.objects.get(pk=2))
self.assertTrue(Site.objects.filter(is_default_site=True).exists())
def test_change_default_site(self):
default = Site.objects.get(is_default_site=True)
default.is_default_site = False
default.save()
Site.objects.create(hostname='test.com', is_default_site=True,
root_page=Page.objects.get(pk=2))
self.assertTrue(Site.objects.filter(is_default_site=True).exists())
def test_there_can_only_be_one(self):
site = Site(hostname='test.com', is_default_site=True,
root_page=Page.objects.get(pk=2))
with self.assertRaises(ValidationError):
site.clean_fields()
def test_oops_there_is_more_than_one(self):
Site.objects.create(hostname='example.com', is_default_site=True,
root_page=Page.objects.get(pk=2))
site = Site(hostname='test.com', is_default_site=True,
root_page=Page.objects.get(pk=2))
with self.assertRaises(Site.MultipleObjectsReturned):
# If there already are multiple default sites, you're in trouble
site.clean_fields()