Ensure title and seo_title are not whitespace

- Add form clean methods and validation on title and seo_title
    - Fixes #413
This commit is contained in:
Frank Wiles 2015-01-28 10:22:52 -06:00
parent 0f7104ce40
commit 9f12c109be
2 changed files with 48 additions and 6 deletions

View file

@ -186,7 +186,7 @@ class TestPageCreation(TestCase, WagtailTestUtils):
# Should be redirected to edit page
self.assertRedirects(response, reverse('wagtailadmin_pages_edit', args=(page.id, )))
self.assertEqual(page.title, post_data['title'])
self.assertIsInstance(page, SimplePage)
self.assertFalse(page.live)
@ -325,7 +325,7 @@ class TestPageCreation(TestCase, WagtailTestUtils):
# Should be redirected to explorer
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))
self.assertEqual(page.title, post_data['title'])
self.assertIsInstance(page, SimplePage)
self.assertFalse(page.live)
@ -389,6 +389,20 @@ class TestPageCreation(TestCase, WagtailTestUtils):
self.assertTrue(response.context['self'].path.startswith(self.root_page.path))
self.assertEqual(response.context['self'].get_parent(), self.root_page)
def test_whitespace_titles(self):
post_data = {
'title': " ", # Single space on purpose
'content': "Some content",
'slug': 'hello-world',
'action-submit': "Submit",
'seo_title': '\t',
}
response = self.client.post(reverse('wagtailadmin_pages_create', args=('tests', 'simplepage', self.root_page.id)), post_data)
# Check that a form error was raised
self.assertFormError(response, 'form', 'title', "Value cannot be entirely whitespace characters")
self.assertFormError(response, 'form', 'seo_title', "Value cannot be entirely whitespace characters")
class TestPageEdit(TestCase, WagtailTestUtils):
def setUp(self):
@ -439,7 +453,7 @@ class TestPageEdit(TestCase, WagtailTestUtils):
'slug': 'hello-world',
}
response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data)
# Should be redirected to edit page
self.assertRedirects(response, reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )))
@ -461,7 +475,7 @@ class TestPageEdit(TestCase, WagtailTestUtils):
'slug': 'hello-world',
}
response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data)
# Shouldn't be redirected
self.assertContains(response, "The page could not be saved as it is locked")
@ -550,7 +564,7 @@ class TestPageEdit(TestCase, WagtailTestUtils):
'action-publish': "Publish",
}
response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data)
# Should be redirected to explorer
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))
@ -652,7 +666,7 @@ class TestPageEdit(TestCase, WagtailTestUtils):
'action-submit': "Submit",
}
response = self.client.post(reverse('wagtailadmin_pages_edit', args=(self.child_page.id, )), post_data)
# Should be redirected to explorer
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))

View file

@ -20,6 +20,7 @@ from wagtail.wagtailadmin import tasks, signals
from wagtail.wagtailcore import hooks
from wagtail.wagtailcore.models import Page, PageRevision, get_navigation_menu_items
from wagtail.wagtailcore.validators import validate_not_whitespace
from wagtail.wagtailadmin import messages
@ -161,6 +162,19 @@ def create(request, content_type_app_name, content_type_model_name, parent_page_
return slug
form.fields['slug'].clean = clean_slug
# Validate title and seo_title are not entirely whitespace
def clean_title(title):
validate_not_whitespace(title)
return title
form.fields['title'].clean = clean_title
def clean_seo_title(seo_title):
if not seo_title:
return ''
validate_not_whitespace(seo_title)
return seo_title
form.fields['seo_title'].clean = clean_seo_title
# Stick another validator into the form to check that the scheduled publishing settings are set correctly
def clean():
cleaned_data = form_class.clean(form)
@ -277,6 +291,20 @@ def edit(request, page_id):
return slug
form.fields['slug'].clean = clean_slug
# Validate title and seo_title are not entirely whitespace
def clean_title(title):
validate_not_whitespace(title)
return title
form.fields['title'].clean = clean_title
def clean_seo_title(seo_title):
if not seo_title:
return ''
validate_not_whitespace(seo_title)
return seo_title
form.fields['seo_title'].clean = clean_seo_title
# Stick another validator into the form to check that the scheduled publishing settings are set correctly
def clean():
cleaned_data = form_class.clean(form)