From 2d0512c72b2a8feb6fa10c3cd8b8c53bc74a9ba6 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 28 Apr 2021 09:55:25 +0200 Subject: WIP: preventive forms --- .../templates/blocks/bs_formset_snippet.html | 26 +++++++++++ .../templates/ishtar/forms/base_form.html | 50 +++++++++++++++++++++ ishtar_common/templates/ishtar/forms/document.html | 51 +++------------------- .../templates/widgets/CentimeterMeterWidget.html | 21 --------- .../templates/widgets/GramKilogramWidget.html | 21 --------- .../templates/widgets/SquareMeterWidget.html | 21 --------- ishtar_common/templates/widgets/UnitWidget.html | 21 +++++++++ ishtar_common/templatetags/table_form.py | 9 ++++ ishtar_common/widgets.py | 17 +++++--- 9 files changed, 122 insertions(+), 115 deletions(-) create mode 100644 ishtar_common/templates/blocks/bs_formset_snippet.html create mode 100644 ishtar_common/templates/ishtar/forms/base_form.html delete mode 100644 ishtar_common/templates/widgets/CentimeterMeterWidget.html delete mode 100644 ishtar_common/templates/widgets/GramKilogramWidget.html delete mode 100644 ishtar_common/templates/widgets/SquareMeterWidget.html create mode 100644 ishtar_common/templates/widgets/UnitWidget.html (limited to 'ishtar_common') diff --git a/ishtar_common/templates/blocks/bs_formset_snippet.html b/ishtar_common/templates/blocks/bs_formset_snippet.html new file mode 100644 index 000000000..0d089ee1b --- /dev/null +++ b/ishtar_common/templates/blocks/bs_formset_snippet.html @@ -0,0 +1,26 @@ +{% load i18n from_dict %} +
+ {% for form in formset %} + {% if form.non_field_errors and not no_error %} + + {% endif %} + + {% for hidden in form.hidden_fields %} + {{hidden}} + {% if hidden.errors %}
+ {{ hidden.errors }} +
{% endif %} + {% endfor %} + + {% csrf_token %} + {% for field in form.visible_fields %} + {% include "blocks/bs_field_snippet.html" %} + {% endfor %} + + {% if form.extra_render %} + {{form.extra_render|safe}} + {% endif %} + {% endfor %} +
diff --git a/ishtar_common/templates/ishtar/forms/base_form.html b/ishtar_common/templates/ishtar/forms/base_form.html new file mode 100644 index 000000000..fb1fc997f --- /dev/null +++ b/ishtar_common/templates/ishtar/forms/base_form.html @@ -0,0 +1,50 @@ +{% extends "base.html" %} +{% load i18n inline_formset table_form from_dict %} +{% block extra_head %} +{{form.media}} +{% endblock %} + +{% block pre_container %} +
{% csrf_token %} +{% endblock %} +{% block content %} +

{{page_name}}

+
+ {% if form.non_field_errors or form.errors %} +
+
+ {% trans "Error on validation. Check all your fields. Modification not saved." %} +
+ {% for key, error_details in form.errors.items %} + {% with field=key|from_dict:form.fields %}{{ field.label }}{{error_details}}{% endwith %} + {% endfor %} +
+ {% endif %} + {% block form_head %} + {% endblock %} + {% bs_form form 0 True %} + {% for inline in inline_forms %} + {% bs_formset inline 0 True %} + {% endfor %} +
+{% endblock %} + +{% block footer %} + +
+{% endblock %} + diff --git a/ishtar_common/templates/ishtar/forms/document.html b/ishtar_common/templates/ishtar/forms/document.html index 8e2683a03..fe3df8c74 100644 --- a/ishtar_common/templates/ishtar/forms/document.html +++ b/ishtar_common/templates/ishtar/forms/document.html @@ -1,48 +1,7 @@ -{% extends "base.html" %} -{% load i18n inline_formset table_form from_dict %} -{% block extra_head %} -{{form.media}} -{% endblock %} - -{% block pre_container %} -
{% csrf_token %} -{% endblock %} -{% block content %} -

{{page_name}}

