Add tests and move HTML title to python
This commit is contained in:
parent
e7cdcb0774
commit
063ee3e1ff
5 changed files with 60 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
from wagtail.core.models import Page
|
from wagtail.core.models import Page
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.conf import settings
|
||||||
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel
|
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel
|
||||||
from wagtail.images.edit_handlers import ImageChooserPanel
|
from wagtail.images.edit_handlers import ImageChooserPanel
|
||||||
from wagtailmetadata.models import MetadataPageMixin
|
from wagtailmetadata.models import MetadataPageMixin
|
||||||
|
@ -33,6 +34,10 @@ class Entity(MetadataPageMixin, Page):
|
||||||
def get_meta_description(self):
|
def get_meta_description(self):
|
||||||
return self.search_description
|
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):
|
def get_short_body(self):
|
||||||
body_words = BeautifulSoup(str(self.body), 'html5lib').get_text().split(' ')
|
body_words = BeautifulSoup(str(self.body), 'html5lib').get_text().split(' ')
|
||||||
ending = '...' if len(body_words) > SHORT_BODY_LENGTH else ''
|
ending = '...' if len(body_words) > SHORT_BODY_LENGTH else ''
|
||||||
|
|
|
@ -17,9 +17,10 @@ class BaseTestCase(WagtailPageTests):
|
||||||
|
|
||||||
self.root = self.create_initial_homepage()
|
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=[
|
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({
|
data.update({
|
||||||
'action-publish': 'action-publish',
|
'action-publish': 'action-publish',
|
||||||
|
@ -39,6 +40,9 @@ class BaseTestCase(WagtailPageTests):
|
||||||
def parse_content(self, content):
|
def parse_content(self, content):
|
||||||
return BeautifulSoup(content, 'html.parser')
|
return BeautifulSoup(content, 'html.parser')
|
||||||
|
|
||||||
|
def get_page_title(self, response):
|
||||||
|
return self.parse_content(response.content).title.string
|
||||||
|
|
||||||
def create_initial_homepage(self):
|
def create_initial_homepage(self):
|
||||||
"""
|
"""
|
||||||
from https://github.com/wagtail/wagtail/blob/master/wagtail/project_template/home/migrations/0002_create_homepage.py
|
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:
|
for key in SETTINGS_KEYS:
|
||||||
self.assertIn(key, response.context['settings'])
|
self.assertIn(key, response.context['settings'])
|
||||||
self.assertEqual(response.context['settings'][key], getattr(settings, key))
|
self.assertEqual(response.context['settings'][key], getattr(settings, key))
|
||||||
|
|
||||||
|
|
||||||
|
class PageTestCase(BaseTestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.create_model(HomePage, {
|
||||||
|
'title': 'Test',
|
||||||
|
'body': '<p>Some test content</p>',
|
||||||
|
})
|
||||||
|
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': '<p>Some content of the child page</p>',
|
||||||
|
}, 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))
|
||||||
|
|
10
project/common/utils.py
Normal file
10
project/common/utils.py
Normal file
|
@ -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
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
{% meta_tags %}
|
{% meta_tags %}
|
||||||
|
|
||||||
<title>{% include "common/title-item.html" with page=self current=self %}{{ settings.WAGTAIL_SITE_NAME }}</title>
|
<title>{{ page.get_page_title }}</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{% wagtailuserbar %}
|
{% wagtailuserbar %}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
{{ page.title }} ::
|
|
||||||
|
|
||||||
{% if page.get_parent and page.get_parent.url and page.get_parent.id != homepage.id %}
|
|
||||||
{% include "common/title-item.html" with page=page.get_parent %}
|
|
||||||
{% endif %}
|
|
Reference in a new issue