def new_field(self): choices = [ Choice(1, Field(label='One')), Choice(2, Field(label='Two')) ] field = ChoiceField(choices) field.bind('field', self) return field
def test_checkbox_select_input_allowed_fields(web_fixture): """A CheckboxSelectInput can only be used with a MultiChoiceField.""" model_object = web_fixture form = Form(web_fixture.view, 'test') choice_field = ChoiceField([]) choice_field.bind('an_attribute', model_object) with expected(ProgrammerError, test='<class \'reahl.component.modelinterface.ChoiceField\'> is not allowed to be used with <class \'reahl.web.ui.CheckboxSelectInput\'>'): CheckboxSelectInput(form, choice_field) multi_choice_field = MultiChoiceField([]) multi_choice_field.bind('another_attribute', model_object) with expected(NoException): CheckboxSelectInput(form, multi_choice_field)
class Scenarios(WebFixture, InputMixin): @scenario def text_input(self): self.widget = TextInput(self.form, self.field) self.expected_html = r'<input name="an_attribute" form="test" type="text" value="field value" class="reahl-textinput">' self.field_controls_visibility = True @scenario def text_input_placeholder_default(self): self.widget = TextInput(self.form, self.field, placeholder=True) self.expected_html = r'<input name="an_attribute" form="test" placeholder="the label" type="text" value="field value" class="reahl-textinput">' self.field_controls_visibility = True @scenario def text_input_placeholder_specified(self): self.widget = TextInput(self.form, self.field, placeholder="some text") self.expected_html = r'<input name="an_attribute" form="test" placeholder="some text" type="text" value="field value" class="reahl-textinput">' self.field_controls_visibility = True @scenario def input_label(self): html_input = TextInput(self.form, self.field) self.widget = Label(self.view, for_input=html_input) self.expected_html = r'<label for="%s">the label</label>' % html_input.css_id self.field_controls_visibility = True @scenario def input_label_with_text(self): html_input = TextInput(self.form, self.field) self.widget = Label(self.view, for_input=html_input, text='some text') self.expected_html = r'<label for="%s">some text</label>' % html_input.css_id self.field_controls_visibility = True @scenario def button_input(self): self.widget = self.form.add_child(ButtonInput(self.form, self.event)) self.expected_html = r'<input name="event.aname\?" form="test" type="submit" value="click me">' self.field_controls_visibility = False @scenario def password(self): self.widget = self.form.add_child(PasswordInput(self.form, self.field)) self.expected_html = r'<input name="an_attribute" form="test" type="password">' self.field_controls_visibility = True def setup_checkbox_scenario(self, boolean_value): self.model_object.an_attribute = boolean_value self.field = BooleanField(required=True, label='my text', required_message='$label is needed here') self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(CheckboxInput(self.form, self.field)) self.field_controls_visibility = True @scenario def checkbox_true(self): """A checkbox needs a 'checked' an_attribute if its field is True. It also renders ONLY the validation message for its required validation_constraint, not for all constraints""" self.setup_checkbox_scenario(True) self.expected_html = r'<input name="an_attribute" checked="checked" form="test" required="\*" type="checkbox" ' \ r'class="{"validate": {"messages": {"required": "my text is needed here"}}}">' @scenario def checkbox_false(self): self.setup_checkbox_scenario(False) self.expected_html = r'<input name="an_attribute" form="test" required="\*" type="checkbox" '\ r'class="{"validate": {"messages": {"required": "my text is needed here"}}}">' @scenario def text_area_input(self): self.widget = self.form.add_child( TextArea(self.form, self.field, rows=30, columns=20)) self.expected_html = r'<textarea name="an_attribute" cols="20" rows="30">field value</textarea>' self.field_controls_visibility = True @scenario def select_input(self): self.model_object.an_attribute = 2 choices = [ Choice(False, BooleanField(label='None')), ChoiceGroup('grouped', [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ]) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) group = r'<optgroup label="grouped"><option value="1">One</option><option selected="selected" value="2">Two</option></optgroup>' option = r'<option value="off">None</option>' self.expected_html = r'<select name="an_attribute" form="test">%s%s</select>' % ( option, group) self.field_controls_visibility = True @scenario def select_input_multi(self): self.model_object.an_attribute = [2] choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ] self.field = MultiChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) options = r'<option value="1">One</option><option selected="selected" value="2">Two</option>' self.expected_html = r'<select name="an_attribute" form="test" multiple="multiple">%s</select>' % ( options) self.field_controls_visibility = True @scenario def radio_button(self): self.model_object.an_attribute = 2 choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child( RadioButtonInput(self.form, self.field)) validations = html_escape( '{"validate": {"messages": {"pattern": "an_attribute should be one of the following: 1\|2"}}}' ) radio_button = r'<span class="reahl-radio-button">'\ r'<input name="an_attribute"%s form="test" pattern="\(1\|2\)" '\ r'title="an_attribute should be one of the following: 1\|2" '\ r'type="radio" value="%s" class="%s">%s'\ r'</span>' outer_div = r'<div class="reahl-radio-button-input">%s</div>' buttons = (radio_button % ('', '1', validations, 'One')) +\ (radio_button % (' checked="checked"', '2', validations, 'Two')) self.expected_html = outer_div % buttons self.field_controls_visibility = True
class InputScenarios(SimpleInputFixture): @scenario def text_input(self): self.widget = TextInput(self.form, self.field) self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" form="test" type="text" value="field value" class="reahl-primitiveinput reahl-textinput">' self.field_controls_visibility = True @scenario def text_input_placeholder_default(self): self.widget = TextInput(self.form, self.field, placeholder=True) self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" aria-label="the label" form="test" placeholder="the label" type="text" value="field value" class="reahl-primitiveinput reahl-textinput">' self.field_controls_visibility = True @scenario def text_input_placeholder_specified(self): self.widget = TextInput(self.form, self.field, placeholder="some text") self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" aria-label="some text" form="test" placeholder="some text" type="text" value="field value" class="reahl-primitiveinput reahl-textinput">' self.field_controls_visibility = True @scenario def input_label(self): html_input = TextInput(self.form, self.field) self.widget = Label(self.web_fixture.view, for_input=html_input) self.expected_html = '<label for="%s">the label</label>' % html_input.css_id self.field_controls_visibility = True @scenario def input_label_with_text(self): html_input = TextInput(self.form, self.field) self.widget = Label(self.web_fixture.view, for_input=html_input, text='some text') self.expected_html = '<label for="%s">some text</label>' % html_input.css_id self.field_controls_visibility = True @scenario def button_input(self): self.widget = self.form.add_child(ButtonInput(self.form, self.event)) self.expected_html = '<input name="event.test-aname?" id="id-event-46-test-aname-63-" form="test" type="submit" value="click me" class="reahl-primitiveinput">' self.field_controls_visibility = False @scenario def password(self): self.widget = self.form.add_child(PasswordInput(self.form, self.field)) self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" form="test" type="password" class="reahl-primitiveinput">' self.field_controls_visibility = True def setup_checkbox_scenario(self, boolean_value): self.model_object.an_attribute = boolean_value self.field = BooleanField(required=True, label='my text', required_message='$label is needed here') self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(CheckboxInput(self.form, self.field)) self.field_controls_visibility = True @scenario def checkbox_true(self): """A checkbox needs a 'checked' an_attribute if its field is True. It also renders ONLY the validation message for its required validation_constraint, not for all constraints""" self.setup_checkbox_scenario(True) self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" checked="checked" data-false-boolean-value="off" data-is-boolean="" data-msg-required="my text is needed here" data-true-boolean-value="on" form="test" required="*" type="checkbox" class="reahl-primitiveinput">' @scenario def checkbox_false(self): self.setup_checkbox_scenario(False) self.expected_html = '<input name="test-an_attribute" id="id-test-an_attribute" data-false-boolean-value="off" data-is-boolean="" data-msg-required="my text is needed here" data-true-boolean-value="on" form="test" required="*" type="checkbox" class="reahl-primitiveinput">' @scenario def text_area_input(self): self.widget = self.form.add_child(TextArea(self.form, self.field, rows=30, columns=20)) self.expected_html = '<textarea name="test-an_attribute" id="id-test-an_attribute" cols="20" rows="30" class="reahl-primitiveinput">field value</textarea>' self.field_controls_visibility = True @scenario def select_input(self): self.model_object.an_attribute = 2 choices = [Choice(False, BooleanField(label='None')), ChoiceGroup('grouped', [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))]) ] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) group = '<optgroup label="grouped">'+\ '<option id="id-test-an_attribute-1" value="1">One</option>'+\ '<option id="id-test-an_attribute-2" selected="selected" value="2">Two</option>'+\ '</optgroup>' option = '<option id="id-test-an_attribute-off" value="off">None</option>' self.expected_html = r'<select name="test-an_attribute" id="id-test-an_attribute" form="test" class="reahl-primitiveinput">%s%s</select>' % (option, group) self.field_controls_visibility = True @scenario def select_input_multi(self): self.model_object.an_attribute = [2] choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))] self.field = MultiChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(SelectInput(self.form, self.field)) options = '<option id="id-test-an_attribute-91--93--1" value="1">One</option><option id="id-test-an_attribute-91--93--2" selected="selected" value="2">Two</option>' self.expected_html = '<select name="test-an_attribute[]" id="id-test-an_attribute-91--93-" form="test" multiple="multiple" class="reahl-primitiveinput">%s</select>' % (options) self.field_controls_visibility = True @scenario def checkbox_input_multi(self): selected_checkboxes = [2, 3] self.model_object.an_attribute = selected_checkboxes choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ] self.field = MultiChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(CheckboxSelectInput(self.form, self.field)) not_checked_option = r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--1" form="test" type="checkbox" value="1">One</label>' checked_options = r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--2" checked="checked" form="test" type="checkbox" value="2">Two</label>' checked_options += r'<label><input name="test-an_attribute[]" id="id-test-an_attribute-91--93--3" checked="checked" form="test" type="checkbox" value="3">Three</label>' options = not_checked_option + checked_options self.expected_html = r'<div class="reahl-checkbox-input reahl-primitiveinput">%s</div>' % (options) self.field_controls_visibility = True @scenario def radio_button(self): self.model_object.an_attribute = 2 choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))] self.field = ChoiceField(choices) self.field.bind('an_attribute', self.model_object) self.widget = self.form.add_child(RadioButtonSelectInput(self.form, self.field)) radio_button = '<label>'\ '<input name="test-an_attribute" id="id-test-an_attribute-%s"%s '\ 'data-msg-pattern="an_attribute should be one of the following: 1|2" '\ '%s'\ 'form="test" pattern="(1|2)" '\ 'title="an_attribute should be one of the following: 1|2" '\ 'type="radio" value="%s">%s'\ '</label>' outer_div = '<div class="reahl-primitiveinput reahl-radio-button-input">%s</div>' buttons = (radio_button % ('1', '', '', '1', 'One')) +\ (radio_button % ('2', ' checked="checked"', '', '2', 'Two')) self.expected_html = outer_div % buttons self.field_controls_visibility = True