def validate_page_form(form, parent_page, instance=None): # Perform default validation first form.full_clean() if 'slug' in form.cleaned_data: # Get siblings for the page siblings = parent_page.get_children() if instance and instance.id: siblings = siblings.exclude(id=instance.id) # Make sure the slug isn't being used by a sibling 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') # Go live must be before expire if go_live_at and expire_at: if go_live_at > expire_at: msg = _('Go live date/time must be before expiry date/time') form.add_error('go_live_at', ValidationError(msg)) form.add_error('expire_at', ValidationError(msg)) # Expire at must be in the future if expire_at and expire_at < timezone.now(): form.add_error( 'expire_at', ValidationError(_('Expiry date/time must be in the future')))
def validate_page_form(form, parent_page, instance=None): # Perform default validation first form.full_clean() if 'slug' in form.cleaned_data: # Get siblings for the page siblings = parent_page.get_children() if instance and instance.id: siblings = siblings.exclude(id=instance.id) # Make sure the slug isn't being used by a sibling 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') # Go live must be before expire if go_live_at and expire_at: if go_live_at > expire_at: msg = _('Go live date/time must be before expiry date/time') form.add_error('go_live_at', ValidationError(msg)) form.add_error('expire_at', ValidationError(msg)) # Expire at must be in the future if expire_at and expire_at < timezone.now(): form.add_error('expire_at', ValidationError(_('Expiry date/time must be in the future')))
def clean_seo_title(seo_title): if not seo_title: return '' validate_not_whitespace(seo_title) return seo_title
def clean_title(title): validate_not_whitespace(title) return title
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)