kopia lustrzana https://github.com/wagtail/wagtail
feat(django 1.9): Replaced validation for whitespace-only title fields
Django now strips whitespace automatically making Wagtail's whitespace check obsolete. This commit removes our old checks and replicates Django's new behaviour for users of 1.8 and below.pull/1990/merge
rodzic
b38579495c
commit
baed1d50be
|
@ -1,6 +1,7 @@
|
|||
from datetime import timedelta
|
||||
import mock
|
||||
|
||||
import django
|
||||
from django.test import TestCase
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.contrib.auth import get_user_model
|
||||
|
@ -626,15 +627,48 @@ class TestPageCreation(TestCase, WagtailTestUtils):
|
|||
'content': "Some content",
|
||||
'slug': 'hello-world',
|
||||
'action-submit': "Submit",
|
||||
'seo_title': '\t',
|
||||
}
|
||||
response = self.client.post(
|
||||
reverse('wagtailadmin_pages:add', 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")
|
||||
if django.VERSION >= (1, 9):
|
||||
self.assertFormError(response, 'form', 'title', "This field is required.")
|
||||
else:
|
||||
self.assertFormError(response, 'form', 'title', "This field cannot be blank.")
|
||||
|
||||
def test_whitespace_titles_with_tab(self):
|
||||
post_data = {
|
||||
'title': "\t", # Single space on purpose
|
||||
'content': "Some content",
|
||||
'slug': 'hello-world',
|
||||
'action-submit': "Submit",
|
||||
}
|
||||
response = self.client.post(reverse('wagtailadmin_pages:add', args=('tests', 'simplepage', self.root_page.id)), post_data)
|
||||
|
||||
# Check that a form error was raised
|
||||
if django.VERSION >= (1, 9):
|
||||
self.assertFormError(response, 'form', 'title', "This field is required.")
|
||||
else:
|
||||
self.assertFormError(response, 'form', 'title', "This field cannot be blank.")
|
||||
|
||||
def test_whitespace_titles_with_tab_in_seo_title(self):
|
||||
post_data = {
|
||||
'title': "Hello",
|
||||
'content': "Some content",
|
||||
'slug': 'hello-world',
|
||||
'action-submit': "Submit",
|
||||
'seo_title': '\t'
|
||||
}
|
||||
response = self.client.post(reverse('wagtailadmin_pages:add', args=('tests', 'simplepage', self.root_page.id)), post_data)
|
||||
|
||||
# Should be successful, as seo_title is not required
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
# The tab should be automatically stripped from the seo_title
|
||||
page = Page.objects.order_by('-id').first()
|
||||
self.assertEqual(page.seo_title, '')
|
||||
|
||||
def test_long_slug(self):
|
||||
post_data = {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import django
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.core.exceptions import ValidationError, PermissionDenied
|
||||
|
@ -18,7 +19,6 @@ from wagtail.wagtailadmin import 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
|
||||
|
||||
|
@ -316,7 +316,19 @@ def edit(request, page_id):
|
|||
|
||||
|
||||
def validate_page_form(form, parent_page, instance=None):
|
||||
# Perform default validation first
|
||||
# Strip whitespace in title and seo_title fields
|
||||
# This is done for us in Django 1.9 and above
|
||||
if django.VERSION < (1, 9):
|
||||
def clean_title():
|
||||
return form.cleaned_data['title'].strip()
|
||||
|
||||
def clean_seo_title():
|
||||
return form.cleaned_data['seo_title'].strip()
|
||||
|
||||
form.clean_title = clean_title
|
||||
form.clean_seo_title = clean_seo_title
|
||||
|
||||
# Perform default validation
|
||||
form.full_clean()
|
||||
|
||||
if 'slug' in form.cleaned_data:
|
||||
|
@ -329,20 +341,6 @@ def validate_page_form(form, parent_page, instance=None):
|
|||
if siblings.filter(slug=form.cleaned_data['slug']).exists():
|
||||
form.add_error('slug', ValidationError(_("This slug is already in use")))
|
||||
|
||||
# Check that the title and seo_title are not entirely whitespace
|
||||
if 'title' in form.cleaned_data:
|
||||
try:
|
||||
validate_not_whitespace(form.cleaned_data['title'])
|
||||
except ValidationError as error:
|
||||
form.add_error('title', error)
|
||||
|
||||
if 'seo_title' in form.cleaned_data:
|
||||
if form.cleaned_data['seo_title']:
|
||||
try:
|
||||
validate_not_whitespace(form.cleaned_data['seo_title'])
|
||||
except ValidationError as error:
|
||||
form.add_error('seo_title', error)
|
||||
|
||||
# Check scheduled publishing fields
|
||||
go_live_at = form.cleaned_data.get('go_live_at')
|
||||
expire_at = form.cleaned_data.get('expire_at')
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
from django.test import TestCase
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from wagtail.wagtailcore.validators import validate_not_whitespace
|
||||
|
||||
|
||||
class TestValidators(TestCase):
|
||||
|
||||
def test_not_whitespace(self):
|
||||
validate_not_whitespace('bar')
|
||||
|
||||
for test_value in (' ', '\t', '\r', '\n', '\r\n'):
|
||||
with self.assertRaises(ValidationError):
|
||||
validate_not_whitespace(test_value)
|
|
@ -1,15 +0,0 @@
|
|||
import re
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
WHITESPACE_RE = re.compile(r'^\s+$')
|
||||
|
||||
|
||||
def validate_not_whitespace(value):
|
||||
"""
|
||||
Validate that a value isn't all whitespace, for example in title and
|
||||
seo_title
|
||||
"""
|
||||
if value and WHITESPACE_RE.match(value):
|
||||
raise ValidationError(_('Value cannot be entirely whitespace characters'))
|
Ładowanie…
Reference in New Issue