diff --git a/compensation/forms/modalForms.py b/compensation/forms/modalForms.py index 90b0b64c..20ff5ddc 100644 --- a/compensation/forms/modalForms.py +++ b/compensation/forms/modalForms.py @@ -11,12 +11,14 @@ from django import forms from django.contrib import messages from django.http import HttpRequest, HttpResponseRedirect from django.shortcuts import render +from django.urls import reverse from django.utils.translation import pgettext_lazy as _con, gettext_lazy as _ from codelist.models import KonovaCode from codelist.settings import CODELIST_BIOTOPES_ID, CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID, \ CODELIST_COMPENSATION_ACTION_DETAIL_ID from compensation.models import CompensationDocument, EcoAccountDocument +from intervention.inputs import TreeSelectMultiple from konova.contexts import BaseContext from konova.forms import BaseModalForm, NewDocumentModalForm, RemoveModalForm from konova.models import DeadlineType @@ -411,15 +413,11 @@ class NewActionModalForm(BaseModalForm): required=True, help_text=_("Select the action type"), queryset=KonovaCode.objects.filter( - is_archived=False, - is_leaf=True, code_lists__in=[CODELIST_COMPENSATION_ACTION_ID], + is_archived=False, ), - widget=autocomplete.ModelSelect2Multiple( - url="codes-compensation-action-autocomplete", - attrs={ - "data-placeholder": _("Action"), - } + widget=TreeSelectMultiple( + url=None, ), ) action_type_details = forms.ModelMultipleChoiceField( @@ -482,6 +480,14 @@ class NewActionModalForm(BaseModalForm): super().__init__(*args, **kwargs) self.form_title = _("New action") self.form_caption = _("Insert data for the new action") + url = reverse("codes-action-children") + self.fields["action_type"].widget.attrs = { + "url": url, + } + + def is_valid(self): + super_valid = super().is_valid() + return super_valid def save(self): action = self.instance.add_action(self) diff --git a/compensation/templates/compensation/detail/compensation/view.html b/compensation/templates/compensation/detail/compensation/view.html index 3f843c0a..17f79bc8 100644 --- a/compensation/templates/compensation/detail/compensation/view.html +++ b/compensation/templates/compensation/detail/compensation/view.html @@ -2,6 +2,11 @@ {% load i18n l10n static fontawesome_5 humanize ksp_filters %} {% block head %} + {% comment %} + Needed for custom Checkbox Tree Select Widget + {% endcomment %} + {% include 'form/scripts/jstree-scripts.html' %} + {% comment %} dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. diff --git a/compensation/templates/compensation/detail/eco_account/view.html b/compensation/templates/compensation/detail/eco_account/view.html index f276f931..132e0b61 100644 --- a/compensation/templates/compensation/detail/eco_account/view.html +++ b/compensation/templates/compensation/detail/eco_account/view.html @@ -2,6 +2,11 @@ {% load i18n l10n static fontawesome_5 humanize %} {% block head %} + {% comment %} + Needed for custom Checkbox Tree Select Widget + {% endcomment %} + {% include 'form/scripts/jstree-scripts.html' %} + {% comment %} dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. diff --git a/ema/templates/ema/detail/view.html b/ema/templates/ema/detail/view.html index 32ddd66b..cdab570d 100644 --- a/ema/templates/ema/detail/view.html +++ b/ema/templates/ema/detail/view.html @@ -2,6 +2,11 @@ {% load i18n l10n static fontawesome_5 humanize %} {% block head %} + {% comment %} + Needed for custom Checkbox Tree Select Widget + {% endcomment %} + {% include 'form/scripts/jstree-scripts.html' %} + {% comment %} dal documentation (django-autocomplete-light) states using form.media for adding needed scripts. This does not work properly with modal forms, as the scripts are not loaded properly inside the modal. diff --git a/intervention/inputs.py b/intervention/inputs.py index 2e84edc5..9cf8e5ab 100644 --- a/intervention/inputs.py +++ b/intervention/inputs.py @@ -1,4 +1,5 @@ from django import forms +from django.urls import reverse class DummyFilterInput(forms.HiddenInput): @@ -30,3 +31,17 @@ class GenerateInput(forms.TextInput): """ template_name = "konova/widgets/generate-content-input.html" + + +class TreeSelectMultiple(forms.SelectMultiple): + """ Provides multiple selection of parent-child data + + """ + template_name = "konova/widgets/checkbox-tree-select-base.html" + url = None + + def __init__(self, *args, **kwargs): + self.url = kwargs.pop("url", None) + if self.url: + self.url = reverse(self.url) + super().__init__(*args, **kwargs) diff --git a/konova/templates/konova/widgets/checkbox-tree-select-base.html b/konova/templates/konova/widgets/checkbox-tree-select-base.html new file mode 100644 index 00000000..793ab83f --- /dev/null +++ b/konova/templates/konova/widgets/checkbox-tree-select-base.html @@ -0,0 +1,45 @@ + +