diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-04-28 09:55:25 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-07-08 09:58:47 +0200 |
commit | 96ad40f28ff3e17f8173ec8de8b63ef6056e6e55 (patch) | |
tree | 5284082668976459d1b304532f883aad0f8044a9 /ishtar_common | |
parent | c706c72acceef2665bd0a44214401fcb9cd38033 (diff) | |
download | Ishtar-96ad40f28ff3e17f8173ec8de8b63ef6056e6e55.tar.bz2 Ishtar-96ad40f28ff3e17f8173ec8de8b63ef6056e6e55.zip |
WIP: preventive forms
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/templates/blocks/bs_formset_snippet.html | 26 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/forms/base_form.html | 50 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/forms/document.html | 51 | ||||
-rw-r--r-- | ishtar_common/templates/widgets/GramKilogramWidget.html | 21 | ||||
-rw-r--r-- | ishtar_common/templates/widgets/SquareMeterWidget.html | 21 | ||||
-rw-r--r-- | ishtar_common/templates/widgets/UnitWidget.html (renamed from ishtar_common/templates/widgets/CentimeterMeterWidget.html) | 6 | ||||
-rw-r--r-- | ishtar_common/templatetags/table_form.py | 9 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 17 |
8 files changed, 104 insertions, 97 deletions
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 %} +<div class="form-row"> + {% for form in formset %} + {% if form.non_field_errors and not no_error %} + <div class="alert alert-danger" role="alert"> + {{form.non_field_errors}} + </div> + {% endif %} + + {% for hidden in form.hidden_fields %} + {{hidden}} + {% if hidden.errors %}<div class="invalid-feedback"> + {{ hidden.errors }} + </div>{% 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 %} +</div> 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 %} +<form enctype="multipart/form-data" action="." method="post"{% if confirm %} + onsubmit='return confirm("{{confirm}}");'{% endif %}>{% csrf_token %} +{% endblock %} +{% block content %} +<h2>{{page_name}}</h2> +<div class='form{% if not form.SEARCH_AND_SELECT %} container{% endif %}'> + {% if form.non_field_errors or form.errors %} + <div class="alert alert-danger"> + <div><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> + {% trans "Error on validation. Check all your fields. Modification not saved." %} + </div> + {% for key, error_details in form.errors.items %} + {% with field=key|from_dict:form.fields %}<strong>{{ field.label }}</strong>{{error_details}}{% endwith %} + {% endfor %} + </div> + {% endif %} + {% block form_head %} + {% endblock %} + {% bs_form form 0 True %} + {% for inline in inline_forms %} + {% bs_formset inline 0 True %} + {% endfor %} +</div> +{% endblock %} + +{% block footer %} +<div id="footer"> + {% if form.SEARCH_AND_SELECT %} + <p class="confirm-message">{% trans "Search and select an item in the table" %}</p> + {% endif %} + <div id='validation-bar' class="row text-center"> + <div class="col-sm"> + <button type="submit" id="submit_form" name='validate' + value="validate" class="btn btn-success"> + {% if submit_label %}{{submit_label}}{% else%}{% trans "Validate" %}{% endif %} + </button> + </div> + </div> + {% include 'ishtar/blocks/footer.html' %} +</div> +</form> +{% 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 %} -<form enctype="multipart/form-data" action="." method="post"{% if confirm %} - onsubmit='return confirm("{{confirm}}");'{% endif %}>{% csrf_token %} -{% endblock %} -{% block content %} -<h2>{{page_name}}</h2> -<div class='form{% if not form.SEARCH_AND_SELECT %} container{% endif %}'> - {% if form.non_field_errors or form.errors %} - <div class="alert alert-danger"> - <div><i class="fa fa-exclamation-triangle" aria-hidden="true"></i> - {% trans "Error on validation. Check all your fields. Modification not saved." %} - </div> - {% for key, error_details in form.errors.items %} - {% with field=key|from_dict:form.fields %}<strong>{{ field.label }}</strong>{{error_details}}{% endwith %} - {% endfor %} - </div> - {% endif %} - {% if item_related_label %}<div class="alert alert-info"> +{% extends "ishtar/forms/base_form.html" %} +{% load i18n %} +{% block form_head %} +{% if item_related_label %}<div class="alert alert-info"> <strong>{% trans "Related items" %}{% trans ":" %}</strong> {{ item_related_label }} - </div>{% endif %} - {% bs_form form 0 True %} -</div> -{% endblock %} - -{% block footer %} -<div id="footer"> - {% if form.SEARCH_AND_SELECT %} - <p class="confirm-message">{% trans "Search and select an item in the table" %}</p> - {% endif %} - <div id='validation-bar' class="row text-center"> - <div class="col-sm"> - <button type="submit" id="submit_form" name='validate' - value="validate" class="btn btn-success"> - {% if submit_label %}{{submit_label}}{% else%}{% trans "Validate" %}{% endif %} - </button> - </div> - </div> - {% include 'ishtar/blocks/footer.html' %} -</div> -</form> +</div>{% endif %} {% endblock %} - 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 @@ -<div class="input-group"> - <input class="area_widget form-control" type="text"{{final_attrs|safe}}> - <div class="input-group-append"> - <div class="input-group-text"> - {{unit}} (<span id="kg_{{id}}">0</span> kg) - </div> - </div> -</div> -<script type="text/javascript"><!--// - function evaluate_{{safe_id}}(){ - value = parseFloat($("#{{id}}").val()); - if(!isNaN(value)){ - value = value/1000; - } else { - value = 0; - } - $("#kg_{{id}}").html(value); - } - $("#{{id}}").keyup(evaluate_{{safe_id}}); - $(document).ready(evaluate_{{safe_id}}()); -//--></script> 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 @@ -<div class="input-group"> - <input class="area_widget form-control" type="text"{{final_attrs|safe}}> - <div class="input-group-append"> - <div class="input-group-text"> - {{unit}} (<span id="ha_{{id}}">0</span> ha) - </div> - </div> -</div> -<script type="text/javascript"><!--// - function evaluate_{{safe_id}}(){ - value = parseFloat($("#{{id}}").val()); - if(!isNaN(value)){ - value = value/10000; - } else { - value = 0; - } - $("#ha_{{id}}").html(value); - } - $("#{{id}}").keyup(evaluate_{{safe_id}}); - $(document).ready(evaluate_{{safe_id}}()); -//--></script> diff --git a/ishtar_common/templates/widgets/CentimeterMeterWidget.html b/ishtar_common/templates/widgets/UnitWidget.html index 00c1614b5..46b883352 100644 --- a/ishtar_common/templates/widgets/CentimeterMeterWidget.html +++ b/ishtar_common/templates/widgets/UnitWidget.html @@ -2,7 +2,7 @@ <input class="area_widget form-control" type="text"{{final_attrs|safe}}> <div class="input-group-append"> <div class="input-group-text"> - {{unit}} (<span id="meter_{{id}}">0</span> m) + {{unit1}} (<span id="subunit_{{id}}">0</span> {{unit2}}) </div> </div> </div> @@ -10,11 +10,11 @@ function evaluate_{{safe_id}}(){ value = parseFloat($("#{{id}}").val()); if(!isNaN(value)){ - value = value/100; + value = value/{{factor}}; } else { value = 0; } - $("#meter_{{id}}").html(value); + $("#subunit_{{id}}").html(value); } $("#{{id}}").keyup(evaluate_{{safe_id}}); $(document).ready(evaluate_{{safe_id}}()); 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 0b5804748..b80faee6d 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 |