Skip to content

Commit

Permalink
Merge pull request #15 from DaeronAlagos/14-add-fieldset
Browse files Browse the repository at this point in the history
[WIP] Add formset template
  • Loading branch information
ckrybus authored Mar 28, 2024
2 parents a7c228b + 6340f8f commit 494eb2f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 10 deletions.
8 changes: 8 additions & 0 deletions crispy_bulma/templates/bulma/errors_formset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if formset.non_form_errors %}
<div class="alert alert-block alert-danger">
{% if formset_error_title %}<h4 class="alert-heading">{{ formset_error_title }}</h4>{% endif %}
<ul class="m-0">
{{ formset.non_form_errors|unordered_list }}
</ul>
</div>
{% endif %}
6 changes: 6 additions & 0 deletions crispy_bulma/templates/bulma/layout/fieldset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<fieldset {% if fieldset.css_id %}id="{{ fieldset.css_id }}"{% endif %}
{% if fieldset.css_class or form_style %}class="{{ fieldset.css_class }} {{ form_style }}"{% endif %}
{{ fieldset.flat_attrs }}>
{% if legend %}<legend>{{ legend|safe }}</legend>{% endif %}
{{ fields|safe }}
</fieldset>
57 changes: 57 additions & 0 deletions crispy_bulma/templates/bulma/table_inline_formset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{% load crispy_forms_tags %}
{% load crispy_forms_utils %}
{% load crispy_forms_field %}

{% specialspaceless %}
{% if formset_tag %}
<form {{ flat_attrs }} method="{{ form_method }}" {% if formset.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% endif %}
{% if formset_method|lower == 'post' and not disable_csrf %}
{% csrf_token %}
{% endif %}

<div>
{{ formset.management_form|crispy }}
</div>

<table{% if form_id %} id="{{ form_id }}_table"{% endif%} class="table table-striped table-sm">
<thead>
{% if formset.readonly and not formset.queryset.exists %}
{% else %}
<tr>
{% for field in formset.forms.0 %}
{% if field.label and not field.is_hidden %}
<th for="{{ field.auto_id }}" class="{% if field.field.required %}requiredField{% endif %}">
{{ field.label }}{% if field.field.required and not field|is_checkbox %}<span class="asteriskField">*</span>{% endif %}
</th>
{% endif %}
{% endfor %}
</tr>
{% endif %}
</thead>

<tbody>
<tr class="d-none empty-form">
{% for field in formset.empty_form %}
{% include 'bulma/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr>

{% for form in formset %}
{% if form_show_errors and not form.is_extra %}
{% include "bulma/errors.html" %}
{% endif %}

<tr>
{% for field in form %}
{% include 'bulma/field.html' with tag="td" form_show_labels=False %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>

{% include "bulma/inputs.html" %}

{% if formset_tag %}</form>{% endif %}
{% endspecialspaceless %}
30 changes: 30 additions & 0 deletions crispy_bulma/templates/bulma/whole_uni_formset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% load crispy_forms_tags %}
{% load crispy_forms_utils %}

{% specialspaceless %}
{% if formset_tag %}
<form {{ flat_attrs }} method="{{ form_method }}" {% if formset.is_multipart %} enctype="multipart/form-data"{% endif %}>
{% endif %}
{% if formset_method|lower == 'post' and not disable_csrf %}
{% csrf_token %}
{% endif %}

<div>
{{ formset.management_form|crispy }}
</div>

{% include "bulma/errors_formset.html" %}

{% for form in formset %}
{% include "bulma/display_form.html" %}
{% endfor %}

{% if inputs %}
<div class="form-actions">
{% for input in inputs %}
{% include "bulma/layout/baseinput.html" %}
{% endfor %}
</div>
{% endif %}
{% if formset_tag %}</form>{% endif %}
{% endspecialspaceless %}
15 changes: 5 additions & 10 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def test_layout_fieldset_row_html_with_unicode_fieldnames():
assert "testLink" in html


@pytest.mark.skip(reason="fieldset")
def test_change_layout_dynamically_delete_field():
template = Template(
"""
Expand Down Expand Up @@ -240,7 +239,6 @@ def test_change_layout_dynamically_delete_field():
assert "email" not in html


@pytest.mark.skip(reason="fieldset")
def test_column_has_css_classes():
template = Template(
"""
Expand Down Expand Up @@ -269,10 +267,9 @@ def test_column_has_css_classes():
html = template.render(c)

assert html.count("formColumn") == 0
assert html.count("col-md") == 1
assert html.count("column") == 1


@pytest.mark.skip(reason="formset")
def test_formset_layout():
SampleFormSet = formset_factory(SampleForm, extra=3)
formset = SampleFormSet()
Expand Down Expand Up @@ -332,12 +329,11 @@ def test_formset_layout():
assert "Item 2" in html
assert "Item 3" in html
assert html.count("Note for first form only") == 1
assert html.count("row") == 3
assert html.count("columns") == 3

assert html.count("mb-3") == 21
assert html.count("control") == 18


@pytest.mark.skip(reason="formset")
def test_modelformset_layout():
CrispyModelFormSet = modelformset_factory(
CrispyTestModel, form=SampleForm4, extra=3
Expand Down Expand Up @@ -375,7 +371,6 @@ def test_modelformset_layout():
assert html.count("password") == 0


@pytest.mark.skip(reason="fieldset")
def test_i18n():
template = Template(
"""
Expand Down Expand Up @@ -517,7 +512,7 @@ def test_tabular_formset_layout():
SampleFormSet = formset_factory(SampleForm, extra=3)
formset = SampleFormSet()
formset.helper = FormHelper()
formset.helper.template = "bootstrap5/table_inline_formset.html"
formset.helper.template = "bulma/table_inline_formset.html"
assert parse_form(formset) == parse_expected("test_tabular_formset_layout.html")

SampleFormSet = formset_factory(SampleForm, extra=3)
Expand All @@ -527,7 +522,7 @@ def test_tabular_formset_layout():
}
formset = SampleFormSet(data)
formset.helper = FormHelper()
formset.helper.template = "bootstrap5/table_inline_formset.html"
formset.helper.template = "bulma/table_inline_formset.html"
assert parse_form(formset) == parse_expected(
"test_tabular_formset_layout_failing.html"
)
Expand Down

0 comments on commit 494eb2f

Please sign in to comment.