diff --git a/project/common/models.py b/project/common/models.py index 213a504..c7c8c34 100644 --- a/project/common/models.py +++ b/project/common/models.py @@ -1,5 +1,6 @@ from wagtail.core.models import Page from django.db import models +from django.conf import settings from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel from wagtail.images.edit_handlers import ImageChooserPanel from wagtailmetadata.models import MetadataPageMixin @@ -33,6 +34,10 @@ class Entity(MetadataPageMixin, Page): def get_meta_description(self): return self.search_description + def get_page_title(self): + from . import utils + return utils.get_page_title(self) + settings.WAGTAIL_SITE_NAME + def get_short_body(self): body_words = BeautifulSoup(str(self.body), 'html5lib').get_text().split(' ') ending = '...' if len(body_words) > SHORT_BODY_LENGTH else '' diff --git a/project/common/tests.py b/project/common/tests.py index 3b30e45..b5732a0 100644 --- a/project/common/tests.py +++ b/project/common/tests.py @@ -17,9 +17,10 @@ class BaseTestCase(WagtailPageTests): self.root = self.create_initial_homepage() - def create_model(self, model, data={}): + def create_model(self, model, data={}, parent=None): + parent = parent.pk if parent else self.root.pk add_url = reverse('wagtailadmin_pages:add', args=[ - model._meta.app_label, model._meta.model_name, self.root.pk + model._meta.app_label, model._meta.model_name, parent ]) data.update({ 'action-publish': 'action-publish', @@ -39,6 +40,9 @@ class BaseTestCase(WagtailPageTests): def parse_content(self, content): return BeautifulSoup(content, 'html.parser') + def get_page_title(self, response): + return self.parse_content(response.content).title.string + def create_initial_homepage(self): """ from https://github.com/wagtail/wagtail/blob/master/wagtail/project_template/home/migrations/0002_create_homepage.py @@ -66,3 +70,41 @@ class ContextInjectorTestCase(BaseTestCase): for key in SETTINGS_KEYS: self.assertIn(key, response.context['settings']) self.assertEqual(response.context['settings'][key], getattr(settings, key)) + + +class PageTestCase(BaseTestCase): + def setUp(self): + super().setUp() + self.create_model(HomePage, { + 'title': 'Test', + 'body': '
Some test content
', + }) + self.page = HomePage.objects.get(title='Test') + + def test_short_body(self): + self.assertEqual(self.page.get_short_body(), 'Some test content') + + def test_page(self): + response = self.client.get(self.page.url) + self.assertEqual(response.status_code, 200) + content = self.parse_content(response.content) + self.assertIn(self.page.get_short_body(), content.get_text()) + + def test_page_title(self): + response = self.client.get(self.page.url) + self.assertEqual(response.status_code, 200) + title = self.get_page_title(response) + self.assertEqual(self.page.get_page_title(), title) + self.assertEqual(self.page.get_page_title(), "Test :: {}".format(settings.WAGTAIL_SITE_NAME)) + + def test_nested_page_title(self): + self.create_model(HomePage, { + 'title': 'Test Child', + 'body': 'Some content of the child page
', + }, parent=self.page) + new_page = HomePage.objects.get(title='Test Child') + response = self.client.get(new_page.url) + self.assertEqual(response.status_code, 200) + title = self.get_page_title(response) + self.assertEqual(new_page.get_page_title(), title) + self.assertEqual(new_page.get_page_title(), "Test Child :: Test :: {}".format(settings.WAGTAIL_SITE_NAME)) diff --git a/project/common/utils.py b/project/common/utils.py new file mode 100644 index 0000000..9565e65 --- /dev/null +++ b/project/common/utils.py @@ -0,0 +1,10 @@ +from project.home.models import HomePage + + +def get_page_title(page): + title = "{} :: ".format(page.title) + homepage = HomePage.objects.live().first() + parent = page.get_parent() + if parent and parent.url and parent.id != homepage.id: + title += get_page_title(parent) + return title diff --git a/templates/base.html b/templates/base.html index c1c6d70..14f261f 100644 --- a/templates/base.html +++ b/templates/base.html @@ -13,7 +13,7 @@ {% meta_tags %} -