-
- {% if form.non_field_errors or form.errors %} -
-
- {% trans "Error on validation. Check all your fields. Modification not saved." %} -
- {% for key, error_details in form.errors.items %} - {% with field=key|from_dict:form.fields %}{{ field.label }}{{error_details}}{% endwith %} - {% endfor %} -
- {% endif %} - {% if item_related_label %}
+{% extends "ishtar/forms/base_form.html" %} +{% load i18n %} +{% block form_head %} +{% if item_related_label %}
{% trans "Related items" %}{% trans ":" %} {{ item_related_label }} -
{% endif %} - {% bs_form form 0 True %} -
-{% endblock %} - -{% block footer %} - - +
{% endif %} {% endblock %} - diff --git a/ishtar_common/templates/widgets/CentimeterMeterWidget.html b/ishtar_common/templates/widgets/CentimeterMeterWidget.html deleted file mode 100644 index 00c1614b5..000000000 --- a/ishtar_common/templates/widgets/CentimeterMeterWidget.html +++ /dev/null @@ -1,21 +0,0 @@ -
- -
-
- {{unit}} (0 m) -
-
-
- diff --git a/ishtar_common/templates/widgets/GramKilogramWidget.html b/ishtar_common/templates/widgets/GramKilogramWidget.html deleted file mode 100644 index 27c066d13..000000000 --- a/ishtar_common/templates/widgets/GramKilogramWidget.html +++ /dev/null @@ -1,21 +0,0 @@ -
- -
-
- {{unit}} (0 kg) -
-
-
- diff --git a/ishtar_common/templates/widgets/SquareMeterWidget.html b/ishtar_common/templates/widgets/SquareMeterWidget.html deleted file mode 100644 index 09e93b425..000000000 --- a/ishtar_common/templates/widgets/SquareMeterWidget.html +++ /dev/null @@ -1,21 +0,0 @@ -
- -
-
- {{unit}} (0 ha) -
-
-
- diff --git a/ishtar_common/templates/widgets/UnitWidget.html b/ishtar_common/templates/widgets/UnitWidget.html new file mode 100644 index 000000000..46b883352 --- /dev/null +++ b/ishtar_common/templates/widgets/UnitWidget.html @@ -0,0 +1,21 @@ +
+ +
+
+ {{unit1}} (0 {{unit2}}) +
+
+
+ diff --git a/ishtar_common/templatetags/table_form.py b/ishtar_common/templatetags/table_form.py index d5b20da26..819d99c50 100644 --- a/ishtar_common/templatetags/table_form.py +++ b/ishtar_common/templatetags/table_form.py @@ -16,6 +16,15 @@ def bs_form(context, form, position=0, no_error=False): 'no_error': no_error} +@register.inclusion_tag('blocks/bs_formset_snippet.html', takes_context=True) +def bs_formset(context, formset, position=0, no_error=False): + user = context['user'] + show_field_number = user.ishtaruser and user.ishtaruser.show_field_number() + return {'formset': formset, 'odd': position % 2, + 'show_field_number': show_field_number, + 'no_error': no_error} + + @register.inclusion_tag('blocks/bs_compact_form_snippet.html', takes_context=True) def bs_compact_form(context, form, position=0): diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 8f95f4521..008925276 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -521,18 +521,23 @@ class CustomWidget(forms.TextInput): class SquareMeterWidget(CustomWidget): - TEMPLATE = "widgets/SquareMeterWidget.html" - EXTRA_DCT = {"unit": settings.SURFACE_UNIT_LABEL} + TEMPLATE = "widgets/UnitWidget.html" + EXTRA_DCT = {"unit1": "m²", "unit2": "ha", "factor": 10000} class GramKilogramWidget(CustomWidget): - TEMPLATE = "widgets/GramKilogramWidget.html" - EXTRA_DCT = {"unit": "g"} + TEMPLATE = "widgets/UnitWidget.html" + EXTRA_DCT = {"unit1": "g", "unit2": "kg", "factor": 1000} class CentimeterMeterWidget(CustomWidget): - TEMPLATE = "widgets/CentimeterMeterWidget.html" - EXTRA_DCT = {"unit": "cm"} + TEMPLATE = "widgets/UnitWidget.html" + EXTRA_DCT = {"unit1": "cm", "unit2": "m", "factor": 100} + + +class MeterKilometerWidget(CustomWidget): + TEMPLATE = "widgets/UnitWidget.html" + EXTRA_DCT = {"unit1": "m", "unit2": "km", "factor": 1000} AreaWidget = forms.TextInput -- cgit v1.2.3