kopia lustrzana https://github.com/wagtail/wagtail
Add tests for custom page form classes
rodzic
83d8c1a50e
commit
eda5b86bee
|
@ -0,0 +1,14 @@
|
|||
from django import forms
|
||||
|
||||
from wagtail.wagtailadmin.forms import WagtailAdminPageForm
|
||||
|
||||
|
||||
class ValidatedPageForm(WagtailAdminPageForm):
|
||||
def clean_foo(self):
|
||||
if 'foo' not in self.cleaned_data:
|
||||
return
|
||||
|
||||
value = self.cleaned_data['foo']
|
||||
if value != 'bar':
|
||||
raise forms.ValidationError('Field foo must be bar')
|
||||
return value
|
|
@ -0,0 +1,28 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9 on 2016-01-25 05:54
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('wagtailcore', '0024_alter_page_content_type_on_delete_behaviour'),
|
||||
('tests', '0023_mycustompage'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ValidatedPage',
|
||||
fields=[
|
||||
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
|
||||
('foo', models.CharField(max_length=255)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
bases=('wagtailcore.page',),
|
||||
),
|
||||
]
|
|
@ -32,6 +32,8 @@ from wagtail.wagtailsearch import index
|
|||
from wagtail.wagtailimages.models import AbstractImage, Image
|
||||
from wagtail.wagtailimages.blocks import ImageChooserBlock
|
||||
|
||||
from .forms import ValidatedPageForm
|
||||
|
||||
|
||||
EVENT_AUDIENCE_CHOICES = (
|
||||
('public', "Public"),
|
||||
|
@ -602,3 +604,12 @@ class MyBasePage(Page):
|
|||
|
||||
class MyCustomPage(MyBasePage):
|
||||
pass
|
||||
|
||||
|
||||
class ValidatedPage(Page):
|
||||
foo = models.CharField(max_length=255)
|
||||
|
||||
base_form_class = ValidatedPageForm
|
||||
content_panels = Page.content_panels + [
|
||||
FieldPanel('foo'),
|
||||
]
|
||||
|
|
|
@ -20,9 +20,10 @@ from wagtail.wagtailadmin.widgets import AdminPageChooser, AdminDateInput, Admin
|
|||
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
|
||||
from wagtail.wagtailcore.models import Page, Site
|
||||
from wagtail.wagtailcore.fields import RichTextArea
|
||||
from wagtail.tests.testapp.forms import ValidatedPageForm
|
||||
from wagtail.tests.testapp.models import (
|
||||
PageChooserModel, EventPageChooserModel, EventPage, EventPageSpeaker, SimplePage
|
||||
)
|
||||
PageChooserModel, EventPageChooserModel, EventPage, EventPageSpeaker,
|
||||
SimplePage, ValidatedPage)
|
||||
from wagtail.tests.utils import WagtailTestUtils
|
||||
|
||||
|
||||
|
@ -122,6 +123,14 @@ class TestPageEditHandlers(TestCase):
|
|||
# The generated form should inherit from WagtailAdminPageForm
|
||||
self.assertTrue(issubclass(EventPageForm, WagtailAdminPageForm))
|
||||
|
||||
def test_get_form_for_page_with_custom_base(self):
|
||||
EditHandler = ValidatedPage.get_edit_handler()
|
||||
GeneratedValidatedPageForm = EditHandler.get_form_class(ValidatedPage)
|
||||
|
||||
# The generated form should inherit from ValidatedPageForm, because
|
||||
# ValidatedPageForm.base_form_class == ValidatedPageForm
|
||||
self.assertTrue(issubclass(GeneratedValidatedPageForm, ValidatedPageForm))
|
||||
|
||||
|
||||
class TestExtractPanelDefinitionsFromModelClass(TestCase):
|
||||
def test_can_extract_panel_property(self):
|
||||
|
|
Ładowanie…
Reference in New Issue