From 62e452c62567064f047e81940487e80131567094 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Fri, 11 Feb 2022 14:13:42 +0100 Subject: [PATCH] WIP: CompensationAction using jstree --- compensation/forms/modalForms.py | 20 ++++++--- .../detail/compensation/view.html | 5 +++ .../compensation/detail/eco_account/view.html | 5 +++ ema/templates/ema/detail/view.html | 5 +++ intervention/inputs.py | 15 +++++++ .../widgets/checkbox-tree-select-base.html | 45 +++++++++++++++++++ .../widgets/checkbox-tree-select-content.html | 9 ++++ konova/urls.py | 4 +- konova/views.py | 24 ++++++++++ templates/form/scripts/jstree-scripts.html | 2 + 10 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 konova/templates/konova/widgets/checkbox-tree-select-base.html create mode 100644 konova/templates/konova/widgets/checkbox-tree-select-content.html create mode 100644 templates/form/scripts/jstree-scripts.html diff --git a/compensation/forms/modalForms.py b/compensation/forms/modalForms.py index 90b0b64..20ff5dd 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 3f843c0..17f79bc 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 f276f93..132e0b6 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 32ddd66..cdab570 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 2e84edc..9cf8e5a 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 0000000..793ab83 --- /dev/null +++ b/konova/templates/konova/widgets/checkbox-tree-select-base.html @@ -0,0 +1,45 @@ + +
+
+ + + + \ No newline at end of file diff --git a/konova/templates/konova/widgets/checkbox-tree-select-content.html b/konova/templates/konova/widgets/checkbox-tree-select-content.html new file mode 100644 index 0000000..13b3a34 --- /dev/null +++ b/konova/templates/konova/widgets/checkbox-tree-select-content.html @@ -0,0 +1,9 @@ +{% load l10n %} + + \ No newline at end of file diff --git a/konova/urls.py b/konova/urls.py index 68256e7..734be3c 100644 --- a/konova/urls.py +++ b/konova/urls.py @@ -23,7 +23,7 @@ from konova.autocompletes import EcoAccountAutocomplete, \ ShareUserAutocomplete, BiotopeExtraCodeAutocomplete, CompensationActionDetailCodeAutocomplete from konova.settings import SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY, DEBUG from konova.sso.sso import KonovaSSOClient -from konova.views import logout_view, home_view +from konova.views import logout_view, home_view, get_konova_code_action_children sso_client = KonovaSSOClient(SSO_SERVER, SSO_PUBLIC_KEY, SSO_PRIVATE_KEY) urlpatterns = [ @@ -40,6 +40,8 @@ urlpatterns = [ path('analysis/', include("analysis.urls")), path('api/', include("api.urls")), + path("codes/comp/action/children", get_konova_code_action_children, name="codes-action-children"), + # Autocomplete paths for all apps path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="accounts-autocomplete"), path("atcmplt/interventions", InterventionAutocomplete.as_view(), name="interventions-autocomplete"), diff --git a/konova/views.py b/konova/views.py index 2a4c8ad..2eac8b2 100644 --- a/konova/views.py +++ b/konova/views.py @@ -9,9 +9,12 @@ from django.contrib.auth import logout from django.contrib.auth.decorators import login_required from django.http import HttpRequest, FileResponse from django.shortcuts import redirect, render, get_object_or_404 +from django.template.loader import render_to_string from django.utils import timezone from django.utils.translation import gettext_lazy as _ +from codelist.models import KonovaCode +from codelist.settings import CODELIST_COMPENSATION_ACTION_ID from compensation.models import Compensation, EcoAccount from intervention.models import Intervention from konova.contexts import BaseContext @@ -124,3 +127,24 @@ def get_500_view(request: HttpRequest): """ context = BaseContext.context return render(request, "500.html", context, status=500) + + +@login_required +def get_konova_code_action_children(request: HttpRequest): + template = "konova/widgets/checkbox-tree-select-content.html" + _id = request.GET.get("id", None) + if _id == "#": + # Return all! + codes = KonovaCode.objects.filter( + code_lists__in=[CODELIST_COMPENSATION_ACTION_ID], + parent=None, + ) + else: + codes = KonovaCode.objects.filter( + code_lists__in=[CODELIST_COMPENSATION_ACTION_ID], + parent__id=_id, + ) + context = { + "codes": codes + } + return render(request, template, context) \ No newline at end of file diff --git a/templates/form/scripts/jstree-scripts.html b/templates/form/scripts/jstree-scripts.html new file mode 100644 index 0000000..c936760 --- /dev/null +++ b/templates/form/scripts/jstree-scripts.html @@ -0,0 +1,2 @@ + + \ No newline at end of file