Python wagtail.core.blocks.ChoiceBlock() Examples
The following are 21
code examples of wagtail.core.blocks.ChoiceBlock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
wagtail.core.blocks
, or try the search function
.
Example #1
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_subclassing(self): class BeverageMultipleChoiceBlock(blocks.MultipleChoiceBlock): choices = [ ('tea', 'Tea'), ('coffee', 'Coffee'), ] block = BeverageMultipleChoiceBlock(required=False) html = block.render_form('tea', prefix='beverage') self.assertTagInHTML('<select multiple id="beverage" name="beverage" placeholder="">', html) self.assertInHTML('<option value="tea" selected="selected">Tea</option>', html) # subclasses of ChoiceBlock should deconstruct to a basic ChoiceBlock for migrations self.assertEqual( block.deconstruct(), ( 'wagtail.core.blocks.MultipleChoiceBlock', [], { 'choices': [('tea', 'Tea'), ('coffee', 'Coffee')], 'required': False, }, ) )
Example #2
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_deconstruct_with_callable_choices(self): def callable_choices(): return [ ('tea', 'Tea'), ('coffee', 'Coffee'), ] block = blocks.ChoiceBlock(choices=callable_choices, required=False) html = block.render_form('tea', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertInHTML('<option value="tea" selected="selected">Tea</option>', html) self.assertEqual( block.deconstruct(), ( 'wagtail.core.blocks.ChoiceBlock', [], { 'choices': callable_choices, 'required': False, }, ) )
Example #3
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_optgroup_searchable_content_with_lazy_translation(self): block = blocks.ChoiceBlock(choices=[ (__('Section 1'), [ ('1-1', __("Block 1")), ('1-2', __("Block 2")), ]), (__('Section 2'), [ ('2-1', __("Block 1")), ('2-2', __("Block 2")), ]), ]) result = block.get_searchable_content("2-2") # result must survive JSON (de)serialisation, which is not the case for # lazy translation objects result = json.loads(json.dumps(result)) self.assertEqual(result, ["Section 2", "Block 2"])
Example #4
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_subclassing(self): class BeverageChoiceBlock(blocks.ChoiceBlock): choices = [ ('tea', 'Tea'), ('coffee', 'Coffee'), ] block = BeverageChoiceBlock(required=False) html = block.render_form('tea', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertInHTML('<option value="tea" selected="selected">Tea</option>', html) # subclasses of ChoiceBlock should deconstruct to a basic ChoiceBlock for migrations self.assertEqual( block.deconstruct(), ( 'wagtail.core.blocks.ChoiceBlock', [], { 'choices': [('tea', 'Tea'), ('coffee', 'Coffee')], 'required': False, }, ) )
Example #5
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_named_groups_with_blank_option(self): block = blocks.ChoiceBlock( choices=[ ('Alcoholic', [ ('gin', 'Gin'), ('whisky', 'Whisky'), ]), ('Non-alcoholic', [ ('tea', 'Tea'), ('coffee', 'Coffee'), ]), ('Not thirsty', [ ('', 'No thanks') ]), ], required=False) # test rendering with the blank option selected html = block.render_form(None, prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertNotIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertNotInHTML('<option value="" selected="selected">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<optgroup label="Alcoholic">', html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="" selected="selected">No thanks</option>', html) # test rendering with a non-blank option selected html = block.render_form('tea', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertNotIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertNotInHTML('<option value="" selected="selected">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<optgroup label="Alcoholic">', html) self.assertInHTML('<option value="tea" selected="selected">Tea</option>', html)
Example #6
Source File: wagtailstreamforms_fields.py From wagtailstreamforms with MIT License | 5 votes |
def get_form_block(self): return blocks.StructBlock([ ('label', blocks.CharBlock()), ('help_text', blocks.CharBlock(required=False)), ('required', blocks.BooleanBlock(required=False)), ('regex', blocks.ChoiceBlock(choices=self.get_regex_choices())), ('error_message', blocks.CharBlock()), ('default_value', blocks.CharBlock(required=False)), ], icon=self.icon, label=self.label)
Example #7
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_with_validator(self): choices = [ ('tea', 'Tea'), ('coffee', 'Coffee'), ] def validate_tea_is_selected(value): raise ValidationError("You must select 'tea'") block = blocks.ChoiceBlock(choices=choices, validators=[validate_tea_is_selected]) with self.assertRaises(ValidationError): block.clean('coffee')
Example #8
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_searchable_content_with_lazy_translation(self): block = blocks.ChoiceBlock(choices=[ ('choice-1', __("Choice 1")), ('choice-2', __("Choice 2")), ]) result = block.get_searchable_content("choice-1") # result must survive JSON (de)serialisation, which is not the case for # lazy translation objects result = json.loads(json.dumps(result)) self.assertEqual(result, ["Choice 1"])
Example #9
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_optgroup_searchable_content(self): block = blocks.ChoiceBlock(choices=[ ('Section 1', [ ('1-1', "Block 1"), ('1-2', "Block 2"), ]), ('Section 2', [ ('2-1', "Block 1"), ('2-2', "Block 2"), ]), ]) self.assertEqual(block.get_searchable_content("2-2"), ["Section 2", "Block 2"])
Example #10
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_searchable_content_with_callable_choices(self): def callable_choices(): return [ ('choice-1', "Choice 1"), ('choice-2', "Choice 2"), ] block = blocks.ChoiceBlock(choices=callable_choices) self.assertEqual(block.get_searchable_content("choice-1"), ["Choice 1"])
Example #11
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_searchable_content(self): block = blocks.ChoiceBlock(choices=[ ('choice-1', "Choice 1"), ('choice-2', "Choice 2"), ]) self.assertEqual(block.get_searchable_content("choice-1"), ["Choice 1"])
Example #12
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_choicefield_render(self): class ChoiceBlock(blocks.FieldBlock): field = forms.ChoiceField(choices=( ('choice-1', "Choice 1"), ('choice-2', "Choice 2"), )) block = ChoiceBlock() html = block.render('choice-2') self.assertEqual(html, "choice-2")
Example #13
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_choice_block_with_existing_blank_choice_and_with_callable_choices(self): def callable_choices(): return [('tea', 'Tea'), ('coffee', 'Coffee'), ('', 'No thanks')] block = blocks.ChoiceBlock( choices=callable_choices, required=False) html = block.render_form(None, prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertNotIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertInHTML('<option value="" selected="selected">No thanks</option>', html) self.assertIn('<option value="tea">Tea</option>', html) self.assertIn('<option value="coffee">Coffee</option>', html)
Example #14
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_choice_block_with_existing_blank_choice(self): block = blocks.ChoiceBlock( choices=[('tea', 'Tea'), ('coffee', 'Coffee'), ('', 'No thanks')], required=False) html = block.render_form(None, prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertNotIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertInHTML('<option value="" selected="selected">No thanks</option>', html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee">Coffee</option>', html)
Example #15
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_validate_non_required_choice_block(self): block = blocks.ChoiceBlock(choices=[('tea', 'Tea'), ('coffee', 'Coffee')], required=False) self.assertEqual(block.clean('coffee'), 'coffee') with self.assertRaises(ValidationError): block.clean('whisky') self.assertEqual(block.clean(''), '') self.assertEqual(block.clean(None), '')
Example #16
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_non_required_choice_block_with_callable_choices(self): def callable_choices(): return [('tea', 'Tea'), ('coffee', 'Coffee')] block = blocks.ChoiceBlock(choices=callable_choices, required=False) html = block.render_form('coffee', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee" selected="selected">Coffee</option>', html)
Example #17
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_non_required_choice_block(self): block = blocks.ChoiceBlock(choices=[('tea', 'Tea'), ('coffee', 'Coffee')], required=False) html = block.render_form('coffee', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) self.assertIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee" selected="selected">Coffee</option>', html)
Example #18
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_required_choice_block_with_callable_choices(self): def callable_choices(): return [('tea', 'Tea'), ('coffee', 'Coffee')] block = blocks.ChoiceBlock(choices=callable_choices) html = block.render_form('coffee', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) # blank option should still be rendered for required fields # (we may want it as an initial value) self.assertIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee" selected="selected">Coffee</option>', html)
Example #19
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_required_choice_block_with_default(self): block = blocks.ChoiceBlock(choices=[('tea', 'Tea'), ('coffee', 'Coffee')], default='tea') html = block.render_form('coffee', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) # blank option should NOT be rendered if default and required are set. self.assertNotIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee" selected="selected">Coffee</option>', html)
Example #20
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_required_choice_block(self): block = blocks.ChoiceBlock(choices=[('tea', 'Tea'), ('coffee', 'Coffee')]) html = block.render_form('coffee', prefix='beverage') self.assertTagInHTML('<select id="beverage" name="beverage" placeholder="">', html) # blank option should still be rendered for required fields # (we may want it as an initial value) self.assertIn('<option value="">%s</option>' % self.blank_choice_dash_label, html) self.assertIn('<option value="tea">Tea</option>', html) self.assertInHTML('<option value="coffee" selected="selected">Coffee</option>', html)
Example #21
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_choicefield_render_form(self): class ChoiceBlock(blocks.FieldBlock): field = forms.ChoiceField(choices=( ('choice-1', "Choice 1"), ('choice-2', "Choice 2"), )) block = ChoiceBlock() html = block.render_form('choice-2') self.assertIn('<div class="field choice_field widget-select">', html) self.assertTagInHTML('<select id="" name="" placeholder="">', html) self.assertInHTML('<option value="choice-1">Choice 1</option>', html) self.assertInHTML('<option value="choice-2" selected="selected">Choice 2</option>', html)