def test_submit_cancel_form_helper(self): """Test custom crispy forms layout helper""" template = Engine().get_default().from_string(""" {% load crispy_forms_tags %} {% crispy form %} """) test_form = SampleForm() test_form.helper = SubmitCancelFormHelper(test_form) c = Context({'form': test_form}) html = template.render(c) self.assertEqual(html.count('role="button"'), 1) self.assertEqual(html.count('href="#"'), 1) self.assertEqual(html.count('Cancel'), 1) self.assertEqual(html.count('Submit'), 1) test_form = SampleForm() test_form.helper = SubmitCancelFormHelper(test_form, cancel_href="/some/url/") c = Context({'form': test_form}) html = template.render(c) self.assertEqual(html.count('href="/some/url/'), 1)
def test_filter_by_widget(advanced_layout): form = SampleForm() form.helper = FormHelper(form) form.helper.layout = advanced_layout assert form.helper.filter_by_widget(forms.PasswordInput).slice == [ [[0, 1, 0, 0], 'password1'], [[0, 4, 0], 'password2'], ]
def test_exclude_by_widget(advanced_layout): form = SampleForm() form.helper = FormHelper(form) form.helper.layout = advanced_layout assert form.helper.exclude_by_widget(forms.PasswordInput).slice == [ [[0, 0, 0, 0], 'email'], [[0, 3, 0], 'first_name'], [[1], 'last_name'], ]
def test_exclude_by_widget_and_wrap(advanced_layout): form = SampleForm() form.helper = FormHelper(form) form.helper.layout = advanced_layout form.helper.exclude_by_widget(forms.PasswordInput).wrap(Field, css_class='hero') # Check wrapped fields assert isinstance(form.helper.layout[0][0][0][0], Field) assert isinstance(form.helper.layout[0][3][0], Field) assert isinstance(form.helper.layout[1], Field) # Check others stay the same assert isinstance(form.helper.layout[0][3][1], HTML) assert isinstance(form.helper.layout[0][1][0][0], string_types) assert isinstance(form.helper.layout[0][4][0], string_types)
def test_exclude_by_widget_and_wrap(advanced_layout): form = SampleForm() form.helper = FormHelper(form) form.helper.layout = advanced_layout form.helper.exclude_by_widget(forms.PasswordInput).wrap(Field, css_class='hero') # Check wrapped fields assert isinstance(form.helper.layout[0][0][0][0], Field) assert isinstance(form.helper.layout[0][3][0], Field) assert isinstance(form.helper.layout[1], Field) # Check others stay the same assert isinstance(form.helper.layout[0][3][1], HTML) assert isinstance(form.helper.layout[0][1][0][0], str) assert isinstance(form.helper.layout[0][4][0], str)
def test_filter_by_widget_without_form(advanced_layout): form = SampleForm() form.helper = FormHelper() form.helper.layout = advanced_layout with pytest.raises(FormHelpersException): form.helper.filter_by_widget(forms.PasswordInput)
def test_all_without_layout(): form = SampleForm() form.helper = FormHelper() with pytest.raises(FormHelpersException): form.helper.all().wrap(Div)