本文整理汇总了Python中wagtail.admin.edit_handlers.ObjectList.get_form_class方法的典型用法代码示例。如果您正苦于以下问题:Python ObjectList.get_form_class方法的具体用法?Python ObjectList.get_form_class怎么用?Python ObjectList.get_form_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wagtail.admin.edit_handlers.ObjectList
的用法示例。
在下文中一共展示了ObjectList.get_form_class方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_form_without_model
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_get_form_without_model(self):
edit_handler = ObjectList()
with self.assertRaisesMessage(
AttributeError,
'ObjectList is not bound to a model yet. '
'Use `.bind_to(model=model)` before using this method.'):
edit_handler.get_form_class()
示例2: TestObjectList
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
class TestObjectList(TestCase):
def setUp(self):
self.request = RequestFactory().get('/')
user = AnonymousUser() # technically, Anonymous users cannot access the admin
self.request.user = user
# a custom ObjectList for EventPage
self.event_page_object_list = ObjectList([
FieldPanel('title', widget=forms.Textarea),
FieldPanel('date_from'),
FieldPanel('date_to'),
InlinePanel('speakers', label="Speakers"),
], heading='Event details', classname="shiny").bind_to(
model=EventPage, request=self.request)
def test_get_form_class(self):
EventPageForm = self.event_page_object_list.get_form_class()
form = EventPageForm()
# form must include the 'speakers' formset required by the speakers InlinePanel
self.assertIn('speakers', form.formsets)
# form must respect any overridden widgets
self.assertEqual(type(form.fields['title'].widget), forms.Textarea)
def test_render(self):
EventPageForm = self.event_page_object_list.get_form_class()
event = EventPage(title='Abergavenny sheepdog trials')
form = EventPageForm(instance=event)
object_list = self.event_page_object_list.bind_to(
instance=event,
form=form,
)
result = object_list.render()
# result should contain ObjectList furniture
self.assertIn('<ul class="objects">', result)
# result should contain h2 headings (including labels) for children
self.assertInHTML('<h2><label for="id_date_from">Start date</label></h2>', result)
# result should include help text for children
self.assertIn('<div class="object-help help"><span class="icon-help-inverse" aria-hidden="true"></span>Not required if event is on a single day</div>', result)
# result should contain rendered content from descendants
self.assertIn('Abergavenny sheepdog trials</textarea>', result)
# this result should not include fields that are not covered by the panel definition
self.assertNotIn('signup_link', result)
示例3: test_render_with_panel_overrides
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_render_with_panel_overrides(self):
"""
Check that inline panel renders the panels listed in the InlinePanel definition
where one is specified
"""
speaker_object_list = ObjectList([
InlinePanel('speakers', label="Speakers", panels=[
FieldPanel('first_name', widget=forms.Textarea),
ImageChooserPanel('image'),
]),
]).bind_to_model(EventPage)
speaker_inline_panel = speaker_object_list.children[0]
EventPageForm = speaker_object_list.get_form_class()
# speaker_inline_panel should instruct the form class to include a 'speakers' formset
self.assertEqual(['speakers'], list(EventPageForm.formsets.keys()))
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = speaker_inline_panel.bind_to_instance(
instance=event_page, form=form, request=self.request)
result = panel.render_as_field()
# rendered panel should contain first_name rendered as a text area, but no last_name field
self.assertIn('<label for="id_speakers-0-first_name">Name:</label>', result)
self.assertIn('Father</textarea>', result)
self.assertNotIn('<label for="id_speakers-0-last_name">Surname:</label>', result)
# test for #338: surname field should not be rendered as a 'stray' label-less field
self.assertTagInHTML('<input id="id_speakers-0-last_name">', result, count=0, allow_extra_attrs=True)
self.assertIn('<label for="id_speakers-0-image">Image:</label>', result)
self.assertIn('Choose an image', result)
# rendered panel must also contain hidden fields for id, DELETE and ORDER
self.assertTagInHTML(
'<input id="id_speakers-0-id" name="speakers-0-id" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-DELETE" name="speakers-0-DELETE" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-ORDER" name="speakers-0-ORDER" type="hidden">',
result, allow_extra_attrs=True
)
# rendered panel must contain maintenance form for the formset
self.assertTagInHTML(
'<input id="id_speakers-TOTAL_FORMS" name="speakers-TOTAL_FORMS" type="hidden">',
result, allow_extra_attrs=True
)
# render_js_init must provide the JS initializer
self.assertIn('var panel = InlinePanel({', panel.render_js_init())
示例4: test_render
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_render(self):
"""
Check that the inline panel renders the panels set on the model
when no 'panels' parameter is passed in the InlinePanel definition
"""
speaker_object_list = ObjectList([
InlinePanel('speakers', label="Speakers", classname="classname-for-speakers")
]).bind_to_model(EventPage)
EventPageForm = speaker_object_list.get_form_class()
# SpeakerInlinePanel should instruct the form class to include a 'speakers' formset
self.assertEqual(['speakers'], list(EventPageForm.formsets.keys()))
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = speaker_object_list.bind_to_instance(instance=event_page,
form=form,
request=self.request)
result = panel.render_as_field()
self.assertIn('<li class="object classname-for-speakers">', result)
self.assertIn('<label for="id_speakers-0-first_name">Name:</label>', result)
self.assertIn('value="Father"', result)
self.assertIn('<label for="id_speakers-0-last_name">Surname:</label>', result)
self.assertIn('<label for="id_speakers-0-image">Image:</label>', result)
self.assertIn('Choose an image', result)
# rendered panel must also contain hidden fields for id, DELETE and ORDER
self.assertTagInHTML(
'<input id="id_speakers-0-id" name="speakers-0-id" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-DELETE" name="speakers-0-DELETE" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-ORDER" name="speakers-0-ORDER" type="hidden">',
result, allow_extra_attrs=True
)
# rendered panel must contain maintenance form for the formset
self.assertTagInHTML(
'<input id="id_speakers-TOTAL_FORMS" name="speakers-TOTAL_FORMS" type="hidden">',
result, allow_extra_attrs=True
)
# rendered panel must include the JS initializer
self.assertIn('var panel = InlinePanel({', result)
示例5: test_autodetect_page_type
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_autodetect_page_type(self):
# Model has a foreign key to EventPage, which we want to autodetect
# instead of specifying the page type in PageChooserPanel
MyPageObjectList = ObjectList([PageChooserPanel('page')]).bind_to_model(EventPageChooserModel)
MyPageChooserPanel = MyPageObjectList.children[0]
PageChooserForm = MyPageObjectList.get_form_class(EventPageChooserModel)
form = PageChooserForm(instance=self.test_instance)
page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form)
result = page_chooser_panel.render_as_field()
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, false, null);'.format(
id="id_page", model="tests.eventpage", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
示例6: test_override_page_type
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_override_page_type(self):
# Model has a foreign key to Page, but we specify EventPage in the PageChooserPanel
# to restrict the chooser to that page type
MyPageObjectList = ObjectList([
PageChooserPanel('page', 'tests.EventPage')
]).bind_to_model(EventPageChooserModel)
MyPageChooserPanel = MyPageObjectList.children[0]
PageChooserForm = MyPageObjectList.get_form_class(EventPageChooserModel)
form = PageChooserForm(instance=self.test_instance)
page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form)
result = page_chooser_panel.render_as_field()
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, false, null);'.format(
id="id_page", model="tests.eventpage", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
示例7: test_render_js_init_with_can_choose_root_true
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_render_js_init_with_can_choose_root_true(self):
# construct an alternative page chooser panel object, with can_choose_root=True
MyPageObjectList = ObjectList([
PageChooserPanel('page', can_choose_root=True)
]).bind_to_model(PageChooserModel)
MyPageChooserPanel = MyPageObjectList.children[0]
PageChooserForm = MyPageObjectList.get_form_class(EventPageChooserModel)
form = PageChooserForm(instance=self.test_instance)
page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form)
result = page_chooser_panel.render_as_field()
# the canChooseRoot flag on createPageChooser should now be true
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, true, null);'.format(
id="id_page", model="wagtailcore.page", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
示例8: test_no_thousand_separators_in_js
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
def test_no_thousand_separators_in_js(self):
"""
Test that the USE_THOUSAND_SEPARATOR setting does not screw up the rendering of numbers
(specifically maxForms=1000) in the JS initializer:
https://github.com/wagtail/wagtail/pull/2699
https://github.com/wagtail/wagtail/issues/3227
"""
speaker_object_list = ObjectList([
InlinePanel('speakers', label="Speakers", panels=[
FieldPanel('first_name', widget=forms.Textarea),
ImageChooserPanel('image'),
]),
]).bind_to(model=EventPage, request=self.request)
speaker_inline_panel = speaker_object_list.children[0]
EventPageForm = speaker_object_list.get_form_class()
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = speaker_inline_panel.bind_to(instance=event_page, form=form)
self.assertIn('maxForms: 1000', panel.render_js_init())
示例9: TestPageChooserPanel
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import get_form_class [as 别名]
class TestPageChooserPanel(TestCase):
fixtures = ['test.json']
def setUp(self):
model = PageChooserModel # a model with a foreign key to Page which we want to render as a page chooser
# a PageChooserPanel class that works on PageChooserModel's 'page' field
self.EditHandler = ObjectList([PageChooserPanel('page')]).bind_to_model(PageChooserModel)
self.MyPageChooserPanel = self.EditHandler.children[0]
# build a form class containing the fields that MyPageChooserPanel wants
self.PageChooserForm = self.EditHandler.get_form_class(PageChooserModel)
# a test instance of PageChooserModel, pointing to the 'christmas' page
self.christmas_page = Page.objects.get(slug='christmas')
self.events_index_page = Page.objects.get(slug='events')
self.test_instance = model.objects.create(page=self.christmas_page)
self.form = self.PageChooserForm(instance=self.test_instance)
self.page_chooser_panel = self.MyPageChooserPanel(instance=self.test_instance, form=self.form)
def test_page_chooser_uses_correct_widget(self):
self.assertEqual(type(self.form.fields['page'].widget), AdminPageChooser)
def test_render_js_init(self):
result = self.page_chooser_panel.render_as_field()
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, false, null);'.format(
id="id_page", model="wagtailcore.page", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
def test_render_js_init_with_can_choose_root_true(self):
# construct an alternative page chooser panel object, with can_choose_root=True
MyPageObjectList = ObjectList([
PageChooserPanel('page', can_choose_root=True)
]).bind_to_model(PageChooserModel)
MyPageChooserPanel = MyPageObjectList.children[0]
PageChooserForm = MyPageObjectList.get_form_class(EventPageChooserModel)
form = PageChooserForm(instance=self.test_instance)
page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form)
result = page_chooser_panel.render_as_field()
# the canChooseRoot flag on createPageChooser should now be true
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, true, null);'.format(
id="id_page", model="wagtailcore.page", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
def test_get_chosen_item(self):
result = self.page_chooser_panel.get_chosen_item()
self.assertEqual(result, self.christmas_page)
def test_render_as_field(self):
result = self.page_chooser_panel.render_as_field()
self.assertIn('<p class="help">help text</p>', result)
self.assertIn('<span class="title">Christmas</span>', result)
self.assertIn(
'<a href="/admin/pages/%d/edit/" class="edit-link button button-small button-secondary" target="_blank">'
'Edit this page</a>' % self.christmas_page.id,
result)
def test_render_as_empty_field(self):
test_instance = PageChooserModel()
form = self.PageChooserForm(instance=test_instance)
page_chooser_panel = self.MyPageChooserPanel(instance=test_instance, form=form)
result = page_chooser_panel.render_as_field()
self.assertIn('<p class="help">help text</p>', result)
self.assertIn('<span class="title"></span>', result)
self.assertIn('Choose a page', result)
def test_render_error(self):
form = self.PageChooserForm({'page': ''}, instance=self.test_instance)
self.assertFalse(form.is_valid())
page_chooser_panel = self.MyPageChooserPanel(instance=self.test_instance, form=form)
self.assertIn('<span>This field is required.</span>', page_chooser_panel.render_as_field())
def test_override_page_type(self):
# Model has a foreign key to Page, but we specify EventPage in the PageChooserPanel
# to restrict the chooser to that page type
MyPageObjectList = ObjectList([
PageChooserPanel('page', 'tests.EventPage')
]).bind_to_model(EventPageChooserModel)
MyPageChooserPanel = MyPageObjectList.children[0]
PageChooserForm = MyPageObjectList.get_form_class(EventPageChooserModel)
form = PageChooserForm(instance=self.test_instance)
page_chooser_panel = MyPageChooserPanel(instance=self.test_instance, form=form)
result = page_chooser_panel.render_as_field()
expected_js = 'createPageChooser("{id}", ["{model}"], {parent}, false, null);'.format(
id="id_page", model="tests.eventpage", parent=self.events_index_page.id)
self.assertIn(expected_js, result)
def test_autodetect_page_type(self):
# Model has a foreign key to EventPage, which we want to autodetect
# instead of specifying the page type in PageChooserPanel
MyPageObjectList = ObjectList([PageChooserPanel('page')]).bind_to_model(EventPageChooserModel)
#.........这里部分代码省略.........